fix(responses): fix streaming EOF, missing [DONE], reasoning_tokens and created_at float - #6683
fix(responses): fix streaming EOF, missing [DONE], reasoning_tokens and created_at float#6683TokensZhuanfa wants to merge 4 commits into
Conversation
WalkthroughThe change preserves reasoning-token usage in OpenAI Responses payloads. It adds usage fields, converts reasoning-token data, re-serializes non-streaming and streaming responses, falls back to raw data on serialization errors, and signals stream completion. ChangesReasoning usage handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@relay/channel/openai/relay_responses.go`:
- Around line 115-123: Update the stream response handling around streamResponse
and sendResponsesStreamData so usage normalization, including
output_tokens_details derived from top-level reasoning_tokens, completes before
serialization and sending. Move the marshal/fallback send block after the
normalization logic, preserving the existing raw-data fallback when marshaling
fails.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: a1f040b3-0b26-4a18-b1a1-33107757ac5f
📒 Files selected for processing (4)
relay/channel/openai/relay_responses.gorelay/channel/openai/responses_via_chat.gorelaykit/dto/openai_response.gorelaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go
| // Re-serialize to ensure created_at is always a clean integer (not float) | ||
| // and usage includes output_tokens_details with reasoning_tokens. | ||
| reSerialized, err := common.Marshal(streamResponse) | ||
| if err != nil { | ||
| // Fallback to raw data if re-serialization fails | ||
| sendResponsesStreamData(c, streamResponse, data) | ||
| } else { | ||
| sendResponsesStreamData(c, streamResponse, string(reSerialized)) | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Serialize the completion event after usage normalization.
Lines 117-123 send the event before Lines 141-154 add missing output_tokens_details. When an upstream reports only top-level reasoning_tokens, the client receives the original completion event without output_tokens_details.
Process the event usage first. Then marshal and send the event.
Also applies to: 141-155
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@relay/channel/openai/relay_responses.go` around lines 115 - 123, Update the
stream response handling around streamResponse and sendResponsesStreamData so
usage normalization, including output_tokens_details derived from top-level
reasoning_tokens, completes before serialization and sending. Move the
marshal/fallback send block after the normalization logic, preserving the
existing raw-data fallback when marshaling fails.
Problem
When using Codex client (
wire_api = "responses") with NewAPI gateway, streaming responses fail with:This causes repeated retries and duplicate responses.
Root Causes
Bug 1: Missing
[DONE]terminator in streamOaiResponsesStreamHandlerandOaiChatToResponsesStreamHandlerdid not callhelper.Done(c)at the end of the stream. Clients (e.g. Codex) receive EOF and treat it as an abnormal disconnection, triggering reconnection loops.Bug 2: Missing
reasoning_tokensinoutput_tokens_detailsSome upstreams (e.g. Moonshot/Kimi) report
reasoning_tokensat the top level of the usage object:{"usage":{"prompt_tokens":86,"completion_tokens":417,"total_tokens":503,"reasoning_tokens":398}}The
Usagestruct had noreasoning_tokenstop-level field and nooutput_tokens_detailsfield, so the value was silently dropped. Clients parsingresponse.completedevents fail withmissing field reasoning_tokens.Bug 3:
created_atfloat transparent passthroughIn passthrough mode,
created_atwas forwarded as raw JSON from upstream. Some upstreams serialize it as a float (e.g.1785990796.0), which Go clients with strictinttypes cannot parse.Fix
1.
relaykit/dto/openai_response.goAdded
OutputTokensDetails *OutputTokenDetailsandReasoningTokens intfields to theUsagestruct.2.
relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.goUsageFromChatUsagenow extractsreasoning_tokensfrom both top-level andcompletion_tokens_details, and populatesOutputTokensDetails.3.
relay/channel/openai/relay_responses.gocreated_at), populateoutput_tokens_detailsreasoning_tokensintooutput_tokens_details, callhelper.Done(c)at stream end4.
relay/channel/openai/responses_via_chat.goCall
helper.Done(c)at stream end.Impact
/v1/chat/completions— unchanged, no impact/v1/responses— all three bugs fixedSummary by CodeRabbit