Skip to content

fix(relayconvert): keep reasoning summary items active after mid-stream finish_reason / 修复 reasoning summary 在流中途 finish_reason 后失去 active item - #7497

Closed
MOON-DREAM-STARS wants to merge 1 commit into
QuantumNous:mainfrom
MOON-DREAM-STARS:fix/7495-reasoning-segment-lifecycle
Closed

MOON-DREAM-STARS wants to merge 1 commit into
QuantumNous:mainfrom
MOON-DREAM-STARS:fix/7495-reasoning-segment-lifecycle

Conversation

@MOON-DREAM-STARS

@MOON-DREAM-STARS MOON-DREAM-STARS commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Agent

  • Tool: Codex desktop app
  • Tool version: codex-cli 0.155.0-alpha.9
  • Model (full id): deepseek-v4.1-flash
  • Host: Codex desktop app (Windows)
  • Date (UTC): 2026-09-21

Links

User request

"I want you to create a branch locally, fix only this bug, merge that branch into local main, and submit the PR from the branch."

Out of scope — refuse

  • Matched: no
  • If yes, what was told to the user (stop here; do not open a PR): Not applicable

Kind

  • Bug fix
  • New feature
  • Performance / refactor
  • Docs
  • Other:

Issue facts

  • Actual behavior: After a non-empty finish_reason arrives mid-stream, ChatToResponsesStreamState closes the reasoning output item but never resets reasoningStarted / reasoningDone. If the same stream later emits more reasoning_content, the converter keeps sending response.reasoning_summary_text.delta for the already-closed item_id / output_index, without a new response.output_item.added or response.reasoning_summary_part.added.
  • Impact: Clients that merge deltas by Responses output-item lifecycle reject or miss the second reasoning segment, losing or misattributing the reasoning summary. The completed response also merges multiple summaries into one reasoning item.
  • Frequency: Deterministic for the input shape reasoning -> tool call -> mid-stream finish_reason -> reasoning again; real client logs showed thousands of occurrences across multiple days.
  • Evidence that the problem is in new-api rather than the client or upstream: The defect reproduces on unmodified QuantumNous/new-api main @ 9a0be8750 by feeding pure protocol frames directly to the converter, with no upstream service or channel involved.
  • Applicable types and their fields:
    • relay: POST /v1/responses, OpenAI Chat Completions to OpenAI Responses streaming conversion; reasoning output item item_id, output_index, and summary-part lifecycle.
    • billing: not applicable.
    • frontend: not applicable.
    • deployment: not applicable.

Change

The fix repairs the reasoning summary segment lifecycle instead of continuing to emit deltas for a closed item:

  • Adds the response.reasoning_summary_part.added event constant.
  • When a reasoning delta arrives after the current reasoning item is done, appendReasoningDelta starts a new reasoning segment, allocates a new output index and _reasoning_N item ID, and emits response.output_item.added, response.reasoning_summary_part.added, then the delta.
  • doneDeltaEvents stores the current segment text before closing it; each segment gets its own done event and item.
  • finalResponse resolves reasoning items from the output-order segment index so a later segment cannot overwrite an earlier one.
  • Adds a regression test for both EmitSequenceNumber=true and false, covering active-item validity, part-added count, and two independent reasoning outputs in the completed response.

Research

Duplicate / prior art

Docs and code

  • https://docs.newapi.ai/ : unreachable from this machine; not used as deciding evidence.
  • https://deepwiki.com/QuantumNous/new-api : not used as authoritative evidence for this fix.
  • README / repo docs: relaykit/README.md describes response-stream state conversion conventions.
  • Code paths:
    • relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go: maintains reasoning/text/tool output-item lifecycle; this PR fixes reasoning segmentation.
    • relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go: Responses stream event constants.
    • relaykit/relayconvert/response_registry.go and relay/channel/openai/responses_via_chat.go: call path for this converter in the /v1/responses streaming path.

Alternatives considered

  • Option A: reset the flags but keep reusing _reasoning_0. Rejected: it still reuses a closed item ID and output index and cannot represent two independent summaries in the final response.
  • Option B: add reasoning_summary_part.added without opening a new item. Rejected: deltas would still reference an item that is already done, so active-item validation would still fail.
  • Why this approach: allocate a separate output item and index per reasoning segment and preserve each segment's text, satisfying the Responses item lifecycle while keeping the completed response complete.

Files

Path Why
relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go Adds response.reasoning_summary_part.added
relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go Reasoning segment lifecycle, independent IDs/indexes, and segmented text persistence
relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp_test.go Regression coverage for resumed reasoning after mid-stream finish_reason

Behavior

  • Before: the second reasoning delta references the already-closed resp_1_reasoning_0; no reasoning_summary_part.added; the completed response has one reasoning item with text round 1round 2.
  • After: the second reasoning segment opens a new output item and summary part before its deltas; the completed response has two independent reasoning items with round 1 and round 2.
  • Explicit non-goals: do not change text/tool output-item behavior for the same unusual stream shape; do not modify upstream channels, billing, frontend, or deployment configuration.

Verification

Only what was actually run.

  • Commands and results:
    • Unmodified production code on upstream/main @ 9a0be8750, targeted test first failed with:
      • reasoning delta for "resp_1_reasoning_0" arrived without an active item
      • reasoning_summary_part.added expected 2, actual 0
      • completed response should have 2 reasoning items, but had 1 with round 1round 2
    • After the fix, the same test passed:
      • PASS: TestChatCompletionsStreamToResponsesReopensReasoningAfterMidStreamFinishReason
      • both EmitSequenceNumber sub-tests passed.
    • GOWORK=off go vet ./... passed for root and relaykit.
    • GOWORK=off go build ./... passed for root and relaykit.
    • relaykit: GOWORK=off go test ./... passed, including all existing relaykit tests.
    • Upstream CI run 35559346672 is currently action_required (waiting for maintainer approval of the first-time contributor workflow); no jobs have executed yet.
  • Manual steps and observed result: none beyond the reproducible protocol regression test.
  • UI: no UI changes.
  • Tests added or updated: TestChatCompletionsStreamToResponsesReopensReasoningAfterMidStreamFinishReason.
  • Databases / providers / platforms exercised: Linux/amd64 Docker (golang:1.26) for unit tests; SQLite fixtures in root module; no real provider calls.
  • Not verified:
    • Local root make test fails in this environment at TestResponsesWebSocketInitialUpstreamRejectionRefundsReservation (expected 2990 / actual 3000). This failure reproduces on unmodified upstream/main @ 9a0be8750 with both Go 1.25.1 and Go 1.26 and is tracked by upstream PR test(billing): fix websocket rejection refund test after pre-consume dropped output estimation #7491. This PR does not modify controller or billing code.
    • frontend bun run typecheck / bun run test not run; no frontend files changed, upstream CI covers them.
    • no deployment, server change, or real upstream call.

Risks

Scope check

  • Single focused change: yes
  • Secrets included: no
  • Out of scope (Coding Plan / reverse-engineered channel / third-party wrapper / Codex): no

中文版 / Chinese

Agent

  • 工具: Codex 桌面应用
  • 工具版本: codex-cli 0.155.0-alpha.9
  • 模型完整 ID: deepseek-v4.1-flash
  • 运行宿主: Codex 桌面应用(Windows)
  • 日期(UTC): 2026-09-21

Links

User request

"我希望你在本地,新建一个branch,专门修复这个bug,然后将这个分支合并到本地的main,但是要用分支提交PR"

Out of scope — refuse

  • 匹配:否
  • 如果匹配,已告知用户的内容(停止,不创建 PR):不适用

Kind

  • Bug 修复
  • 新功能
  • 性能优化或重构
  • 文档
  • 其他:

Issue facts

  • 实际行为:流中途收到非空 finish_reason 后,ChatToResponsesStreamState 会关闭 reasoning output item,但不会复位 reasoningStarted / reasoningDone。同一流后续再次出现 reasoning_content 时,转换器继续为已关闭的 item_id / output_index 发送 response.reasoning_summary_text.delta,且不补发 response.output_item.added 或 response.reasoning_summary_part.added。
  • 影响:依赖 Responses output item 生命周期归并 delta 的客户端会拒绝或错失第二段 reasoning,导致推理摘要丢失或归属错误;最终响应也会把多段摘要合并进同一个 reasoning item。
  • 频率:对「reasoning → tool call → 中途 finish_reason → 再次 reasoning」这一输入形态确定性复现;真实客户端日志中连续多日累计上万次。
  • 证明问题位于 new-api 而非客户端或上游:在未修改的 QuantumNous/new-api main @ 9a0be8750 上,用纯协议帧直接调用该转换器即可复现;不涉及任何上游服务或渠道。
  • 适用类型及字段:
    • relay / 转发:POST /v1/responses 的 OpenAI Chat Completions → OpenAI Responses 流式转换;reasoning output item 的 item_id、output_index 和 summary part 生命周期。
    • billing / 计费:不适用。
    • frontend / 前端:不适用。
    • deployment / 部署:不适用。

Change

修复 reasoning summary segment 的生命周期,而不是继续向已关闭的 item 发送 delta:

  • 新增 response.reasoning_summary_part.added 事件常量。
  • 当前 reasoning item 已 done 且再次收到 delta 时,appendReasoningDelta 开启新的 reasoning segment,分配新的 output index 和 _reasoning_N item ID,并依次发送 response.output_item.added、response.reasoning_summary_part.added 和 delta。
  • doneDeltaEvents 在关闭当前 segment 前保存其文本;每个 segment 都有自己的 done 事件和 item。
  • finalResponse 按 output order 中的 segment 还原 reasoning item,避免后一段覆盖前一段。
  • 新增回归测试覆盖 EmitSequenceNumber=true/false,验证 active item、part.added 数量以及最终 completed response 中两个独立 reasoning output。

Research

Duplicate / prior art

Docs and code

  • https://docs.newapi.ai/ :本机不可达,未作为判定依据。
  • https://deepwiki.com/QuantumNous/new-api :未作为本修复的权威依据。
  • README / 仓库文档:relaykit/README.md 描述了 response stream state 的转换约定。
  • 代码路径:
    • relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go:维护 reasoning/text/tool output item 生命周期;本 PR 修正 reasoning 分段。
    • relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go:Responses stream 事件常量。
    • relaykit/relayconvert/response_registry.go 与 relay/channel/openai/responses_via_chat.go:该转换器在 /v1/responses 流式路径上的调用点。

Alternatives considered

  • 方案 A:只复位状态但继续复用 _reasoning_0。拒绝:仍然复用已关闭 item 的 ID 和 output index,且最终响应无法表示两段独立 summary。
  • 方案 B:只补发 reasoning_summary_part.added,不开启新 item。拒绝:delta 仍会引用已经 done 的 item,active-item 校验仍会失败。
  • 选择理由:为每个 reasoning segment 分配独立 output item 和 index,并保存每段文本,既满足 Responses item 生命周期,也保证最终响应完整。

Files

Path 说明
relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go 新增 response.reasoning_summary_part.added 事件常量
relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go reasoning segment 生命周期、独立 ID/index 与分段文本保存
relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp_test.go 中途 finish_reason 后再次 reasoning 的回归测试

Behavior

  • 修复前:第二段 reasoning delta 引用已关闭的 resp_1_reasoning_0;没有 reasoning_summary_part.added;completed response 只有 1 个 reasoning item,文本为 round 1round 2。
  • 修复后:第二段 reasoning 先开启新的 output item 和 summary part,再发送 delta;completed response 有 2 个独立 reasoning item,分别为 round 1 和 round 2。
  • 非目标:不改变同类异常流下 text/tool output item 的行为;不修改上游渠道、计费、前端或部署配置。

Verification

仅记录实际运行内容。

  • 命令与结果:
    • 未修改 production 代码的 upstream/main @ 9a0be8750 上,定向测试先失败:
      • reasoning delta for "resp_1_reasoning_0" arrived without an active item
      • reasoning_summary_part.added expected 2,actual 0
      • completed response 应为 2 个 reasoning item,实际为 1 个,文本为 round 1round 2
    • 修复后同一测试通过:
      • PASS: TestChatCompletionsStreamToResponsesReopensReasoningAfterMidStreamFinishReason
      • 两个 EmitSequenceNumber 子测试均通过。
    • root 与 relaykit 的 GOWORK=off go vet ./... 通过。
    • root 与 relaykit 的 GOWORK=off go build ./... 通过。
    • relaykit:GOWORK=off go test ./... 通过,包括全部既有 relaykit 测试。
    • 上游 CI run 35559346672 当前为 action_required(等待维护者批准首次贡献者工作流),尚无 job 执行。
  • 手工步骤与观察结果:除可重复的协议回归测试外无额外手工步骤。
  • UI:本次无 UI 变更。
  • 新增或更新测试:TestChatCompletionsStreamToResponsesReopensReasoningAfterMidStreamFinishReason。
  • 实际涉及的数据库、provider 与平台:Linux/amd64 Docker(golang:1.26)运行单元测试;root module 使用 SQLite fixture;未调用真实 provider。
  • 未验证:
    • 本地 root make test 在当前环境失败于 TestResponsesWebSocketInitialUpstreamRejectionRefundsReservation(expected 2990 / actual 3000)。该失败已在未修改的 upstream/main @ 9a0be8750 上、Go 1.25.1 与 Go 1.26 环境下复现,并由上游 PR test(billing): fix websocket rejection refund test after pre-consume dropped output estimation #7491 跟踪;本 PR 未修改 controller 或 billing 代码。
    • 前端 bun run typecheck / bun run test 未运行;本次无前端变更,由上游 CI 覆盖。
    • 未部署、未修改服务器、未调用真实上游。

Risks

Scope check

  • 单一聚焦变更:是
  • 包含秘密信息:否
  • 超出范围(Coding Plan、逆向渠道、第三方封装、Codex):否

Copilot AI lite review requested due to automatic review settings September 21, 2026 03:59
@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: df27e826-5b7e-402c-b722-bda83999d847

📥 Commits

Reviewing files that changed from the base of the PR and between 9a0be87 and 3fe825c.

📒 Files selected for processing (3)
  • relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go
  • relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp_test.go
  • relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

The converter now creates separate reasoning output items when reasoning resumes after a mid-stream finish_reason. It assigns new indices and identifiers, emits the required lifecycle events, preserves completed text, and tests both sequence-number modes.

Changes

Reasoning segment lifecycle

Layer / File(s) Summary
Segment contract and state
relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp.go, relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go
Adds the reasoning summary-part event constant. Stream state and output references now track reasoning segment numbers, text, indices, and identifiers.
Segment lifecycle and response rendering
relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go
Resumed reasoning opens a new segment and emits new lifecycle events. Completed text is stored, and final response entries resolve the correct segment.
Resumed reasoning regression coverage
relaykit/relayconvert/internal/oai_chat/to_oai_responses_resp_test.go
Tests resumed reasoning after a tool-call finish for both sequence-number modes. The tests verify separate items, valid deltas, ordered summaries, and completed output.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant OAIChatStream
  participant ResponsesConverter
  participant ResponsesClient
  OAIChatStream->>ResponsesConverter: emits finish_reason
  ResponsesConverter->>ResponsesClient: completes current reasoning item
  OAIChatStream->>ResponsesConverter: emits later reasoning delta
  ResponsesConverter->>ResponsesClient: adds a new reasoning item and summary part
  ResponsesConverter->>ResponsesClient: sends reasoning deltas and completion
Loading

Suggested reviewers: calcium-ion

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed PR 实现了 Issue #7495 的编码要求。appendReasoningDelta 在已完成 reasoning item 后创建新的 segment、output_index 和 item_id,并发送新的 response.output_item.added 与 response.reasoning_summary_part.added。每个 reasoning s…
Out of Scope Changes check ✅ Passed 变更范围与 Issue #7495 直接相关。生产代码只修改 reasoning item 生命周期、事件和最终响应重建逻辑。测试覆盖该故障场景及两种 payload 形态。未发现无关功能或无关重构。
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main fix: preserving reasoning summary items after a mid-stream finish_reason. It is specific and related to the changes.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

❤️ Share

A rabbit hops where reason streams
New segments carry fresh IDs
Old thoughts rest in ordered rows
Bright deltas follow added signs
Two paths pass every test
The converter closes cleanly

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

All reviewed changes have regression coverage and no blocking issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Fixes reasoning summary item lifecycle after a mid-stream finish_reason, preserving separate reasoning segments in Responses streams.

Changes:

  • Adds response.reasoning_summary_part.added.
  • Reopens reasoning with independent item IDs and output indexes.
  • Preserves segmented reasoning in completed responses.
  • Adds regression coverage for both sequence-number modes.
File Description
relaykit/​relayconvert/​internal/​oai_chat/​to_oai_responses_stream_resp.go Implements segmented reasoning lifecycle and reconstruction.
relaykit/​relayconvert/​internal/​oai_chat/​to_oai_responses_resp.go Adds the reasoning summary part event constant.
relaykit/​relayconvert/​internal/​oai_chat/​to_oai_responses_resp_test.go Tests reopening and preserving reasoning segments.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@MOON-DREAM-STARS MOON-DREAM-STARS changed the title fix(relayconvert): 修复 reasoning summary 在流中途 finish_reason 后失去 active item fix(relayconvert): keep reasoning summary items active after mid-stream finish_reason / 修复 reasoning summary 在流中途 finish_reason 后失去 active item Sep 21, 2026
@MOON-DREAM-STARS
MOON-DREAM-STARS deleted the fix/7495-reasoning-segment-lifecycle branch September 21, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] oai_chat_to_oai_responses 转换器:流中途出现 finish_reason 后 reasoning delta 失去 active item

3 participants