diff --git a/litellm/main.py b/litellm/main.py index 17edafcdfca..c72cd7554e7 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -1065,7 +1065,7 @@ def responses_api_bridge_check( try: model_info = cast( dict, - _get_model_info_helper(model=model, custom_llm_provider=custom_llm_provider), + _get_model_info_helper(model=model, custom_llm_provider=custom_llm_provider, api_base=api_base), ) if model_info.get("mode") is None and model.startswith("responses/"): model = model.replace("responses/", "") @@ -4325,7 +4325,7 @@ def _complete_ollama(ctx: _CompletionDispatchContext) -> _CompletionDispatchResu stream: Final = ctx.stream timeout: Final = ctx.timeout - api_base = litellm.api_base or api_base or get_secret("OLLAMA_API_BASE") or "http://localhost:11434" + api_base = api_base or litellm.api_base or get_secret("OLLAMA_API_BASE") or "http://localhost:11434" if api_key is not None and "Authorization" not in headers: headers["Authorization"] = f"Bearer {api_key}" @@ -4365,7 +4365,7 @@ def _complete_ollama_chat(ctx: _CompletionDispatchContext) -> _CompletionDispatc stream: Final = ctx.stream timeout: Final = ctx.timeout - api_base = litellm.api_base or api_base or get_secret("OLLAMA_API_BASE") or "http://localhost:11434" + api_base = api_base or litellm.api_base or get_secret("OLLAMA_API_BASE") or "http://localhost:11434" api_key = api_key or litellm.ollama_key or os.environ.get("OLLAMA_API_KEY") or litellm.api_key if api_key is not None and "Authorization" not in headers: @@ -6744,7 +6744,7 @@ def embedding( api_key=api_key, ) elif custom_llm_provider == "ollama": - api_base = litellm.api_base or api_base or get_secret_str("OLLAMA_API_BASE") or "http://localhost:11434" + api_base = api_base or litellm.api_base or get_secret_str("OLLAMA_API_BASE") or "http://localhost:11434" if isinstance(input, str): input = [input] diff --git a/tests/test_litellm/test_main.py b/tests/test_litellm/test_main.py index f71225c6fc5..4e882589969 100644 --- a/tests/test_litellm/test_main.py +++ b/tests/test_litellm/test_main.py @@ -815,6 +815,96 @@ def test_responses_api_bridge_check_gpt_5_5_tools_plus_reasoning_routes_to_respo assert model_info.get("mode") == "responses" +_OLLAMA_SHOW_RESPONSE: Final = { + "model_info": {"llama.context_length": 8192}, + "capabilities": ["completion"], +} + +_OLLAMA_GENERATE_RESPONSE: Final = { + "model": "llama3-custom", + "response": "hello from ollama", + "done": True, + "done_reason": "stop", + "prompt_eval_count": 5, + "eval_count": 4, +} + +_OLLAMA_CHAT_RESPONSE: Final = { + "model": "llama3-custom", + "message": {"role": "assistant", "content": "hello from ollama"}, + "done": True, + "done_reason": "stop", + "prompt_eval_count": 5, + "eval_count": 4, +} + + +def test_responses_api_bridge_check_forwards_api_base_to_model_info_lookup(respx_mock: respx.MockRouter): + """Regression test for https://github.com/BerriAI/litellm/issues/37041 -- the bridge + check's model-info lookup must hit the request's api_base, not fall back to the + provider default (localhost:11434 for ollama).""" + from litellm.main import responses_api_bridge_check + + show_route = respx_mock.post("http://my-host:30000/api/show").respond(json=_OLLAMA_SHOW_RESPONSE) + + litellm.get_model_info.cache_clear() + model_info, model = responses_api_bridge_check( + model="llama3-custom", + custom_llm_provider="ollama", + api_base="http://my-host:30000", + ) + + assert show_route.called + assert model == "llama3-custom" + assert model_info["max_tokens"] == 8192 + + +@pytest.mark.parametrize( + "model, completion_path, completion_response", + [ + ("ollama/llama3-custom", "/api/generate", _OLLAMA_GENERATE_RESPONSE), + ("ollama_chat/llama3-custom", "/api/chat", _OLLAMA_CHAT_RESPONSE), + ], +) +def test_ollama_completion_explicit_api_base_overrides_global( + model, completion_path, completion_response, monkeypatch, respx_mock: respx.MockRouter +): + """Regression test for https://github.com/BerriAI/litellm/issues/26170 -- the explicit + api_base kwarg must win over the litellm.api_base global, matching the openai provider.""" + monkeypatch.setattr(litellm, "api_base", "https://unrelated-global.example.com") + respx_mock.post("http://my-host:30000/api/show").respond(json=_OLLAMA_SHOW_RESPONSE) + completion_route = respx_mock.post(f"http://my-host:30000{completion_path}").respond(json=completion_response) + + litellm.get_model_info.cache_clear() + response = litellm.completion( + model=model, + messages=[{"role": "user", "content": "hi"}], + api_base="http://my-host:30000", + ) + + assert completion_route.called + assert response.choices[0].message.content == "hello from ollama" + + +def test_ollama_embedding_explicit_api_base_overrides_global(monkeypatch, respx_mock: respx.MockRouter): + """Regression test for https://github.com/BerriAI/litellm/issues/26170 (embedding path).""" + monkeypatch.setattr(litellm, "api_base", "https://unrelated-global.example.com") + respx_mock.post("http://my-host:30000/api/show").respond(json=_OLLAMA_SHOW_RESPONSE) + embed_route = respx_mock.post("http://my-host:30000/api/embed").respond( + json={"model": "qwen3-embedding:0.6b", "embeddings": [[0.1, 0.2, 0.3]], "prompt_eval_count": 2} + ) + + litellm.get_model_info.cache_clear() + response = litellm.embedding( + model="ollama/qwen3-embedding:0.6b", + input="hello", + api_base="http://my-host:30000", + ) + + assert embed_route.called + assert response.data[0]["embedding"] == [0.1, 0.2, 0.3] + + def test_responses_api_bridge_check_azure_gpt_5_4_tools_plus_reasoning_routes_to_responses(): """Azure gpt-5.4 with both tools and reasoning_effort should route to Responses API.""" from litellm.main import responses_api_bridge_check