-
Notifications
You must be signed in to change notification settings - Fork 25
Handle API Status errors 499 differently #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
47ae16d
70c8be7
c38b939
a0fe49e
d665364
389529a
14f2480
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |||||
| from agents.mcp import MCPServer, MCPServerStdio, MCPServerSse, MCPServerStreamableHttp, create_static_tool_filter | ||||||
| from agents.extensions.handoff_prompt import prompt_with_handoff_instructions | ||||||
| from agents import Tool, RunContextWrapper, TContext, Agent | ||||||
| from openai import BadRequestError, APITimeoutError, RateLimitError | ||||||
| from openai import BadRequestError, APITimeoutError, RateLimitError, APIStatusError | ||||||
| from openai.types.responses import ResponseTextDeltaEvent | ||||||
| from typing import Callable | ||||||
|
|
||||||
|
|
@@ -334,7 +334,16 @@ async def _run_streamed(): | |||||
| return | ||||||
| except APITimeoutError: | ||||||
| if not max_retry: | ||||||
| logging.error(f"Max retries for APITimeoutError reached") | ||||||
| logging.error(f"Max API retries reached") | ||||||
| raise | ||||||
| max_retry -= 1 | ||||||
| except APIStatusError as e: | ||||||
| # Retry transient “client closed request / upstream cancelled” style errors | ||||||
| if getattr(e, "status_code", None) != 499: | ||||||
| raise # propagate non-499 errors | ||||||
| # 499: retry | ||||||
| if not max_retry: | ||||||
| logging.error(f"Max API retries reached") | ||||||
| raise | ||||||
| max_retry -= 1 | ||||||
|
Comment on lines
+358
to
366
|
||||||
| except RateLimitError: | ||||||
|
|
@@ -370,6 +379,11 @@ async def _run_streamed(): | |||||
| async_task=async_task, | ||||||
| task_id=task_id) | ||||||
| logging.error(f"Bad Request: {e}") | ||||||
|
||||||
| logging.error(f"Bad Request: {e}") | |
| logging.exception("Timeout Error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new APIStatusError exception handler should be added to the outer exception handling block (lines 361-381) to provide consistent error handling and user feedback. Currently, if a non-499 APIStatusError is raised and propagated from the inner try-except block, it won't be caught by any outer handler, potentially resulting in an unhandled exception. Consider adding an exception handler for APIStatusError similar to the existing handlers for BadRequestError and APITimeoutError.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea probably worth adding a handler in the outer scope like
APITimeoutError