From 66a11a6287f6c63bca90f9de82e2905824deeda2 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:49:59 -0700 Subject: [PATCH] fix(bedrock_mantle): map the gpt-oss Responses API overflow wording too Mantle's /v1/responses words gpt-oss overflow as "The engine prompt length N exceeds the max_model_len M", so a streamed /v1/responses overflow still ended in a response.failed carrying a plain BadRequestError. Match that wording as well. --- .../exception_mapping_utils.py | 7 ++++--- .../test_exception_mapping_utils.py | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index c37db350c6d..6aac1918993 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -895,6 +895,7 @@ def _map_openai_like_exception( _BEDROCK_MANTLE_CONTEXT_WINDOW_PATTERNS: Final = ( re.compile(r"prompt tokens \((\d+)\) exceed model maximum \((\d+)\)"), re.compile(r"Input length \((\d+)\) exceeds model\\?'s maximum context length \((\d+)\)"), + re.compile(r"The engine prompt length (\d+) exceeds the max_model_len (\d+)"), ) _BEDROCK_MANTLE_CONTEXT_LENGTH_EXCEEDED_CODE_PATTERN: Final = re.compile( r"""\\?["']code\\?["']\s*:\s*\\?["']context_length_exceeded\\?["']""" @@ -916,9 +917,9 @@ def _bedrock_mantle_context_window_response(original_exception: _ProviderHTTPExc def _get_bedrock_mantle_context_window_message(error_str: str) -> str | None: """ Mantle reports context overflow as a validation_error carrying the token counts (worded - differently for gpt-oss), or as OpenAI's context_length_exceeded code, either in a 400 body - or in a streamed error event with no error type. Clients such as Claude Code only treat - "prompt is too long" as overflow (litellm/litellm#36546). + differently for gpt-oss, and again on its Responses API), or as OpenAI's context_length_exceeded + code, either in a 400 body or in a streamed error event with no error type. Clients such as + Claude Code only treat "prompt is too long" as overflow (litellm/litellm#36546). """ matches: Final = (pattern.search(error_str) for pattern in _BEDROCK_MANTLE_CONTEXT_WINDOW_PATTERNS) match: Final = next((found for found in matches if found is not None), None) 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 1a96a090f3b..4ac7c644cda 100644 --- a/tests/unit/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/unit/litellm_core_utils/test_exception_mapping_utils.py @@ -1538,6 +1538,27 @@ def test_bedrock_mantle_gpt_oss_input_length_overflow_maps_to_context_window_exc assert "prompt is too long: 360068 tokens > 131072 maximum" in excinfo.value.message +def test_bedrock_mantle_gpt_oss_streamed_responses_overflow_event_maps_to_context_window_exceeded(): + from litellm.responses.streaming_iterator import map_stream_error_to_exception + + mapped_exception = map_stream_error_to_exception( + { + "code": "invalid_prompt", + "message": ( + 'ErrorEvent { error: APIError { type: "invalid_request_error", code: Some(400), message: ' + '"The engine prompt length 165072 exceeds the max_model_len 131072. Please reduce prompt.", ' + 'param: Some("input") } }' + ), + }, + model="openai.gpt-oss-20b", + custom_llm_provider="bedrock_mantle", + ) + + assert isinstance(mapped_exception, litellm.ContextWindowExceededError) + assert mapped_exception.status_code == 400 + assert "prompt is too long: 165072 tokens > 131072 maximum" in mapped_exception.message + + def test_bedrock_mantle_openai_envelope_context_overflow_maps_to_context_window_exceeded(): from litellm.llms.base_llm.chat.transformation import BaseLLMException