fix: Make OpenAI context window exceeded error more specific (#2624)

This commit is contained in:
Matthew Zhou
2025-06-04 12:57:51 -07:00
committed by GitHub
parent bd2b7dc6d4
commit 82b3222a52
3 changed files with 6 additions and 12 deletions

View File

@@ -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
)

View File

@@ -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"""

View File

@@ -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(