Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: Make OpenAI context window exceeded error more specific (#2624)
Matthew Zhou3ccd79efa5dd
diff --git a/letta/agents/letta_agent.py b/letta/agents/letta_agent.pyindex 31f4e3dc3..547e349f5 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 ContextWindowExceededErrorfrom letta.helpers import ToolRulesSolverfrom letta.helpers.datetime_helpers import get_utc_timestamp_nsfrom 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.pyindex de00071cf..17427ea6e 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.pyindex ed77410a3..7be168352 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 ChatCompletionChunkfrom letta.constants import LETTA_MODEL_ENDPOINTfrom 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(