From 45d2e65962ced1a9c675bf437617ad291199b9e4 Mon Sep 17 00:00:00 2001 From: Gerard Cardoso Date: Thu, 23 Oct 2025 11:07:25 +0100 Subject: [PATCH] Add new string for context window exceeded error --- .../exception_mapping_utils.py | 7 +++--- .../test_exception_mapping_utils.py | 23 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index c6d3637ffcb6..767090f6e91f 100644 --- a/litellm/litellm_core_utils/exception_mapping_utils.py +++ b/litellm/litellm_core_utils/exception_mapping_utils.py @@ -43,16 +43,16 @@ def is_error_str_rate_limit(error_str: str) -> bool: """ if not isinstance(error_str, str): return False - + if "429" in error_str or "rate limit" in error_str.lower(): return True - + ####################################### # Mistral API returns this error string ######################################### if "service tier capacity exceeded" in error_str.lower(): return True - + return False @staticmethod @@ -67,6 +67,7 @@ def is_error_str_context_window_exceeded(error_str: str) -> bool: "string too long. expected a string with maximum length", "model's maximum context limit", "is longer than the model's context length", + "input length and `max_tokens` exceed context limit", ] 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 42e65612a96e..0e2738c24043 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 @@ -6,26 +6,35 @@ # Tuple format: (error_message, expected_result) context_window_test_cases = [ # Positive cases (should return True) - ("An error occurred: The input exceeds the model's maximum context limit of 8192 tokens.", True), - ("Some text before, this model's maximum context length is 4096 tokens. Some text after.", True), - ("Validation Error: string too long. expected a string with maximum length 1000.", True), + ( + "An error occurred: The input exceeds the model's maximum context limit of 8192 tokens.", + True, + ), + ( + "Some text before, this model's maximum context length is 4096 tokens. Some text after.", + True, + ), + ( + "Validation Error: string too long. expected a string with maximum length 1000.", + True, + ), ("Your prompt is longer than the model's context length of 2048.", True), ("AWS Bedrock Error: The request payload size has exceed context limit.", True), - + ("input length and `max_tokens` exceed context limit", True), # Test case insensitivity ("ERROR: THIS MODEL'S MAXIMUM CONTEXT LENGTH IS 1024.", True), - # Negative cases (should return False) ("A generic API error occurred.", False), ("Invalid API Key provided.", False), ("Rate limit reached for requests.", False), ("The context is large, but acceptable.", False), - ("", False), # Empty string + ("", False), # Empty string ] + @pytest.mark.parametrize("error_str, expected", context_window_test_cases) def test_is_error_str_context_window_exceeded(error_str, expected): """ Tests the is_error_str_context_window_exceeded function with various error strings. """ - assert ExceptionCheckers.is_error_str_context_window_exceeded(error_str) == expected \ No newline at end of file + assert ExceptionCheckers.is_error_str_context_window_exceeded(error_str) == expected