fix(ollama): build the chat url from any api_base form

The /api/generate suffix was only stripped from the api_base argument, so a
global litellm.api_base ending in /api/generate still produced
/api/generate/api/chat for rerouted tool requests. Normalize in
OllamaChatConfig.get_complete_url instead, which every api_base source goes
through, and drop the extra rebind in completion(). Trailing slashes no longer
produce //api/chat either
This commit is contained in:
Meryem Sakin 2026-09-10 22:36:33 +03:00
parent fc9ee293e2
commit a0a9c05a5a
3 changed files with 22 additions and 10 deletions

View file

@ -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,

View file

@ -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

View file

@ -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