fix: map Azure context_length_exceeded to ContextWindowExceededError

Azure Responses API returns a different error message format
('Your input exceeds the context window') than Chat Completions
('This model'\''s maximum context length is'). The existing check
only matched the latter, so Responses API context window errors
fell through to the generic 'invalid_request_error' handler and
were raised as BadRequestError.

Add checks for:
- azure_error_code == 'context_length_exceeded' (structured body)
- 'input exceeds the context window' (message string)
This commit is contained in:
Brian Caswell 2026-03-03 19:12:03 +00:00
parent 4c1b15d685
commit 4f5222abfd
2 changed files with 37 additions and 2 deletions

View file

@ -2115,7 +2115,11 @@ def exception_type( # type: ignore # noqa: PLR0915
litellm_debug_info=extra_information,
response=getattr(original_exception, "response", None),
)
elif "This model's maximum context length is" in error_str:
elif (
"This model's maximum context length is" in error_str
or azure_error_code == "context_length_exceeded"
or "input exceeds the context window" in error_str
):
exception_mapping_worked = True
raise ContextWindowExceededError(
message=f"AzureException ContextWindowExceededError - {message}",

View file

@ -10,7 +10,7 @@ sys.path.insert(
) # Adds the parent directory to the system path
import litellm
from litellm.exceptions import ContentPolicyViolationError
from litellm.exceptions import ContentPolicyViolationError, ContextWindowExceededError
from litellm.litellm_core_utils.exception_mapping_utils import exception_type
@ -419,6 +419,37 @@ class TestAzureExceptionMapping:
assert "optional_pre_call_checks" in error.message
assert "docs.litellm.ai" in error.message
def test_azure_context_length_exceeded_via_error_code(self):
"""Azure Responses API returns context_length_exceeded in the error code
with a different message format than the chat completions API. This
should still be mapped to ContextWindowExceededError."""
mock_exception = Exception(
"Your input exceeds the context window of this model. "
"Please adjust your input and try again."
)
mock_exception.body = {
"error": {
"message": (
"Your input exceeds the context window of this model. "
"Please adjust your input and try again."
),
"type": "invalid_request_error",
"param": "input",
"code": "context_length_exceeded",
}
}
mock_response = MagicMock()
mock_response.status_code = 400
mock_exception.response = mock_response
with pytest.raises(ContextWindowExceededError):
exception_type(
model="azure/gpt-4o",
original_exception=mock_exception,
custom_llm_provider="azure",
)
def test_openai_invalid_encrypted_content_error(self):
"""Test that OpenAI invalid_encrypted_content errors also get helpful guidance."""
from litellm.exceptions import BadRequestError