diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 0fdfb301291..72205237951 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -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: diff --git a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py index 9de768ea47b..7896dec485c 100644 --- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py @@ -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", [