From 746ca296f3d148dab5c1472ad989d2e320a4634b Mon Sep 17 00:00:00 2001 From: dlowzzxx Date: Wed, 2 Sep 2026 12:32:03 +0200 Subject: [PATCH] fix(exception_mapping): guard validation echoes across mappers --- .../exception_mapping_utils.py | 37 +++++++++++++------ .../test_exception_mapping_utils.py | 35 +++++++++++++++++- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 1625aa5700b..6729762c7cc 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -33,6 +33,7 @@ _VALIDATION_STRUCTURED_MARKERS: Final = frozenset( "invalid_argument", "invalid_request", "invalid_request_error", + "validation", "validation_error", "validation_exception", } @@ -137,7 +138,8 @@ class ExceptionCheckers: status_code: The HTTP status the provider returned, when known. Gates only the bare-number branch: providers echo the request back in validation errors and 429 is an ordinary token id, so an echoed prompt can put a standalone 429 in - the body of a 400. The phrase branches stay ungated (#11455). + the body of a 400. Phrase branches stay ungated (#11455) when no structured + validation signal is present. response: The provider response, when available, for structured error fields. error_body: The parsed provider error body, when available. @@ -406,7 +408,10 @@ def _map_openai_exception( exception_provider = custom_llm_provider[0].upper() + custom_llm_provider[1:] + "Exception" if ExceptionCheckers.is_error_str_rate_limit( - error_str, status_code=getattr(original_exception, "status_code", None) + error_str, + status_code=getattr(original_exception, "status_code", None), + response=getattr(original_exception, "response", None), + error_body=getattr(original_exception, "body", None), ): raise RateLimitError( message=f"RateLimitError: {exception_provider} - {message}", @@ -1345,17 +1350,24 @@ def _map_vertex_exception( ), ) elif ( - "429 Quota exceeded" in error_str - or "Quota exceeded for" in error_str - or "Resource exhausted" in error_str - or "IndexError: list index out of range" in error_str - or "429 Unable to submit request because the service is temporarily out of capacity." in error_str - or ExceptionCheckers.is_error_str_rate_limit( - error_str, - status_code=getattr(original_exception, "status_code", None), + not _structured_validation_signal( + error_str=error_str, response=getattr(original_exception, "response", None), error_body=getattr(original_exception, "body", None), ) + and ( + "429 Quota exceeded" in error_str + or "Quota exceeded for" in error_str + or "Resource exhausted" in error_str + or "IndexError: list index out of range" in error_str + or "429 Unable to submit request because the service is temporarily out of capacity." in error_str + or ExceptionCheckers.is_error_str_rate_limit( + error_str, + status_code=getattr(original_exception, "status_code", None), + response=getattr(original_exception, "response", None), + error_body=getattr(original_exception, "body", None), + ) + ) ): raise RateLimitError( message=f"litellm.RateLimitError: {custom_llm_provider}Exception - {error_str}", @@ -1878,7 +1890,10 @@ def _map_together_ai_exception( llm_provider="together_ai", ) elif ExceptionCheckers.is_error_str_rate_limit( - error_str, status_code=getattr(original_exception, "status_code", None) + error_str, + status_code=getattr(original_exception, "status_code", None), + response=getattr(original_exception, "response", None), + error_body=getattr(original_exception, "body", None), ): raise RateLimitError( message=f"TogetherAIException - {error_str}", diff --git a/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py b/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py index 0972a494110..5b2102f290c 100644 --- a/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py @@ -166,6 +166,24 @@ class TestExceptionCheckers: is True ) + @pytest.mark.parametrize("response_text", ["not-json", '["invalid_request_error"]']) + def test_rate_limit_classifier_falls_back_when_response_is_not_an_object(self, response_text): + """Unusable response JSON must not disable the legacy phrase fallback.""" + + response = httpx.Response( + status_code=400, + request=httpx.Request("POST", "https://example.invalid"), + text=response_text, + ) + assert ( + ExceptionCheckers.is_error_str_rate_limit( + "Provider validation failed: too many requests", + status_code=400, + response=response, + ) + is True + ) + def test_rate_limit_provider_specific_phrases(self): """Test that common vendor rate-limit and quota exhaustion phrases are properly recognized.""" vendor_phrases: Final = ( @@ -1156,7 +1174,7 @@ def test_structured_provider_rate_limit_reaches_dispatch_boundary( ), ( "vertex_ai", - '{"error": {"code": 400, "status": "INVALID_ARGUMENT", "message": "Invalid value: too many requests"}}', + '{"error": {"code": 400, "status": "INVALID_ARGUMENT", "message": "Invalid request: Quota exceeded for the echoed field"}}', None, ), ( @@ -1174,6 +1192,21 @@ def test_structured_provider_rate_limit_reaches_dispatch_boundary( '{"type": "error", "error": {"type": "invalid_request_error", "message": "Invalid request: too many requests"}}', None, ), + ( + "openai", + "{'error': {'type': 'invalid_request_error', 'message': 'Invalid request: too many requests'}}", + { + "error": { + "type": "invalid_request_error", + "message": "Invalid request: too many requests", + } + }, + ), + ( + "together_ai", + "{'error': {'message': 'Invalid request: too many requests'}, 'error_type': 'validation'}", + {"error_type": "validation", "error": "Invalid request: too many requests"}, + ), ], ) def test_validation_echo_does_not_reach_rate_limit_boundary(