From e7fe73f2f3cbda31835b0faf663d6501c72cd406 Mon Sep 17 00:00:00 2001 From: lishen802 Date: Wed, 4 Nov 2026 09:52:27 +0800 Subject: [PATCH 1/2] fix(exception_mapping): map OpenAI 403 replies to PermissionDeniedError The OpenAI mapper had no 403 case, so a 403 surfaced as a generic APIError, or as BadRequestError when the body type was invalid_request_error, while the openai SDK raises PermissionDeniedError for the same reply. Map both paths to litellm.PermissionDeniedError, like the Anthropic and Vertex AI mappers do. The Azure, OpenRouter, and HuggingFace mappers also map a 403 to PermissionDeniedError instead of APIError, not only the OpenAI one. Fixes #49936 --- .../exception_mapping_utils.py | 75 +++++++++++++++++++ .../test_exception_mapping_utils.py | 36 ++++++++- .../azure/test_azure_exception_mapping.py | 22 +++++- tests/unit/llms/openai/test_openai.py | 2 +- 4 files changed, 130 insertions(+), 5 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index f1195802fc5..80abe1169af 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -319,6 +319,31 @@ def _upstream_reply(response: httpx.Response | None) -> httpx.Response | None: return response if response is not None and (response.headers or response.content) else None +def _permission_denied_error( + *, + model: str, + message: str, + custom_llm_provider: str, + exception_provider: str, + response: httpx.Response | None, + extra_information: str, + original_exception: _ProviderHTTPException | None = None, +) -> PermissionDeniedError: + """OpenAI-compatible 403 replies, matching openai.PermissionDeniedError (#49936)""" + try: + req = getattr(original_exception, "request", None) + except RuntimeError: # httpx raises when the request was never set + req = None + request: Final = req if isinstance(req, httpx.Request) else httpx.Request("POST", "https://litellm.ai") + return PermissionDeniedError( + message=f"PermissionDeniedError: {exception_provider} - {message}", + llm_provider=custom_llm_provider, + model=model, + response=response if response is not None else httpx.Response(status_code=403, request=request), + litellm_debug_info=extra_information, + ) + + def _map_openai_exception( *, model: str, @@ -438,6 +463,16 @@ def _map_openai_exception( litellm_debug_info=extra_information, body=getattr(original_exception, "body", None), ) + if invalid_request_status == 403: + raise _permission_denied_error( + model=model, + message=message, + custom_llm_provider=custom_llm_provider, + exception_provider=exception_provider, + response=response, + extra_information=extra_information, + original_exception=original_exception, + ) if invalid_request_status == 404: raise NotFoundError( message=f"{exception_provider} - {message}", @@ -515,6 +550,16 @@ def _map_openai_exception( response=response, litellm_debug_info=extra_information, ) + elif original_exception.status_code == 403: + raise _permission_denied_error( + model=model, + message=message, + custom_llm_provider=custom_llm_provider, + exception_provider=exception_provider, + response=response, + extra_information=extra_information, + original_exception=original_exception, + ) elif original_exception.status_code == 404: raise NotFoundError( message=f"NotFoundError: {exception_provider} - {message}", @@ -1649,6 +1694,16 @@ def _map_huggingface_exception( model=model, response=getattr(original_exception, "response", None), ) + elif original_exception.status_code == 403: + raise _permission_denied_error( + model=model, + message=original_exception.message, + custom_llm_provider="huggingface", + exception_provider="HuggingfaceException", + response=getattr(original_exception, "response", None), + extra_information=extra_information, + original_exception=original_exception, + ) elif original_exception.status_code == 400: raise BadRequestError( message=f"HuggingfaceException - {original_exception.message}", @@ -2194,6 +2249,16 @@ def _map_azure_exception( litellm_debug_info=extra_information, response=response, ) + elif original_exception.status_code == 403: + raise _permission_denied_error( + model=model, + message=message, + custom_llm_provider=custom_llm_provider, + exception_provider=exception_provider, + response=response, + extra_information=extra_information, + original_exception=original_exception, + ) elif original_exception.status_code == 404: raise NotFoundError( message=f"AzureException NotFoundError - {message}", @@ -2302,6 +2367,16 @@ def _map_openrouter_exception( response=getattr(original_exception, "response", None), litellm_debug_info=extra_information, ) + elif original_exception.status_code == 403: + raise _permission_denied_error( + model=model, + message=error_str, + custom_llm_provider=custom_llm_provider, + exception_provider=exception_provider, + response=getattr(original_exception, "response", None), + extra_information=extra_information, + original_exception=original_exception, + ) elif original_exception.status_code == 404: raise NotFoundError( message=f"NotFoundError: {exception_provider} - {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 1b00a32d31d..2d7b532cacd 100644 --- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py @@ -1141,8 +1141,8 @@ def test_openai_invalid_request_error_without_a_status_code_maps_to_bad_request( @pytest.mark.parametrize("provider", ["openai", "groq", "deepseek"]) -@pytest.mark.parametrize("status_code", [403, 409, 422, 429, 500, 503]) -def test_openai_invalid_request_error_keeps_bad_request_for_statuses_other_than_401_and_404( +@pytest.mark.parametrize("status_code", [409, 422, 429, 500, 503]) +def test_openai_invalid_request_error_keeps_bad_request_for_statuses_other_than_401_403_and_404( provider: str, status_code: int ) -> None: message: Final = "Cannot cancel a batch that is already completed." @@ -1244,7 +1244,7 @@ OPENAI_SHAPED = { 400: (litellm.BadRequestError, 400), 401: (litellm.AuthenticationError, 401), 402: (litellm.PaymentRequiredError, 402), - 403: (litellm.APIError, 403), + 403: (litellm.PermissionDeniedError, 403), 404: (litellm.NotFoundError, 404), 408: (litellm.Timeout, 408), 422: (litellm.BadRequestError, 422), @@ -3167,3 +3167,33 @@ def test_openai_error_matched_by_its_message_keeps_the_upstream_body( assert error_response.headers["x-request-id"] == "req-openai" assert "sk-openai-secret" not in str(error_response.request.headers) assert error_response.request.content == b"" + + +@pytest.mark.parametrize("provider", ["openai", "groq", "deepseek"]) +@pytest.mark.parametrize("error_type", ["request_forbidden", "invalid_request_error"]) +def test_openai_403_maps_to_permission_denied(provider: str, error_type: str) -> None: + """Regression for #49936, a 403 must raise PermissionDeniedError like the openai SDK does""" + message: Final = "Country, region, or territory not supported" + error_message: Final = json.dumps( + { + "error": { + "message": message, + "type": error_type, + "param": None, + "code": "unsupported_country_region_territory", + } + } + ) + original_exception: Final = OpenAIError(status_code=403, message=error_message, headers={}) + + with pytest.raises(openai.APIError) as excinfo: + exception_type( + model="gpt-4o", + original_exception=original_exception, + custom_llm_provider=provider, + ) + + assert type(excinfo.value) is litellm.PermissionDeniedError + assert isinstance(excinfo.value, openai.PermissionDeniedError) + assert excinfo.value.status_code == 403 + assert message in excinfo.value.message diff --git a/tests/unit/llms/azure/test_azure_exception_mapping.py b/tests/unit/llms/azure/test_azure_exception_mapping.py index 018b499f401..c914a210160 100644 --- a/tests/unit/llms/azure/test_azure_exception_mapping.py +++ b/tests/unit/llms/azure/test_azure_exception_mapping.py @@ -85,6 +85,26 @@ async def test_azure_content_policy_stream_ends_with_one_finish_reason(monkeypat class TestAzureExceptionMapping: """Test Azure OpenAI exception mapping with provider-specific fields""" + def test_azure_403_content_filter_stays_content_policy_violation(self) -> None: + """A 403 content_filter reply must stay ContentPolicyViolationError, not PermissionDeniedError (#49936)""" + mock_exception = Exception( + "The response was filtered due to the prompt triggering Azure OpenAI's content management policy" + ) + mock_exception.status_code = 403 + mock_exception.body = {"code": "content_filter", "message": "filtered"} + mock_response = MagicMock() + mock_response.status_code = 403 + mock_exception.response = mock_response + + with pytest.raises(ContentPolicyViolationError) as exc_info: + exception_type( + model="azure/gpt-4", + original_exception=mock_exception, + custom_llm_provider="azure", + ) + + assert not isinstance(exc_info.value, litellm.PermissionDeniedError) + def test_azure_content_policy_violation_innererror_access(self): """Test that Azure content policy violation exceptions provide access to innererror details""" @@ -738,7 +758,7 @@ async def _azure_atext_completion_stream() -> None: ( (400, _AZURE_REFUSAL_BODY, BadRequestError), (400, _AZURE_CONTENT_FILTER_BODY, BadRequestError), - (403, _AZURE_REFUSAL_BODY, litellm.APIError), + (403, _AZURE_REFUSAL_BODY, litellm.PermissionDeniedError), (404, _AZURE_DEPLOYMENT_NOT_FOUND_BODY, litellm.NotFoundError), (500, _AZURE_INTERNAL_ERROR_BODY, openai.APIError), (502, _AZURE_REFUSAL_BODY, litellm.BadGatewayError), diff --git a/tests/unit/llms/openai/test_openai.py b/tests/unit/llms/openai/test_openai.py index 77a7c3b83fd..0b9ff08e6f5 100644 --- a/tests/unit/llms/openai/test_openai.py +++ b/tests/unit/llms/openai/test_openai.py @@ -549,7 +549,7 @@ _QUERY_SECRET: Final = "query-secret" ("status_code", "mapped_error", "body"), ( (400, litellm.BadRequestError, _REFUSAL_BODY), - (403, litellm.APIError, _REFUSAL_BODY), + (403, litellm.PermissionDeniedError, _REFUSAL_BODY), (408, litellm.Timeout, _REFUSAL_BODY), (408, litellm.Timeout, _GATEWAY_TIMEOUT_BODY), (409, litellm.APIError, _REFUSAL_BODY), From d4469a5c02cbeb72b06459851faa917b1a8f2a4c Mon Sep 17 00:00:00 2001 From: lishen802 Date: Wed, 4 Nov 2026 10:58:10 +0800 Subject: [PATCH 2/2] fix(exception_mapping): do not carry the upstream request on the fallback 403 response --- .../exception_mapping_utils.py | 17 ++++--------- .../test_exception_mapping_utils.py | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 80abe1169af..a85e646078e 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -327,14 +327,12 @@ def _permission_denied_error( exception_provider: str, response: httpx.Response | None, extra_information: str, - original_exception: _ProviderHTTPException | None = None, ) -> PermissionDeniedError: - """OpenAI-compatible 403 replies, matching openai.PermissionDeniedError (#49936)""" - try: - req = getattr(original_exception, "request", None) - except RuntimeError: # httpx raises when the request was never set - req = None - request: Final = req if isinstance(req, httpx.Request) else httpx.Request("POST", "https://litellm.ai") + """OpenAI-compatible 403 replies, matching openai.PermissionDeniedError (#49936) + + The fallback response uses a neutral request so upstream credentials, query and body never leak into it + """ + request: Final = httpx.Request("POST", "https://litellm.ai") return PermissionDeniedError( message=f"PermissionDeniedError: {exception_provider} - {message}", llm_provider=custom_llm_provider, @@ -471,7 +469,6 @@ def _map_openai_exception( exception_provider=exception_provider, response=response, extra_information=extra_information, - original_exception=original_exception, ) if invalid_request_status == 404: raise NotFoundError( @@ -558,7 +555,6 @@ def _map_openai_exception( exception_provider=exception_provider, response=response, extra_information=extra_information, - original_exception=original_exception, ) elif original_exception.status_code == 404: raise NotFoundError( @@ -1702,7 +1698,6 @@ def _map_huggingface_exception( exception_provider="HuggingfaceException", response=getattr(original_exception, "response", None), extra_information=extra_information, - original_exception=original_exception, ) elif original_exception.status_code == 400: raise BadRequestError( @@ -2257,7 +2252,6 @@ def _map_azure_exception( exception_provider=exception_provider, response=response, extra_information=extra_information, - original_exception=original_exception, ) elif original_exception.status_code == 404: raise NotFoundError( @@ -2375,7 +2369,6 @@ def _map_openrouter_exception( exception_provider=exception_provider, response=getattr(original_exception, "response", None), extra_information=extra_information, - original_exception=original_exception, ) elif original_exception.status_code == 404: raise NotFoundError( 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 2d7b532cacd..e6496f55d0e 100644 --- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py @@ -3197,3 +3197,27 @@ def test_openai_403_maps_to_permission_denied(provider: str, error_type: str) -> assert isinstance(excinfo.value, openai.PermissionDeniedError) assert excinfo.value.status_code == 403 assert message in excinfo.value.message + + +@pytest.mark.parametrize("provider", ["openrouter", "huggingface"]) +def test_fallback_403_response_does_not_carry_the_upstream_request(provider: str) -> None: + """The stub response on a 403 must not reuse the upstream request and its credentials""" + + class _Upstream403(Exception): + status_code = 403 + message = "forbidden" + response = None + request = httpx.Request( + "POST", + "https://upstream.example/v1/chat?key=query-secret", + headers={"Authorization": "Bearer sk-upstream-secret"}, + content=b'{"api_key": "body-secret"}', + ) + + with pytest.raises(litellm.PermissionDeniedError) as excinfo: + exception_type(model="some-model", original_exception=_Upstream403("forbidden"), custom_llm_provider=provider) + + stub_request: Final = excinfo.value.response.request + assert str(stub_request.url) == "https://litellm.ai" + assert "authorization" not in stub_request.headers + assert b"body-secret" not in stub_request.content