diff --git a/litellm/litellm_core_utils/exception_mapping_utils.py b/litellm/litellm_core_utils/exception_mapping_utils.py index 61551b042368..3eda1a2508b3 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 @@ -68,6 +68,7 @@ def is_error_str_context_window_exceeded(error_str: str) -> bool: "model's maximum context limit", "is longer than the model's context length", "input tokens exceed the configured limit", + "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 638aab8fb498..48fe1ac3cb76 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,27 +6,39 @@ # 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 tokens exceed the configured limit of 272000 tokens. Your messages resulted in 509178 tokens. Please reduce the length of the messages.", True), - + ( + "Input tokens exceed the configured limit of 272000 tokens. Your messages resulted in 509178 tokens. Please reduce the length of the messages.", + 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