From dbb5d725ceb7a6f87905bf833b6aca45abd176a2 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 22 Apr 2025 14:18:16 +0000 Subject: [PATCH] Fix #2659: Add litellm ContextWindowExceededError detection Co-Authored-By: Joe Moura --- .../context_window_exceeding_exception.py | 1 + ...test_context_window_exceeding_exception.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/utilities/exceptions/test_context_window_exceeding_exception.py diff --git a/src/crewai/utilities/exceptions/context_window_exceeding_exception.py b/src/crewai/utilities/exceptions/context_window_exceeding_exception.py index 399cf5a00..9207ba4b2 100644 --- a/src/crewai/utilities/exceptions/context_window_exceeding_exception.py +++ b/src/crewai/utilities/exceptions/context_window_exceeding_exception.py @@ -8,6 +8,7 @@ class LLMContextLengthExceededException(Exception): "too many tokens", "input is too long", "exceeds token limit", + "ContextWindowExceededError", ] def __init__(self, error_message: str): diff --git a/tests/utilities/exceptions/test_context_window_exceeding_exception.py b/tests/utilities/exceptions/test_context_window_exceeding_exception.py new file mode 100644 index 000000000..27a9dfd9f --- /dev/null +++ b/tests/utilities/exceptions/test_context_window_exceeding_exception.py @@ -0,0 +1,21 @@ +import pytest + +from crewai.utilities.exceptions.context_window_exceeding_exception import ( + LLMContextLengthExceededException, +) + + +def test_context_window_error_detection(): + """Test detection of different context window error formats.""" + assert LLMContextLengthExceededException("maximum context length exceeded")._is_context_limit_error( + "maximum context length exceeded" + ) + assert LLMContextLengthExceededException("expected a string with maximum length")._is_context_limit_error( + "expected a string with maximum length" + ) + + litellm_error = "litellm.ContextWindowExceededError: litellm.BadRequestError: ContextWindowExceededError: MistralException - Error code: 400 - {'object': 'error', 'message': \"This model's maximum context lenght is 15000 tokens. However, you requested 15018 tokens (12970) in the messages, 2048 in the completion). Please reduce the length of the messages or completion.\", type: 'BadRequestError', 'param': None, 'code': 400}" + + assert LLMContextLengthExceededException(litellm_error)._is_context_limit_error( + litellm_error + )