From 82b3222a52df8f738de0c12c95c7da7ac7e1990a Mon Sep 17 00:00:00 2001 From: Matthew Zhou Date: Wed, 4 Jun 2025 12:57:51 -0700 Subject: [PATCH] fix: Make OpenAI context window exceeded error more specific (#2624) --- letta/agents/letta_agent.py | 4 ++-- letta/errors.py | 4 ---- letta/llm_api/openai_client.py | 10 ++++------ 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/letta/agents/letta_agent.py b/letta/agents/letta_agent.py index 31f4e3dc..547e349f 100644 --- a/letta/agents/letta_agent.py +++ b/letta/agents/letta_agent.py @@ -14,7 +14,7 @@ from letta.agents.helpers import ( _prepare_in_context_messages_no_persist_async, generate_step_id, ) -from letta.errors import LLMContextWindowExceededError +from letta.errors import ContextWindowExceededError from letta.helpers import ToolRulesSolver from letta.helpers.datetime_helpers import get_utc_timestamp_ns from letta.helpers.tool_execution_helper import enable_strict_mode @@ -692,7 +692,7 @@ class LettaAgent(BaseAgent): llm_config: LLMConfig, force: bool, ) -> List[Message]: - if isinstance(e, LLMContextWindowExceededError): + if isinstance(e, ContextWindowExceededError): return await self._rebuild_context_window( in_context_messages=in_context_messages, new_letta_messages=new_letta_messages, llm_config=llm_config, force=force ) diff --git a/letta/errors.py b/letta/errors.py index de00071c..17427ea6 100644 --- a/letta/errors.py +++ b/letta/errors.py @@ -88,10 +88,6 @@ class LLMPermissionDeniedError(LLMError): """Error when permission is denied by LLM service""" -class LLMContextWindowExceededError(LLMError): - """Error when the context length is exceeded.""" - - class LLMNotFoundError(LLMError): """Error when requested resource is not found""" diff --git a/letta/llm_api/openai_client.py b/letta/llm_api/openai_client.py index ed77410a..7be16835 100644 --- a/letta/llm_api/openai_client.py +++ b/letta/llm_api/openai_client.py @@ -8,11 +8,11 @@ from openai.types.chat.chat_completion_chunk import ChatCompletionChunk from letta.constants import LETTA_MODEL_ENDPOINT from letta.errors import ( + ContextWindowExceededError, ErrorCode, LLMAuthenticationError, LLMBadRequestError, LLMConnectionError, - LLMContextWindowExceededError, LLMNotFoundError, LLMPermissionDeniedError, LLMRateLimitError, @@ -342,11 +342,9 @@ class OpenAIClient(LLMClientBase): # Check message content if finer-grained errors are needed # Example: if "context_length_exceeded" in str(e): return LLMContextLengthExceededError(...) # TODO: This is a super soft check. Not sure if we can do better, needs more investigation. - if "context" in str(e): - return LLMContextWindowExceededError( - message=f"Bad request to OpenAI (context length exceeded): {str(e)}", - code=ErrorCode.INVALID_ARGUMENT, # Or more specific if detectable - details=e.body, + if "This model's maximum context length is" in str(e): + return ContextWindowExceededError( + message=f"Bad request to OpenAI (context window exceeded): {str(e)}", ) else: return LLMBadRequestError(