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.
This commit is contained in:
mateo-berri 2026-10-28 20:49:59 -07:00
parent 8cfb20122e
commit 66a11a6287
2 changed files with 25 additions and 3 deletions

View file

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

View file

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