mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-09 03:18:44 +00:00
fix(ollama): strip endpoint paths from api_base in get_model_info() (#22910)
Fix malformed URL construction in OllamaConfig.get_model_info() when api_base already contains an endpoint path (e.g., /api/generate). Before this fix, when api_base was passed with an endpoint path already appended (which happens through the completion flow via get_complete_url()), get_model_info() would naively append /api/show, resulting in URLs like: http://server:11434/api/generate/api/show (404 error) This fix strips known endpoint paths (/api/generate, /api/chat, /api/embed) before appending /api/show, following the same defensive pattern used elsewhere in the codebase (e.g., embedding handler, chat transformation). Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a58d859217
commit
47400782b0
2 changed files with 36 additions and 0 deletions
|
|
@ -238,6 +238,12 @@ class OllamaConfig(BaseConfig):
|
|||
or get_secret_str("OLLAMA_API_BASE")
|
||||
or "http://localhost:11434"
|
||||
)
|
||||
# Strip any endpoint paths that may have been appended by get_complete_url()
|
||||
# to avoid malformed URLs like /api/generate/api/show
|
||||
for endpoint in ["/api/generate", "/api/chat", "/api/embed"]:
|
||||
if api_base.endswith(endpoint):
|
||||
api_base = api_base[: -len(endpoint)]
|
||||
break
|
||||
api_key = self.get_api_key()
|
||||
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
|
||||
|
||||
|
|
|
|||
|
|
@ -219,6 +219,36 @@ class TestOllamaGetModelInfo:
|
|||
config.get_model_info("ollama_chat/llama3", api_base="http://localhost:11434")
|
||||
assert captured_json[1]["name"] == "llama3"
|
||||
|
||||
def test_get_model_info_strips_endpoint_paths_from_api_base(self, monkeypatch):
|
||||
"""When api_base contains endpoint paths like /api/generate, they should be stripped before appending /api/show."""
|
||||
from litellm.llms.ollama.completion.transformation import OllamaConfig
|
||||
|
||||
captured_urls = []
|
||||
|
||||
def mock_post(url, json, headers=None):
|
||||
captured_urls.append(url)
|
||||
return DummyResponse({"template": "", "model_info": {}}, status_code=200)
|
||||
|
||||
monkeypatch.setattr("litellm.module_level_client.post", mock_post)
|
||||
|
||||
config = OllamaConfig()
|
||||
|
||||
# Test with /api/generate endpoint already appended
|
||||
config.get_model_info("llama3", api_base="http://my-server:11434/api/generate")
|
||||
assert captured_urls[0] == "http://my-server:11434/api/show"
|
||||
|
||||
# Test with /api/chat endpoint already appended
|
||||
config.get_model_info("llama3", api_base="http://my-server:11434/api/chat")
|
||||
assert captured_urls[1] == "http://my-server:11434/api/show"
|
||||
|
||||
# Test with /api/embed endpoint already appended
|
||||
config.get_model_info("llama3", api_base="http://my-server:11434/api/embed")
|
||||
assert captured_urls[2] == "http://my-server:11434/api/show"
|
||||
|
||||
# Test with clean base URL (should still work)
|
||||
config.get_model_info("llama3", api_base="http://my-server:11434")
|
||||
assert captured_urls[3] == "http://my-server:11434/api/show"
|
||||
|
||||
|
||||
class TestOllamaAuthHeaders:
|
||||
"""Tests for Ollama authentication header handling in completion calls."""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue