Skip to content

fix: improve context window exceeded error detection - #21647

Closed
hztBUAA wants to merge 1 commit into
BerriAI:mainfrom
hztBUAA:fix/context-window-error-detection
Closed

fix: improve context window exceeded error detection#21647
hztBUAA wants to merge 1 commit into
BerriAI:mainfrom
hztBUAA:fix/context-window-error-detection

Conversation

@hztBUAA

@hztBUAA hztBUAA commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #21558

Context limit errors from some providers (notably Mistral and Perplexity) aren't properly mapped to ContextWindowExceededError, causing applications to receive a generic BadRequestError instead. 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, Bedrock

These 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

  • Added 7 new test cases covering the exact error messages from the issue and additional provider patterns
  • All 44 tests in test_exception_mapping_utils.py pass
  • Negative test cases (non-context-window errors) still correctly return False
  • Existing OpenAI user param length validation exclusion still works correctly

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
@vercel

vercel Bot commented Feb 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 20, 2026 4:41am

Request Review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@CLAassistant

CLAassistant commented Feb 20, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR improves error handling by adding 6 new error message patterns to the centralized is_error_str_context_window_exceeded() function. The patterns were already handled in provider-specific code paths (Bedrock, Replicate, Anthropic) but were missing from the centralized checker used by OpenAI-compatible providers.

Key Changes:

  • Added context window error patterns for Mistral ("too large for model")
  • Added Perplexity error pattern ("length of all messages is too long")
  • Added Bedrock patterns ("too many tokens", "too many input tokens", "input is too long", "prompt is too long")
  • These patterns now apply to all OpenAI-compatible providers, including Mistral and Perplexity
  • 7 new test cases added with real error message examples from issue [Bug]: Context limit error message is not always ContextWindowExceededError #21558

Impact:
This fix ensures that Mistral and Perplexity context window errors are properly mapped to ContextWindowExceededError instead of generic BadRequestError, allowing applications to programmatically distinguish context errors from other bad requests.

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The changes are minimal, well-tested, and improve error handling by centralizing existing error patterns. The PR adds only string matching patterns to an existing utility function without changing any logic flow. All 7 new test cases cover the added patterns including edge cases, and the patterns were already proven to work in provider-specific code.
  • No files require special attention

Important Files Changed

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
Loading

Last reviewed commit: 29b5cc8

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@hztBUAA

hztBUAA commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

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.

@hztBUAA

hztBUAA commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

Quick follow-up: I am reviewing the feedback and will update this PR shortly.

@github-actions

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Context limit error message is not always ContextWindowExceededError

2 participants