Skip to content

fix(responses): map Bedrock Mantle context overflow to ContextWindowExceededError - #37862

Merged
yassin-berriai merged 1 commit into
litellm_internal_stagingfrom
litellm_bedrock_mantle_context_window
Aug 21, 2026
Merged

fix(responses): map Bedrock Mantle context overflow to ContextWindowExceededError#37862
yassin-berriai merged 1 commit into
litellm_internal_stagingfrom
litellm_bedrock_mantle_context_window

Conversation

@yassin-berriai

@yassin-berriai yassin-berriai commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Bedrock Mantle context overflow surfaces as a plain 400 client error
  • Claude Code only detects overflow via the phrase "prompt is too long"

How it solves it:

  • Detects Mantle's structured validation_error token-count message
  • Raises ContextWindowExceededError with that phrase in the message

User Flow

Before: a Claude Code user whose prompt exceeds a Bedrock Mantle model's context window gets an ordinary error with no automatic recovery

  1. They send a prompt larger than the model's context window through Claude Code to a Bedrock Mantle model
  2. Mantle rejects it with HTTP 400 and body {"error":{"code":"validation_error","message":"prompt tokens (400007) exceed model maximum (278528) for openai.gpt-5.5",...}}
  3. Claude Code does not recognize this wording as context overflow, so it does not run reactive compaction and the request just fails

After: the same overflow is recognized and triggers Claude Code's compaction path

  1. They send the same prompt
  2. Mantle rejects it the same way
  3. The client-facing error message now reads "prompt is too long: 400007 tokens > 278528 maximum"
  4. Claude Code recognizes the phrase and runs reactive compaction instead of surfacing a dead-end error

Relevant issues

Follow-up to #36580 (comment #36580 (comment))

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

All runs below are live calls against the real bedrock-mantle.us-east-1.api.aws endpoint with real AWS SigV4 credentials, no mocks

Setup: litellm.responses(model="bedrock_mantle/openai.gpt-5.5", input=..., aws_region_name="us-east-1")

Before (c166225)

Context overflow

  1. Send input built from 400007 tokens worth of text (model maximum is 278528)
  2. Mantle returns HTTP 400 with body {"error":{"code":"validation_error","message":"prompt tokens (400007) exceed model maximum (278528) for openai.gpt-5.5","param":null,"type":"invalid_request_error"}}
  3. LiteLLM raises litellm.BadRequestError: BedrockException - {"error":{"code":"validation_error","message":"prompt tokens (400007) exceed model maximum (278528) for openai.gpt-5.5",...}}, an ordinary 400 with no "prompt is too long" phrase

Unrelated validation error (must stay unaffected)

  1. Send a malformed input item: [{"type": "not_a_real_type", "text": "hi"}]
  2. Mantle returns HTTP 400 with body {"error":{"code":"validation_error","message":"invalid request body: Invalid 'input': value did not match any expected variant","param":null,"type":"invalid_request_error"}}
  3. LiteLLM raises litellm.BadRequestError: BedrockException - {"error":{"code":"validation_error","message":"invalid request body: Invalid 'input': ...

After (ec59c95, unchanged at current head fe8e2ee — a pure rebase onto staging for an unrelated lint fix, no code delta)

Context overflow

  1. Send the same input built from 400007 tokens worth of text
  2. Mantle returns the same HTTP 400 body
  3. LiteLLM now raises litellm.ContextWindowExceededError: litellm.BadRequestError: prompt is too long: 400007 tokens > 278528 maximum, status code 400

Unrelated validation error (must stay unaffected)

  1. Send the same malformed input item
  2. Mantle returns the same HTTP 400 body
  3. LiteLLM still raises the ordinary litellm.BadRequestError: BedrockException - {"error":{"code":"validation_error","message":"invalid request body: Invalid 'input': ..., unchanged from Before

Targeted regression tests also passed:

uv run --no-sync pytest tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py tests/test_litellm/llms/bedrock_mantle/ -q -> 232 passed

Type

Bug Fix

Caveats (if any)

  • The token counts in the reproduction (400007 / 278528) come from openai.gpt-5.5's real context window, chosen over the larger gpt-5.6 models to keep the live repro cheap
  • The exact wording match is scoped to Mantle's validation_error shape; a differently-worded overflow message would need its own pattern

Review notes

Greptile's one open finding (P2) asks to move the Mantle regex/parser out of exception_mapping_utils.py into the provider layer. Kept it in place: this file already holds 18 provider-specific _map_<provider>_exception functions, and _map_bedrock_exception itself already does inline Bedrock-specific substring matching (AccessDeniedException, too many tokens, and 5 more) right next to the new Mantle branch. This file is the codebase's established shared home for per-provider error classification, dispatched by custom_llm_provider; the new code follows that same shape one level down for the Mantle sub-provider that #36580 already routed through this exact function, so moving it out would fragment the pattern rather than isolate it

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR recognizes Bedrock Mantle token-limit validation responses and maps them to ContextWindowExceededError with client-compatible wording.

  • Adds narrowly scoped detection and token-count extraction for Mantle context-overflow errors.
  • Adds regression coverage for context overflow while preserving ordinary validation errors as BadRequestError.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/exception_mapping_utils.py Adds Mantle context-overflow detection and maps matching validation responses to the existing context-window exception type.
tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py Adds regression coverage for Mantle context overflow and strengthens the unrelated-validation-error type assertion.

Reviews (3): Last reviewed commit: "fix(responses): map Bedrock Mantle conte..." | Re-trigger Greptile

Comment thread litellm/litellm_core_utils/exception_mapping_utils.py
@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai kept the parsing in exception_mapping_utils.py; reasoning is in the PR body's Review notes section, at head ec59c95.

@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

The reasoning holds up. _map_bedrock_exception is already dispatched by custom_llm_provider, the function already contains multiple provider-specific branches inline, and bedrock_mantle was explicitly routed through here by #36580. Adding a sub-provider guard at the top of the same function is consistent with the existing pattern — moving it out would be the deviation, not keeping it here.

The implementation looks correct: the early check on "invalid_request_error" / "validation_error" ensures non-overflow Mantle 400s aren't misclassified, and the regex is scoped to Mantle's specific structured format. No concerns with where this lives.

@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai thanks, please re-review the current head ec59c95 so the score reflects that.

@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ellm/litellm_core_utils/exception_mapping_utils.py 92.30% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed-hq

codspeed-hq Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_bedrock_mantle_context_window (fe8e2ee) with litellm_internal_staging (0a5fa4f)

Open in CodSpeed

…xceededError

Mantle reports context overflow as a structured 400 validation_error rather than
the plain-text patterns Bedrock itself uses, so callers such as Claude Code that
key reactive compaction off the phrase "prompt is too long" never see it. Detect
the pattern and normalize the message to that phrase.
@yassin-berriai
yassin-berriai force-pushed the litellm_bedrock_mantle_context_window branch from ec59c95 to fe8e2ee Compare August 21, 2026 18:43
@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai rebased onto staging to pick up an unrelated lint fix, no code changes here. Please re-review the current head fe8e2ee.

@yassin-berriai
yassin-berriai merged commit bb99f57 into litellm_internal_staging Aug 21, 2026
70 checks passed
@yassin-berriai
yassin-berriai deleted the litellm_bedrock_mantle_context_window branch August 21, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants