mirror of
https://github.com/tiennm99/litellm.git
synced 2026-08-16 20:26:25 +00:00
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:
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user