diff --git a/litellm/llms/openai_like/README.md b/litellm/llms/openai_like/README.md index fb00b07c986..4878380a7f3 100644 --- a/litellm/llms/openai_like/README.md +++ b/litellm/llms/openai_like/README.md @@ -62,7 +62,10 @@ That's it! The provider will be automatically loaded and available. // Optional: Special handling flags "special_handling": { - "convert_content_list_to_string": true + "convert_content_list_to_string": true, + // Refuse the request when neither api_key nor api_key_env is set, + // instead of falling back to OpenAI credentials + "require_api_key": true }, // Optional: Capabilities every model of this provider has, used when a diff --git a/litellm/llms/openai_like/providers.json b/litellm/llms/openai_like/providers.json index 99998e25324..c5256bb7bd4 100644 --- a/litellm/llms/openai_like/providers.json +++ b/litellm/llms/openai_like/providers.json @@ -319,6 +319,9 @@ "base_url": "https://aistupidlevel.info/v1", "api_key_env": "AISTUPIDLEVEL_API_KEY", "api_base_env": "AISTUPIDLEVEL_API_BASE", + "special_handling": { + "require_api_key": true + }, "provider_info": { "supports_function_calling": true } diff --git a/litellm/main.py b/litellm/main.py index 8cb133bf76b..a418386a5eb 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -2792,6 +2792,15 @@ def _complete_custom_openai( raise litellm.AuthenticationError( message="Set BIGMODEL_API_KEY or pass api_key", llm_provider="bigmodel", model=model ) + # A JSON provider with `require_api_key` gets its own key or nothing: without this, the + # fallback below sends litellm.openai_key / OPENAI_API_KEY to the provider's host. + json_provider: Final = None if api_key else JSONProviderRegistry.get(custom_llm_provider) + if json_provider is not None and json_provider.special_handling.get("require_api_key"): + raise litellm.AuthenticationError( + message=f"Set {json_provider.api_key_env} or pass api_key", + llm_provider=custom_llm_provider, + model=model, + ) organization = ( organization or (None if custom_llm_provider == "bigmodel" else litellm.organization or get_secret("OPENAI_ORGANIZATION")) diff --git a/tests/unit/llms/openai_like/test_aistupidlevel_provider.py b/tests/unit/llms/openai_like/test_aistupidlevel_provider.py index 87fd0594766..283e25c1b57 100644 --- a/tests/unit/llms/openai_like/test_aistupidlevel_provider.py +++ b/tests/unit/llms/openai_like/test_aistupidlevel_provider.py @@ -171,6 +171,42 @@ def test_aistupidlevel_chat_completion_request(): assert response.choices[0].message.content == "Hello from the router" +@pytest.mark.asyncio +@pytest.mark.parametrize("use_async", [False, True]) +async def test_aistupidlevel_missing_key_never_sends_openai_credentials( + monkeypatch: pytest.MonkeyPatch, use_async: bool +): + monkeypatch.delenv("AISTUPIDLEVEL_API_KEY", raising=False) + monkeypatch.setenv("OPENAI_API_KEY", "sk-openai-secret") + monkeypatch.setattr(litellm, "openai_key", "sk-openai-secret") + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setattr(litellm, "in_memory_llm_clients_cache", LLMClientCache()) + with respx.mock(assert_all_called=False) as upstream: + route: Final = upstream.post(f"{_BASE}/chat/completions").respond(200, json=_CHAT_COMPLETION) + with pytest.raises(litellm.AuthenticationError, match="AISTUPIDLEVEL_API_KEY"): + kwargs: Final = { + "model": "aistupidlevel/auto-coding", + "messages": [{"role": "user", "content": "Say hello"}], + } + if use_async: + await litellm.acompletion(**kwargs) + else: + litellm.completion(**kwargs) + + assert route.call_count == 0 + + +def test_aistupidlevel_env_key_is_sent_instead_of_openai_credentials(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("AISTUPIDLEVEL_API_KEY", "aism-env-key") + monkeypatch.setenv("OPENAI_API_KEY", "sk-openai-secret") + monkeypatch.setattr(litellm, "openai_key", "sk-openai-secret") + with respx.mock() as upstream: + route: Final = upstream.post(f"{_BASE}/chat/completions").respond(200, json=_CHAT_COMPLETION) + litellm.completion(model="aistupidlevel/auto-coding", messages=[{"role": "user", "content": "Say hello"}]) + + assert route.calls.last.request.headers["authorization"] == "Bearer aism-env-key" + + def test_aistupidlevel_tools_are_forwarded(): tools: Final = [ {