fix: properly throw context window exceeded error on the new style of… [LET-4179] (#4380)

fix: properly throw context window exceeded error on the new style of context window overflow from gpt-5
This commit is contained in:
Charles Packer
2025-09-02 22:20:46 -07:00
committed by GitHub
parent 8475f5b316
commit 6ae49ab3c5

View File

@@ -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)}",
)