Skip to content

[BUG] classify_openai_error() doesn't recognize Bedrock Mantle's context-overflow message, so ContextWindowOverflowException / reduce_context() never fires #3718

Description

@bsolomon1124

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

1.50.2

Language Runtime Version

Python 3.14.7

Operating System

MacOS 26.5.2

Installation Method

pip

Steps to Reproduce

Using Bedrock Mantle's OpenAI-compatible endpoint (bedrock_mantle_config on OpenAIResponsesModel), send a
prompt that exceeds the model's real context window for openai.gpt-5.6-terra (confirmed live limit:
1,050,000 tokens).

Minimal reproduction:

from strands import Agent
from strands.models import BedrockMantleConfig
from strands.models.openai_responses import OpenAIResponsesModel
import boto3

model = OpenAIResponsesModel(
    model_id="openai.gpt-5.6-terra",
    bedrock_mantle_config=BedrockMantleConfig(
        boto_session=boto3.Session(), region="us-east-1"
    ),
    params={"max_output_tokens": 16},
)
agent = Agent(model=model, tools=[], callback_handler=None)

# ~8.6M chars / ~1.2M tokens -- well over Terra's 1,050,000-token window
big_input = "quantum entangled photon lattice resonance " * 200_000
agent(big_input)
  1. Run the snippet above against a live Bedrock Mantle endpoint with valid AWS credentials.
  2. Mantle returns HTTP 400 with body:
    {"error": {"code": "validation_error", "message": "prompt tokens (1200008) exceed model maximum (1050000) for openai.gpt-5.6-terra", "param": null, "type": "invalid_request_error"}}
  3. Observe the exception that propagates out of agent(...).

Root cause, confirmed by reading the installed package
(strands/models/_openai_errors.py):

_CONTEXT_WINDOW_OVERFLOW_PATTERNS = (
    "maximum context length",
    "context_length_exceeded",
    "too many tokens",
    "context length",
    "input is too long for requested model",
    "input length and `max_tokens` exceed context limit",
    "too many total text bytes",
    "exceed customer model maximum",   # <-- has "customer"
    "exceeds the max_model_len",
)

Mantle's real message is "...exceed model maximum..." — no "customer" in it, and its error code is
"validation_error", not "context_length_exceeded". No pattern in the list is a substring of the real
message, so classify_openai_error() returns None for every genuine Terra context overflow.

In openai_responses.py's stream():

except (openai.APIError, _OpenAIResponsesStreamError) as error:
    error_kind = classify_openai_error(error)
    if error_kind == "throttling":
        ...
    if error_kind == "context_overflow":
        raise ContextWindowOverflowException(str(error)) from error
    raise  # <-- this is what actually happens for Terra

Since error_kind is None, the original error is re-raised unclassified instead of being wrapped in
ContextWindowOverflowException.

Agent._execute_event_loop_cycle's only auto-recovery path is:

except ContextWindowOverflowException as e:
    self.conversation_manager.reduce_context(self, e=e)
    ...  # retry

Because the exception is never ContextWindowOverflowException, reduce_context() (and the whole
retry-after-trim flow) never runs — even though agent.conversation_manager is correctly a
SlidingWindowConversationManager (the default; we never override it). The conversation-manager wiring
is correct and present; only the error classification is missing the Mantle-specific string.

Expected Behavior

A prompt that exceeds a Bedrock Mantle model's real context window should be classified as
"context_overflow" by classify_openai_error(), causing ContextWindowOverflowException to be raised,
which triggers conversation_manager.reduce_context() and an automatic retry with a trimmed conversation
— the same recovery behavior that already works correctly for providers whose error text matches an
existing pattern (e.g. plain OpenAI's "maximum context length").

Actual Behavior

The raw, unclassified error (_OpenAIResponsesStreamError / openai.BadRequestError, message
"prompt tokens (N) exceed model maximum (M) for <model_id>") propagates straight out of agent(...).
No ContextWindowOverflowException is raised, reduce_context() never runs, and the turn hard-fails with
no auto-trim-and-retry — even though a working SlidingWindowConversationManager is attached to the
agent and would recover correctly if the exception were classified.

In our application, this is caught by a broad top-level exception handler, so the end user sees a generic
error rather than a crash — but the intended graceful-degradation behavior (trim context, retry) never
engages, and the failure is indistinguishable from any other unrelated error.

Additional Context

  • Confirmed live against Bedrock Mantle in us-east-1 on 2026-08-07 (Mantle's advertised context window
    for openai.gpt-5.6-terra is 1,050,000 tokens as of the 2026-08-03 long-context expansion).
  • Verified the installed package's _CONTEXT_WINDOW_OVERFLOW_PATTERNS list directly
    (site-packages/strands/models/_openai_errors.py) matches the current main branch of
    strands-agents/harness-sdk — this is not fixed on a newer unreleased version as of filing.
  • Also reproduced the same gap via client.responses.create() directly against Mantle with the plain
    openai Python SDK (bypassing Strands entirely) to confirm the message text originates from Mantle
    itself, not from any Strands-side wrapping.
  • We reach OpenAIResponsesModel via Bedrock Mantle's bedrock_mantle_config (not plain OpenAI), so this
    is specifically about Mantle's error phrasing, not a general OpenAI regression.
  • The identical gap likely also affects OpenAIModel's Chat-Completions-based stream()/structured_output() for
    non-gpt-5.* Mantle models, since both call the same shared classify_openai_error().

Possible Solution

Add Mantle's exact phrasing to _CONTEXT_WINDOW_OVERFLOW_PATTERNS in
strands/models/_openai_errors.py, e.g.:

_CONTEXT_WINDOW_OVERFLOW_PATTERNS = (
    ...,
    "exceed customer model maximum",
    "exceed model maximum",  # Bedrock Mantle's phrasing (no "customer")
    ...,
)

Since "exceed customer model maximum" is itself a superset-match risk, a more robust long-term fix might
be a single regex like r"exceed(?:s|ed)?(?: customer)? model maximum" or matching on "model maximum"
alone, to absorb minor future wording variants from any OpenAI-compatible provider without needing another
patch release per provider.

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-contextSession or context relatedarea-modelRelated to models or model providersbugSomething isn't workingpythonPull requests that update python code

    Type

    Fields

    Language

    Python

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions