diff --git a/litellm/main.py b/litellm/main.py index cb3ddc2f401..049f0ab07ce 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -3090,7 +3090,7 @@ def completion( # type: ignore # noqa: PLR0915 print_verbose=print_verbose, optional_params=optional_params, litellm_params=litellm_params, - api_key=None, + api_key=api_key, logger_fn=logger_fn, encoding=_get_encoding(), logging_obj=logging, diff --git a/tests/litellm/llms/oobabooga/__init__.py b/tests/litellm/llms/oobabooga/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/litellm/llms/oobabooga/test_oobabooga_api_key.py b/tests/litellm/llms/oobabooga/test_oobabooga_api_key.py new file mode 100644 index 00000000000..1569c7b7173 --- /dev/null +++ b/tests/litellm/llms/oobabooga/test_oobabooga_api_key.py @@ -0,0 +1,94 @@ +""" +Unit tests for the oobabooga provider — verify api_key is forwarded. + +Regression test for https://github.com/BerriAI/litellm/issues/21945 +""" + +import os +import sys + +sys.path.insert( + 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../..")) +) + +from litellm.llms.oobabooga.chat.transformation import OobaboogaConfig + + +def test_validate_environment_sets_auth_header_when_api_key_provided(): + """api_key should produce an Authorization header.""" + config = OobaboogaConfig() + headers = config.validate_environment( + headers={}, + model="test-model", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key="my-secret-token", + ) + assert headers["Authorization"] == "Token my-secret-token" + + +def test_validate_environment_no_auth_header_when_api_key_none(): + """When api_key is None, Authorization header should not be set.""" + config = OobaboogaConfig() + headers = config.validate_environment( + headers={}, + model="test-model", + messages=[{"role": "user", "content": "hi"}], + optional_params={}, + litellm_params={}, + api_key=None, + ) + assert "Authorization" not in headers + + +def test_oobabooga_completion_forwards_api_key_to_http_request(): + """ + Regression test for https://github.com/BerriAI/litellm/issues/21945 + + Verify that calling oobabooga.completion() with an api_key results + in an Authorization header being sent in the outgoing HTTP request. + """ + from unittest.mock import MagicMock, patch + + from litellm.llms.oobabooga.chat import oobabooga + + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = { + "choices": [{"message": {"content": "hello"}}], + "usage": { + "prompt_tokens": 1, + "completion_tokens": 1, + "total_tokens": 2, + }, + } + + mock_client = MagicMock() + mock_client.post.return_value = mock_response + + with patch( + "litellm.llms.oobabooga.chat.oobabooga._get_httpx_client", + return_value=mock_client, + ): + from litellm.types.utils import ModelResponse + + oobabooga.completion( + model="test-model", + messages=[{"role": "user", "content": "hi"}], + api_base="http://localhost:5000", + model_response=ModelResponse(), + print_verbose=lambda *a, **kw: None, + encoding=None, + api_key="my-secret-token", + logging_obj=MagicMock(), + optional_params={}, + litellm_params={}, + ) + + mock_client.post.assert_called_once() + call_kwargs = mock_client.post.call_args + headers = call_kwargs.kwargs.get("headers") or call_kwargs[1].get("headers", {}) + assert headers.get("Authorization") == "Token my-secret-token", ( + f"Expected Authorization header, got: {headers}" + )