fix(openai): return 401 for missing local credentials

This commit is contained in:
林SO 2026-09-28 00:55:51 +08:00
parent 22b36cbcf6
commit bde66586a1
2 changed files with 28 additions and 1 deletions

View file

@ -401,12 +401,12 @@ def _map_openai_exception(
elif (
"The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"
in error_str
or ("Missing credentials" in error_str and "api_key" in error_str)
):
raise AuthenticationError(
message=f"AuthenticationError: {exception_provider} - {message}",
llm_provider=custom_llm_provider,
model=model,
response=response,
litellm_debug_info=extra_information,
)
elif "Mistral API raised a streaming error" in error_str:

View file

@ -1,3 +1,5 @@
from typing import Final
import httpx
import openai
import pytest
@ -360,6 +362,31 @@ def test_openai_compatible_429_still_maps_to_rate_limit():
assert excinfo.value.status_code == 429
@pytest.mark.parametrize(
"error_message",
(
"The api_key client option must be set either by passing api_key to the client "
"or by setting the OPENAI_API_KEY environment variable",
"Missing credentials. Please pass an `api_key`, `workload_identity`, "
"`admin_api_key`, or set the `OPENAI_API_KEY` or `OPENAI_ADMIN_KEY` "
"environment variable.",
),
)
def test_openai_missing_credentials_maps_to_authentication_error(error_message: str) -> None:
original_exception: Final = OpenAIError(status_code=500, message=error_message)
with pytest.raises(litellm.AuthenticationError) as excinfo:
exception_type(
model="test-model",
original_exception=original_exception,
custom_llm_provider="openai",
)
assert excinfo.value.status_code == 401
assert excinfo.value.response.status_code == 401
assert excinfo.value.llm_provider == "openai"
@pytest.mark.parametrize(
"error_message",
[