From 4f5222abfd5cdaa2035256a6685e16e354f97e88 Mon Sep 17 00:00:00 2001 From: Brian Caswell Date: Tue, 3 Mar 2026 19:12:03 +0000 Subject: [PATCH] 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) --- .../exception_mapping_utils.py | 6 +++- .../azure/test_azure_exception_mapping.py | 33 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 951485130b3..ecac5d3837b 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -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}", diff --git a/tests/test_litellm/llms/azure/test_azure_exception_mapping.py b/tests/test_litellm/llms/azure/test_azure_exception_mapping.py index 249b9349c54..6fb61e14561 100644 --- a/tests/test_litellm/llms/azure/test_azure_exception_mapping.py +++ b/tests/test_litellm/llms/azure/test_azure_exception_mapping.py @@ -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