fix: improve context window exceeded error detection - #21647
Conversation
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 BerriAI#21558
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Greptile SummaryThis PR improves error handling by adding 6 new error message patterns to the centralized Key Changes:
Impact: Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/exception_mapping_utils.py | Added 6 error patterns to centralized context window detection for Mistral, Perplexity, Bedrock, Replicate, and Anthropic |
| tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py | Added 7 comprehensive test cases covering new error patterns with real provider examples |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[API Error from Provider] --> B{Provider Type?}
B -->|Bedrock/Anthropic/Replicate| C[Provider-Specific Handler]
B -->|Mistral/Perplexity/OpenAI-compatible| D[Generic OpenAI Handler]
C --> E{Check Error String}
E -->|Match patterns| F[ContextWindowExceededError]
D --> G[is_error_str_context_window_exceeded]
G --> H{Match Pattern?}
H -->|New patterns added| F
H -->|No Match| I[Check Other Error Types]
F --> J[Return Typed Exception]
I --> K[Generic BadRequestError]
style G fill:#90EE90
style H fill:#FFE4B5
style F fill:#87CEEB
Last reviewed commit: 29b5cc8
|
Thanks for the review and feedback. I am following up on this PR now and will either push the requested changes or reply point-by-point shortly. |
|
Quick follow-up: I am reviewing the feedback and will update this PR shortly. |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Summary
Fixes #21558
Context limit errors from some providers (notably Mistral and Perplexity) aren't properly mapped to
ContextWindowExceededError, causing applications to receive a genericBadRequestErrorinstead. This makes it impossible to programmatically distinguish context window errors from other bad request errors.Changes
Added 6 new error message patterns to
ExceptionCheckers.is_error_str_context_window_exceeded():"too large for model"— Mistral (e.g. "Prompt contains X tokens..., too large for model with Y maximum context length")"length of all messages is too long"— Perplexity (e.g. "The total length of all messages is too long.")"too many tokens"— Bedrock"too many input tokens"— Bedrock"input is too long"— Replicate, Bedrock"prompt is too long"— Anthropic, BedrockThese patterns were already handled in provider-specific code paths (Bedrock, Replicate, Anthropic) but were missing from the centralized
is_error_str_context_window_exceeded()checker used by OpenAI-compatible providers (which includes Mistral and Perplexity).Test Plan
test_exception_mapping_utils.pypassFalse