Skip to content

fix: preserve Responses streaming tool call arguments - #5745

Closed
yyhhyyyyyy wants to merge 2 commits into
QuantumNous:mainfrom
yyhhyyyyyy:fix/responses-stream-tool-call-args
Closed

yyhhyyyyyy wants to merge 2 commits into
QuantumNous:mainfrom
yyhhyyyyyy:fix/responses-stream-tool-call-args

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)
修复 ChatCompletions -> Responses 中转时的工具调用中断问题。
之前 Responses 流式结果在转换回 Chat Completions 时,部分场景会把正文、工具调用和结束原因处理成互斥关系:有正文时可能丢掉 tool call;response.incomplete 或 status:"incomplete" 可能被包装成 stop;部分非标准上游缺少 call_id 或只发送 tool argument delta 时,工具参数会被缓冲但没有正确下发。这样 agent 会误以为本轮已经正常结束,导致后续工具调用链路中断。
本次修复让 Chat 输出允许同时保留 content 和 tool_calls,并统一 Responses 流式收尾逻辑:工具调用参数会在完成、incomplete 或 EOF fallback 前 flush;incomplete_details.reason 会正确映射为 length / content_filter,且优先级高于 tool_calls,避免执行被截断的工具参数。同时补充了流式/非流式回归测试,覆盖正文与工具调用共存、缺 call_id、delta-only、incomplete 优先级。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

  • Bug Fixes
    • Improved streamed tool-call handling so content and tool-use blocks can appear together more consistently, with more reliable finalization.
    • Fixed chat finish reasons for incomplete responses, including better mapping for content filtering and length limits.
    • Made response conversion more consistent, allowing text and tool-use parts to be included together when applicable.
    • Preserved token usage/completion details more accurately and updated incomplete-details to use the reason JSON key for compatibility.

@coderabbitai

coderabbitai Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 18475e93-db16-4bd2-9aa6-702b8f8ad1ec

📥 Commits

Reviewing files that changed from the base of the PR and between 410b0c8 and 2bf65d3.

📒 Files selected for processing (1)
  • relay/channel/openai/chat_via_responses.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • relay/channel/openai/chat_via_responses.go

Walkthrough

The PR renames the incomplete-response reason field, updates status-to-finish-reason mapping, changes mixed text/tool-call conversion, and refactors the OpenAI responses stream relay to buffer tool calls and finalize chunks consistently.

Changes

OpenAI responses conversion and streaming

Layer / File(s) Summary
Status contract and finish-reason helper
dto/openai_response.go, service/openaicompat/responses_to_chat.go, service/openai_chat_responses_compat.go
IncompleteDetails now serializes Reason, and shared helpers expose status-to-finish-reason mapping through a compatibility wrapper.
Chat-completion conversion
service/openaicompat/responses_to_chat.go
Response output now extracts tool calls whenever output exists, derives finish reasons from status before falling back to tool_calls, and leaves message content unchanged when no tool calls are present.
Claude and Gemini output conversion
service/convert.go
ResponseOpenAI2Claude and ResponseOpenAI2Gemini now emit text and tool-call parts together, with updated argument parsing and raw fallback payloads.
Stream state helpers
relay/channel/openai/chat_via_responses.go
The stream handler adds canonical tool-call state, metadata application, deterministic flushing, and final-chunk emission helpers.
Stream event handling
relay/channel/openai/chat_via_responses.go
response.created, response.output_text.delta, response.output_item.added/done, and response.function_call_arguments.delta now use the new helper state for metadata and buffered tool-call arguments.
Stream completion and final chunk
relay/channel/openai/chat_via_responses.go
response.function_call_arguments.done, response.completed, response.incomplete, and the scan-loop teardown now flush pending tool calls and finalize through sendFinalChunk.

Sequence Diagram(s)

sequenceDiagram
  participant OpenAIResponsesStream
  participant OaiResponsesToChatStreamHandler
  participant applyResponseMetadata
  participant sendFinalChunk
  participant flushPendingToolCalls

  OpenAIResponsesStream->>OaiResponsesToChatStreamHandler: response.created / deltas / completed
  OaiResponsesToChatStreamHandler->>applyResponseMetadata: response.created / response.completed / response.incomplete
  OaiResponsesToChatStreamHandler->>sendFinalChunk: end of stream
  sendFinalChunk->>flushPendingToolCalls: flush buffered tool calls
  sendFinalChunk-->>OaiResponsesToChatStreamHandler: emit stop chunk
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • QuantumNous/new-api#2397: Also changes service/convert.go’s OpenAI→Claude conversion logic around tool calls and argument parsing.
  • QuantumNous/new-api#2837: Also changes relay/channel/openai/chat_via_responses.go and related OpenAI responses streaming logic.
  • QuantumNous/new-api#2889: Also updates the same responses-to-chat streaming and final-chunk path in relay/channel/openai/chat_via_responses.go.

Suggested reviewers

  • seefs001
  • creamlike1024

Poem

🐰 I hopped through streams where text and tools now meet,
with reason renamed and endings neat.
Pending calls get one last tidy sort,
then stop-chunks land in a carrot cart.
I wiggle my nose: “boop!” — and all is sweet.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: preserving Responses streaming tool call arguments.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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/chat_via_responses.go`:
- Around line 348-359: The fallback usage estimate is being computed too late,
so sendFinalChunk in chat_via_responses.go can finalize Claude conversion with
zero-value usage before the post-loop estimate runs. Move the fallback usage
calculation earlier, before any response.completed or response.incomplete path
can call sendFinalChunk, and make sure ClaudeConvertInfo.Usage is populated from
that computed usage in sendFinalChunk so sentStop does not lock in empty usage.
🪄 Autofix (Beta)

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

Run ID: 86b35c7c-35f3-4f5f-9bd4-4f5ba920224b

📥 Commits

Reviewing files that changed from the base of the PR and between 7939674 and 410b0c8.

📒 Files selected for processing (5)
  • dto/openai_response.go
  • relay/channel/openai/chat_via_responses.go
  • service/convert.go
  • service/openai_chat_responses_compat.go
  • service/openaicompat/responses_to_chat.go

Calcium-Ion added a commit that referenced this pull request Jun 27, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to #5745.
Calcium-Ion added a commit that referenced this pull request Jun 28, 2026
* fix(openai): harden Chat-to-Responses compatibility

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to #5745.

* refactor: rename openaicompat to relayconvert for improved clarity

* feat(gemini): support responses request conversion

* feat: add responses to chat conversion support

* fix: harden responses chat conversion edge cases
liulixin-lex pushed a commit to liulixin-lex/xy-api that referenced this pull request Jul 7, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.
liulixin-lex pushed a commit to liulixin-lex/xy-api that referenced this pull request Jul 7, 2026
* fix(openai): harden Chat-to-Responses compatibility

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

* refactor: rename openaicompat to relayconvert for improved clarity

* feat(gemini): support responses request conversion

* feat: add responses to chat conversion support

* fix: harden responses chat conversion edge cases
0401lucky pushed a commit to 0401lucky/new-api that referenced this pull request Jul 7, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

(cherry picked from commit 3a506f5)
ruanhangjian pushed a commit to ruanhangjian/new-api that referenced this pull request Jul 11, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.
ruanhangjian pushed a commit to ruanhangjian/new-api that referenced this pull request Jul 11, 2026
* fix(openai): harden Chat-to-Responses compatibility

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

* refactor: rename openaicompat to relayconvert for improved clarity

* feat(gemini): support responses request conversion

* feat: add responses to chat conversion support

* fix: harden responses chat conversion edge cases
noah-wung pushed a commit to noah-wung/new-api that referenced this pull request Jul 17, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
* fix(openai): harden Chat-to-Responses compatibility

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

* refactor: rename openaicompat to relayconvert for improved clarity

* feat(gemini): support responses request conversion

* feat: add responses to chat conversion support

* fix: harden responses chat conversion edge cases
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
* fix(openai): harden Chat-to-Responses compatibility

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

* refactor: rename openaicompat to relayconvert for improved clarity

* feat(gemini): support responses request conversion

* feat: add responses to chat conversion support

* fix: harden responses chat conversion edge cases
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
* fix(openai): harden Chat-to-Responses compatibility

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

* refactor: rename openaicompat to relayconvert for improved clarity

* feat(gemini): support responses request conversion

* feat: add responses to chat conversion support

* fix: harden responses chat conversion edge cases
dofastted pushed a commit to dofastted/new-api that referenced this pull request Sep 16, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

(cherry picked from commit 3a506f5)
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
* fix(openai): harden Chat-to-Responses compatibility

Add a shared Responses-to-Chat stream state machine and use it from the OpenAI relay path. Preserve assistant text alongside tool calls, bind tool argument deltas by output_index, map incomplete finish reasons, support reasoning/custom tool events, and buffer upstream SSE for non-stream Chat clients.

Add deterministic service tests and relay SSE tests for the conversion path.

Related to QuantumNous#5745.

* refactor: rename openaicompat to relayconvert for improved clarity

* feat(gemini): support responses request conversion

* feat: add responses to chat conversion support

* fix: harden responses chat conversion edge cases
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.

2 participants