fix: improve context window error detection across providers

Add additional error message patterns to is_error_str_context_window_exceeded()
so that context limit errors from Mistral, Perplexity, Bedrock, Replicate, and
Anthropic are correctly mapped to ContextWindowExceededError instead of raising
a generic BadRequestError.

New patterns:
- "too large for model" (Mistral)
- "length of all messages is too long" (Perplexity)
- "too many tokens" / "too many input tokens" (Bedrock)
- "input is too long" (Replicate, Bedrock)
- "prompt is too long" (Anthropic, Bedrock)

Fixes #21558
This commit is contained in:
hzt 2026-02-20 12:38:50 +08:00
parent 37c98f8325
commit 29b5cc8fbb
2 changed files with 34 additions and 0 deletions

View file

@ -84,6 +84,12 @@ class ExceptionCheckers:
"input tokens exceed the configured limit",
"`inputs` tokens + `max_new_tokens` must be",
"exceeds the maximum number of tokens allowed", # Gemini
"too large for model", # Mistral
"length of all messages is too long", # Perplexity
"too many tokens", # Bedrock
"too many input tokens", # Bedrock
"input is too long", # Replicate, Bedrock
"prompt is too long", # Anthropic, Bedrock
]
for substring in known_exception_substrings:
if substring in _error_str_lowercase:

View file

@ -71,6 +71,34 @@ context_window_test_cases = [
"CerebrasException - Please reduce the length of the messages or completion. Current length is 50000 while limit is 40000",
True,
),
# Mistral context window error (issue #21558)
(
'{"object":"error","message":"Prompt contains 1487700 tokens and 0 draft tokens, too large for model with 131072 maximum context length","type":"invalid_request_invalid_args","param":null,"code":"3051"}',
True,
),
# Perplexity context window error (issue #21558)
(
"The total length of all messages is too long.",
True,
),
# Bedrock context window errors
(
"BedrockException: Context Window Error - too many tokens",
True,
),
(
"Too many input tokens. Max input tokens: 200000, request input token count: 250000",
True,
),
(
"Input is too long. Max input length is 100000 tokens.",
True,
),
# Anthropic/Bedrock prompt too long
(
"prompt is too long: 150000 tokens > 100000 maximum",
True,
),
# Negative cases (should return False)
("A generic API error occurred.", False),
("Invalid API Key provided.", False),