From ad9c69860eaa903e279800d9d7cee20ccdede58b Mon Sep 17 00:00:00 2001 From: Earl St Sauver Date: Tue, 9 Dec 2025 04:02:06 +0100 Subject: [PATCH] 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. --- litellm/litellm_core_utils/exception_mapping_utils.py | 8 ++++++++ .../litellm_core_utils/test_exception_mapping_utils.py | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 056522e2a6..97f62fed81 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -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 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 d59e52d965..8852e9d5ac 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 @@ -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),