diff --git a/tests/local_testing/test_completion.py b/tests/local_testing/test_completion.py index 583a21d9429..55b62bc80f5 100644 --- a/tests/local_testing/test_completion.py +++ b/tests/local_testing/test_completion.py @@ -1323,52 +1323,6 @@ 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" - - -@pytest.mark.asyncio -async def test_ollama_aembedding_explicit_api_base_takes_precedence(monkeypatch): - monkeypatch.setattr(litellm, "api_base", "https://api.deepseek.com") - - with patch("litellm.main.ollama.ollama_aembeddings") as mock_aembeddings: - mock_aembeddings.return_value = MagicMock() - - await litellm.aembedding( - model="ollama/qwen3-embedding:0.6b", - input="hello", - api_base="http://localhost:11434", - ) - - assert mock_aembeddings.call_args.kwargs["api_base"] == "http://localhost:11434" - # ################### Hugging Face Conversational models ######################## # def hf_test_completion_conv(): diff --git a/tests/test_litellm/llms/ollama/test_ollama_api_base_precedence.py b/tests/test_litellm/llms/ollama/test_ollama_api_base_precedence.py new file mode 100644 index 00000000000..c31f22c294d --- /dev/null +++ b/tests/test_litellm/llms/ollama/test_ollama_api_base_precedence.py @@ -0,0 +1,62 @@ +"""Regression tests for ollama `api_base` precedence. + +Explicit `api_base` kwargs must override `litellm.api_base` global, across: + - ollama / ollama_chat completion + - ollama embedding (sync) + - ollama aembedding (async) + +Fixes the precedence bug in `litellm.main` where `litellm.api_base or api_base` +swallowed explicit kwargs whenever the global was set. +""" +from unittest.mock import MagicMock, patch + +import pytest + +import litellm + + +@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" + + +@pytest.mark.asyncio +async def test_ollama_aembedding_explicit_api_base_takes_precedence(monkeypatch): + monkeypatch.setattr(litellm, "api_base", "https://api.deepseek.com") + + with patch("litellm.main.ollama.ollama_aembeddings") as mock_aembeddings: + mock_aembeddings.return_value = MagicMock() + + await litellm.aembedding( + model="ollama/qwen3-embedding:0.6b", + input="hello", + api_base="http://localhost:11434", + ) + + assert mock_aembeddings.call_args.kwargs["api_base"] == "http://localhost:11434"