Repository navigation
fix(openai): retain metrics for incomplete and failed response streams - #978
Conversation
Read final usage and output from response.incomplete and response.failed alongside response.completed in the shared streaming postprocessor. Fixes #962
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d212aeed80
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".
| if result.type in ("response.completed", "response.incomplete", "response.failed"): | ||
| if hasattr(result, "response") and hasattr(result.response, "output"): |
There was a problem hiding this comment.
Preserve buffered output when terminal output is null
When raw responses.create(stream=True) yields a newly handled incomplete or failed event whose response.output is None, this branch passes the hasattr check and immediately returns output=None, discarding the items and deltas already assembled in output. This is a supported lifecycle shape—the OpenAI stream helper reconstructs failed/incomplete output when that field is null—and before this change these event types fell through and retained the buffered output. Use the terminal output only when it is non-null; otherwise preserve the reconstructed buffer.
Useful? React with 👍 / 👎.
AI Summary
OpenAI Responses streams can end with
response.incompletewhenmax_output_tokensis reached, or withresponse.failed. The streaming postprocessor only read the final response forresponse.completed, so these streams omitted available token metrics and logged output reconstructed from intermediate events instead of the provider's finalresponse.output.Recognize all three terminal events in both usage extraction and final-output selection:
The shared postprocessor now uses the terminal response's output and any reported usage for both sync and async calls. When usage is absent, token metrics remain absent.
Fixes #962.