diff --git a/litellm/llms/ollama/completion/transformation.py b/litellm/llms/ollama/completion/transformation.py index ed14b6a3318..1bb29e56b88 100644 --- a/litellm/llms/ollama/completion/transformation.py +++ b/litellm/llms/ollama/completion/transformation.py @@ -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 {} diff --git a/tests/test_litellm/llms/ollama/test_ollama_model_info.py b/tests/test_litellm/llms/ollama/test_ollama_model_info.py index 5585e9d1e0e..fdddfb87dc0 100644 --- a/tests/test_litellm/llms/ollama/test_ollama_model_info.py +++ b/tests/test_litellm/llms/ollama/test_ollama_model_info.py @@ -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."""