Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Fix: Add Gemini context window exception mapping (#17751)

Krish Dholakia9fa6c516782e
diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py
index 97f62fed81a..7bf95ca3404 100644
--- a/litellm/litellm_core_utils/exception_mapping_utils.py
+++ b/litellm/litellm_core_utils/exception_mapping_utils.py
@@ -78,6 +78,9 @@ class ExceptionCheckers:
"is longer than the model's context length",
"input tokens exceed the configured limit",
"`inputs` tokens + `max_new_tokens` must be",
+ # Gemini pattern: "The input token count exceeds the maximum number of tokens allowed"
+ # See: https://github.com/BerriAI/litellm/issues/XXXX
+ "input token count exceeds the maximum number of tokens allowed",
]
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 8852e9d5ac6..f69b9c35236 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
@@ -40,6 +40,16 @@ context_window_test_cases = [
"`inputs` tokens + `max_new_tokens` must be <= 4096",
True,
),
+ # Gemini context window error format
+ # See: https://github.com/BerriAI/litellm/issues/XXXX
+ (
+ "The input token count exceeds the maximum number of tokens allowed 1048576.",
+ True,
+ ),
+ (
+ "GeminiException BadRequestError - {\n \"error\": {\n \"code\": 400,\n \"message\": \"The input token count exceeds the maximum number of tokens allowed 1048576.\",\n \"status\": \"INVALID_ARGUMENT\"\n }\n}\n",
+ True,
+ ),
# Test case insensitivity
("ERROR: THIS MODEL'S MAXIMUM CONTEXT LENGTH IS 1024.", True),
# Cerebras context window error format