fix: track request count when streaming fails mid-request (fixes #1973)#1975
fix: track request count when streaming fails mid-request (fixes #1973)#1975gn00295120 wants to merge 2 commits intoopenai:mainfrom
Conversation
…ai#1973) ## Problem When streaming fails (API errors, connection drops, context window exceeded, etc.), usage tracking was completely lost. The issue occurs because usage is only accumulated when ResponseCompletedEvent arrives. If the model provider raises an exception before yielding ResponseCompletedEvent, the async for loop exits without ever updating usage. ## Solution Wrap the streaming loop in try-except to ensure at least the request count is tracked when streaming fails. While we cannot estimate token counts without introducing dependencies (like tiktoken) or making assumptions about the model, tracking the request count provides valuable information for: - Monitoring API call frequency - Debugging failed requests - Cost estimation (users know a request was made) Token counts remain at 0 when streaming fails before ResponseCompletedEvent, which accurately reflects that we don't have that information. ## Changes - src/agents/run.py: Added try-except around streaming loop - Tracks request count (Usage(requests=1)) on exception if no response received - Preserves existing behavior for successful streaming - tests/test_usage_tracking_on_error.py: Comprehensive test coverage - Test request tracking on streaming error - Test normal usage tracking still works - Test multi-turn scenarios with error Fixes openai#1973
There was a problem hiding this comment.
Pull Request Overview
This PR fixes issue #1973 by ensuring request counts are tracked even when streaming fails mid-request, preventing complete loss of usage tracking data.
Key Changes:
- Wrapped the streaming loop in try-except to track request counts when exceptions occur before
ResponseCompletedEvent - Added comprehensive test coverage for error scenarios including single-turn failures, successful streaming, and multi-turn scenarios with errors
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/agents/run.py | Added try-except wrapper around streaming loop to track request count on failure before re-raising exception |
| tests/test_usage_tracking_on_error.py | New test file covering request tracking on streaming errors, preserved success behavior, and multi-turn error scenarios |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Thanks but I don't think this is a right solution. |
Summary
This PR fixes issue #1973 where usage tracking was completely lost when streaming fails mid-request.
Problem
When streaming fails (API errors, connection drops, context window exceeded, etc.), usage tracking was completely lost. The issue occurs because usage is only accumulated when
ResponseCompletedEventarrives. If the model provider raises an exception before yieldingResponseCompletedEvent, the async for loop exits without ever updating usage.Why This Happens
In
src/agents/run.pyline 1254-1272, usage is only tracked when we receiveResponseCompletedEvent:If an error occurs before this event (e.g., connection drops, rate limits, context window exceeded), the loop exits and no usage is recorded at all.
Solution
Wrap the streaming loop in try-except to ensure at least the request count is tracked when streaming fails:
This approach:
While we cannot estimate token counts without introducing dependencies or making model-specific assumptions, tracking the request count provides valuable information for:
Changes
src/agents/run.py:
tests/test_usage_tracking_on_error.py (new file):
Testing
All tests pass:
Fixes #1973