Repository navigation
fix(openai-chat): keep captured usage on an invalid negotiated completion - #7849
Conversation
…tion Every other terminal path of the streaming generator attaches `pendingUsage` to its error event (`invalidChoicesEvent`, `invalidToolCallsEvent`, `upstreamErrorEvent`), but the invalid negotiated tool completion path threw it away. When upstream reported usage before a repeated completion marker or a late argument fragment, those tokens were missing from the request's error event even though every other error carries them. Pass `pendingUsage` through like the sibling terminal errors.
|
Caution Review failedThe pull request is closed. No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Include pending usage in invalid completion errors src/adapters/openai-chat.ts, tests/adapters/openai/openai-chat-parallel-stream.test.ts |
The invalid negotiated tool-completion error now includes pending usage. The regression test checks that the error contains inputTokens: 11 and outputTokens: 7. |
Priority:
Estimated code review effort: 2 (Simple) | ~8 minutes
Change: Bug fix · Severity of issue fixed: Low
Suggested labels: bug
Suggested reviewers: lidge-jun
Merge Risk: 
Minimal · previous review 9d91a
-
The PR is mergeable with no actionable risks identified.
Pre-merge checks: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Full details: Linked Issues check
-
Explanation
Issue
#7841requires the invalid negotiated tool-completion path to preserve upstream usage and requires a regression test. Insrc/adapters/openai-chat.ts, thecompletion === "invalid"path now suppliesusage: pendingUsageto the existing negotiated-completion error event. The event type, message, and status remain unchanged. Intests/adapters/openai/openai-chat-parallel-stream.test.ts, the new test sends completion, usage, and a late argument fragment, then asserts{ inputTokens: 11, outputTokens: 7 }on the error event. The supplied test result reports 52 passes and 0 failures.
Full details: Out of Scope Changes check
-
Explanation
The reported pull request changes are limited to the required adapter fix in
src/adapters/openai-chat.tsand its regression test intests/adapters/openai/openai-chat-parallel-stream.test.ts. The test directly exercises the repeated-completion or late-fragment failure mode from issue#7841. No unrelated behavior, public declaration, or feature change is identified.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
|
✅ Deterministic PR hygiene checks passed. |
⏳ DRAFT
What to do
Review readiness checklist
0/4 boxes ticked. Automatic draft conversion failed. Please convert this pull request to a draft manually until every box above is ticked. Hygiene✅ Deterministic PR hygiene checks passed. |
|
Maintainer integration into
Fixes #7841. Thank you @xubenqiang. |
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. |
Fixes #7841
Every terminal path of the OpenAI-compatible Chat streaming generator attaches
pendingUsageto its error event —invalidChoicesEvent(pendingUsage),invalidToolCallsEvent(..., pendingUsage),upstreamErrorEvent(..., pendingUsage)— except the invalid negotiated tool completion path (block-stop-v1, #7561), which built its event without it. When upstream reported usage before a repeated completion marker or a late argument fragment, those tokens were missing from the request's error event.Change
src/adapters/openai-chat.tscompletion === "invalid"branch now passesusage: pendingUsage(when captured) like its sibling terminal errors.Test
tests/adapters/openai/openai-chat-parallel-stream.test.tsan invalid completion sequence still reports the usage already captured: streams a call, its completion marker, a usage-only chunk, then a late argument fragment that tripsobserveCompletedToolBlock, and asserts the final error event is still the negotiated-completion error and carries{ inputTokens: 11, outputTokens: 7 }.Verified:
bun test tests/adapters/openai/openai-chat-parallel-stream.test.ts-> 52 pass / 0 fail; with onlyopenai-chat.tsreverted, 51 pass / 1 fail (the new test).bun x tsc --noEmitexits 0.Summary by CodeRabbit
Review readiness checklist
This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:
Required local validation passed; commands, results, and any full-suite exception are documented.
I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
I resolved all correct Codex and CodeRabbit findings.
My PR is ready for review.