fix(exception_mapping): guard validation echoes across mappers

This commit is contained in:
dlowzzxx 2026-09-02 12:32:03 +02:00
parent 9c62625130
commit 746ca296f3
2 changed files with 60 additions and 12 deletions

View file

@ -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}",

View file

@ -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(