mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(aistupidlevel): never send OpenAI credentials when the provider key is missing
With AISTUPIDLEVEL_API_KEY unset and no api_key passed, the shared chat path fell back to litellm.openai_key / OPENAI_API_KEY and sent that key to aistupidlevel.info. JSON providers can now opt in to `special_handling.require_api_key`: the request is refused with an AuthenticationError naming the provider's env var before any client is built (the OpenAI client would otherwise read OPENAI_API_KEY itself). aistupidlevel sets it. Other providers are unchanged. Tests cover sync and async, and that the provider's own key is sent when both are present.
This commit is contained in:
parent
dc6bd593b3
commit
2df02d226a
4 changed files with 52 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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 = [
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue