diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.py index 55d6603f8ab..bd624bfbfa0 100644 --- a/litellm/llms/ollama/chat/transformation.py +++ b/litellm/llms/ollama/chat/transformation.py @@ -222,14 +222,8 @@ class OllamaChatConfig(BaseConfig): Some providers need `model` in `api_base` """ - if api_base is None: - api_base = "http://localhost:11434" - if api_base.endswith("/api/chat"): - url = api_base - else: - url = f"{api_base}/api/chat" - - return url + base: Final = (api_base or "http://localhost:11434").rstrip("/").removesuffix("/api/generate") + return base if base.endswith("/api/chat") else f"{base}/api/chat" def transform_request( self, diff --git a/litellm/main.py b/litellm/main.py index 5bcfeaebce9..8984f6abfec 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -5309,8 +5309,6 @@ def completion( ) if custom_llm_provider == "ollama" and (tools or functions): custom_llm_provider = "ollama_chat" # rebind-ok: /api/generate has no native tool calling - if api_base is not None: - api_base = api_base.rstrip("/").removesuffix("/api/generate") # rebind-ok: chat path elif custom_llm_provider == "ollama": tools = None # rebind-ok: empty tools must not change plain completion behavior functions = None # rebind-ok: empty functions must not change plain completion behavior diff --git a/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py b/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py index 25f9645faa0..69d6eb35ced 100644 --- a/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py +++ b/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py @@ -944,3 +944,23 @@ class TestOllamaToolCallTransformation: assert tool_msg["content"] == "Sunny, 72°F" assert "tool_call_id" in tool_msg, "tool_call_id must be forwarded to Ollama" assert tool_msg["tool_call_id"] == "call_abc123" + + +@pytest.mark.parametrize( + ("api_base", "expected_url"), + [ + (None, "http://localhost:11434/api/chat"), + ("http://ollama.example:11434", "http://ollama.example:11434/api/chat"), + ("http://ollama.example:11434/", "http://ollama.example:11434/api/chat"), + ("http://ollama.example:11434/api/chat", "http://ollama.example:11434/api/chat"), + ("http://ollama.example:11434/api/chat/", "http://ollama.example:11434/api/chat"), + ("http://ollama.example:11434/api/generate", "http://ollama.example:11434/api/chat"), + ("http://ollama.example:11434/prefix/api/generate/", "http://ollama.example:11434/prefix/api/chat"), + ], +) +def test_get_complete_url_points_at_chat_endpoint(api_base, expected_url): + url = OllamaChatConfig().get_complete_url( + api_base=api_base, api_key=None, model="qwen3.8:27b", optional_params={}, litellm_params={} + ) + + assert url == expected_url