fix(bedrock_mantle): map gpt-oss context overflow to ContextWindowExceededError

Bedrock Mantle's gpt-oss models report a too-long prompt as "Input length (N)
exceeds model's maximum context length (M)", which the Mantle mapping missed, so
callers got a BadRequestError (or a 503 when streaming), context window fallbacks
never fired, and Claude Code never compacted. Match that wording too and return
the existing "prompt is too long: N tokens > M maximum" message.
This commit is contained in:
mateo-berri 2026-10-28 20:04:48 -07:00
parent d9c9d84298
commit 8cfb20122e
2 changed files with 57 additions and 6 deletions

View file

@ -892,7 +892,10 @@ def _map_openai_like_exception(
)
_BEDROCK_MANTLE_CONTEXT_WINDOW_PATTERN: Final = re.compile(r"prompt tokens \((\d+)\) exceed model maximum \((\d+)\)")
_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+)\)"),
)
_BEDROCK_MANTLE_CONTEXT_LENGTH_EXCEEDED_CODE_PATTERN: Final = re.compile(
r"""\\?["']code\\?["']\s*:\s*\\?["']context_length_exceeded\\?["']"""
)
@ -912,12 +915,13 @@ 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, 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).
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).
"""
match = _BEDROCK_MANTLE_CONTEXT_WINDOW_PATTERN.search(error_str)
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)
if match is not None:
prompt_tokens, max_tokens = match.groups()
return f"prompt is too long: {prompt_tokens} tokens > {max_tokens} maximum"

View file

@ -1491,6 +1491,53 @@ def test_bedrock_mantle_context_overflow_maps_to_context_window_exceeded():
assert "prompt is too long: 1055489 tokens > 1050000 maximum" in excinfo.value.message
_GPT_OSS_OVERFLOW_EVENT: Final = (
'ErrorEvent { error: APIError { type: "BadRequestError", code: Some(400), '
"message: \"Input length (360068) exceeds model's maximum context length (131072).\", param: None } }"
)
@pytest.mark.parametrize(
"original_exception",
[
pytest.param(
BaseLLMException(
status_code=400,
message=json.dumps(
{
"error": {
"code": "validation_error",
"message": _GPT_OSS_OVERFLOW_EVENT,
"param": None,
"type": "invalid_request_error",
}
}
),
),
id="400-body",
),
pytest.param(OpenAIError(status_code=503, message=_GPT_OSS_OVERFLOW_EVENT), id="streamed-event"),
pytest.param(
BaseLLMException(
status_code=400, message=f"Error code: 400 - {repr({'error': {'message': _GPT_OSS_OVERFLOW_EVENT}})}"
),
id="python-repr",
),
],
)
def test_bedrock_mantle_gpt_oss_input_length_overflow_maps_to_context_window_exceeded(original_exception):
with pytest.raises(litellm.ContextWindowExceededError) as excinfo:
exception_type(
model="openai.gpt-oss-20b",
original_exception=original_exception,
custom_llm_provider="bedrock_mantle",
)
assert excinfo.value.status_code == 400
assert excinfo.value.response.status_code == 400
assert "prompt is too long: 360068 tokens > 131072 maximum" in excinfo.value.message
def test_bedrock_mantle_openai_envelope_context_overflow_maps_to_context_window_exceeded():
from litellm.llms.base_llm.chat.transformation import BaseLLMException