fix: Make OpenAI context window exceeded error more specific (#2624)
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
@@ -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"""
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user