Fix Cerebras context window errors not recognized (#17587)

Add detection for Cerebras's context window exceeded error format:
"Current length is X while limit is Y"

This ensures LiteLLM raises ContextWindowExceededError instead of
generic BadRequestError when Cerebras API calls exceed the model's
context limit, enabling downstream libraries like DSPy to properly
catch and handle these errors for automatic context management.
This commit is contained in:
Earl St Sauver
2025-12-08 19:02:06 -08:00
committed by GitHub
parent a7ad8a36a4
commit ad9c69860e
2 changed files with 18 additions and 0 deletions
@@ -82,6 +82,14 @@ class ExceptionCheckers:
for substring in known_exception_substrings:
if substring in _error_str_lowercase:
return True
# Cerebras pattern: "Current length is X while limit is Y"
if (
"current length is" in _error_str_lowercase
and "while limit is" in _error_str_lowercase
):
return True
return False
@staticmethod
@@ -42,6 +42,16 @@ context_window_test_cases = [
),
# Test case insensitivity
("ERROR: THIS MODEL'S MAXIMUM CONTEXT LENGTH IS 1024.", True),
# Cerebras context window error format
# See: https://github.com/BerriAI/litellm/issues/XXXX
(
"Current length is 132784 while limit is 131000",
True,
),
(
"CerebrasException - Please reduce the length of the messages or completion. Current length is 50000 while limit is 40000",
True,
),
# Negative cases (should return False)
("A generic API error occurred.", False),
("Invalid API Key provided.", False),