Skip to content

Commit

Permalink
Fix: Correctly catch context limit exceeded error in openai (#825)
Browse files Browse the repository at this point in the history
Closes #821
  • Loading branch information
klieret authored Oct 24, 2024
1 parent 06f0c84 commit dd77a99
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sweagent/agent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,13 @@ def query(self, history: list[dict[str, str]]) -> str:
temperature=self.args.temperature,
top_p=self.args.top_p,
)
except BadRequestError:
msg = f"Context window ({self.model_metadata['max_context']} tokens) exceeded"
raise ContextWindowExceededError(msg)
except BadRequestError as e:
logger.exception("BadRequestError")
if "context window" in str(e) or getattr(e, "error", {}).get("code") == "context_length_exceeded":
msg = f"Context window ({self.model_metadata['max_context']} tokens) exceeded"
raise ContextWindowExceededError(msg) from e
else:
raise e
# Calculate + update costs, return response
input_tokens = response.usage.prompt_tokens
output_tokens = response.usage.completion_tokens
Expand Down

0 comments on commit dd77a99

Please sign in to comment.