From 6ae49ab3c56abc4210b261d192455c19b34d45da Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Tue, 2 Sep 2025 22:20:46 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20properly=20throw=20context=20window=20ex?= =?UTF-8?q?ceeded=20error=20on=20the=20new=20style=20of=E2=80=A6=20[LET-41?= =?UTF-8?q?79]=20(#4380)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: properly throw context window exceeded error on the new style of context window overflow from gpt-5 --- letta/llm_api/openai_client.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/letta/llm_api/openai_client.py b/letta/llm_api/openai_client.py index a7e4e100..21c94b0d 100644 --- a/letta/llm_api/openai_client.py +++ b/letta/llm_api/openai_client.py @@ -363,10 +363,19 @@ class OpenAIClient(LLMClientBase): if isinstance(e, openai.BadRequestError): logger.warning(f"[OpenAI] Bad request (400): {str(e)}") # BadRequestError can signify different issues (e.g., invalid args, context length) - # 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 "This model's maximum context length is" in str(e): + # Check for context_length_exceeded error code in the error body + error_code = None + if e.body and isinstance(e.body, dict): + error_details = e.body.get("error", {}) + if isinstance(error_details, dict): + error_code = error_details.get("code") + + # Check both the error code and message content for context length issues + if ( + error_code == "context_length_exceeded" + or "This model's maximum context length is" in str(e) + or "Input tokens exceed the configured limit" in str(e) + ): return ContextWindowExceededError( message=f"Bad request to OpenAI (context window exceeded): {str(e)}", )