From 29b5cc8fbbabc81336479cad3382c7b506c5dc31 Mon Sep 17 00:00:00 2001 From: hzt <3061613175@qq.com> Date: Fri, 20 Feb 2026 12:38:50 +0800 Subject: [PATCH] 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 --- .../exception_mapping_utils.py | 6 ++++ .../test_exception_mapping_utils.py | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index dde44cced36..78af4f2bd89 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -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: diff --git a/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py b/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py index beb978584cb..c286b5d15fb 100644 --- a/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py +++ b/tests/test_litellm/litellm_core_utils/test_exception_mapping_utils.py @@ -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),