diff --git a/litellm/main.py b/litellm/main.py index 73db4a11cb7..3a338799c1f 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -3969,8 +3969,8 @@ def completion( # type: ignore # noqa: PLR0915 response = model_response elif custom_llm_provider == "ollama": api_base = ( - litellm.api_base - or api_base + api_base + or litellm.api_base or get_secret("OLLAMA_API_BASE") or "http://localhost:11434" ) @@ -3998,8 +3998,8 @@ def completion( # type: ignore # noqa: PLR0915 elif custom_llm_provider == "ollama_chat": api_base = ( - litellm.api_base - or api_base + api_base + or litellm.api_base or get_secret("OLLAMA_API_BASE") or "http://localhost:11434" ) @@ -5317,8 +5317,8 @@ def embedding( # noqa: PLR0915 ) elif custom_llm_provider == "ollama": api_base = ( - litellm.api_base - or api_base + api_base + or litellm.api_base or get_secret_str("OLLAMA_API_BASE") or "http://localhost:11434" ) # type: ignore diff --git a/tests/local_testing/test_completion.py b/tests/local_testing/test_completion.py index d6370bfd049..b4d6e205a76 100644 --- a/tests/local_testing/test_completion.py +++ b/tests/local_testing/test_completion.py @@ -1323,6 +1323,37 @@ def test_lm_studio_completion(monkeypatch): print(e) +@pytest.mark.parametrize("model", ["ollama/phi", "ollama_chat/phi"]) +def test_ollama_completion_explicit_api_base_takes_precedence(monkeypatch, model): + monkeypatch.setattr(litellm, "api_base", "https://api.deepseek.com") + + with patch("litellm.main.base_llm_http_handler.completion") as mock_completion: + mock_completion.return_value = MagicMock() + + litellm.completion( + model=model, + messages=[{"role": "user", "content": "Hello"}], + api_base="http://localhost:11434", + ) + + assert mock_completion.call_args.kwargs["api_base"] == "http://localhost:11434" + + +def test_ollama_embedding_explicit_api_base_takes_precedence(monkeypatch): + monkeypatch.setattr(litellm, "api_base", "https://api.deepseek.com") + + with patch("litellm.main.ollama.ollama_embeddings") as mock_embeddings: + mock_embeddings.return_value = MagicMock() + + litellm.embedding( + model="ollama/qwen3-embedding:0.6b", + input="hello", + api_base="http://localhost:11434", + ) + + assert mock_embeddings.call_args.kwargs["api_base"] == "http://localhost:11434" + + # ################### Hugging Face Conversational models ######################## # def hf_test_completion_conv(): # try: