Skip to content

fix(relayconvert): Responses→Chat 工具输出图片改为提升到后续 user 消息 - #7510

Merged
Calcium-Ion merged 1 commit into
mainfrom
fix/responses-tool-output-media
Sep 21, 2026
Merged

Calcium-Ion merged 1 commit into
mainfrom
fix/responses-tool-output-media

Conversation

@Calcium-Ion

@Calcium-Ion Calcium-Ion commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

⚠️ 提交说明 / PR Notice

🔗 关联任务 / Related Issue

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

📝 变更描述 / Description

Responses→Chat 转换器把 function_call_output.output 的内容块数组整体 JSON 字符串化进 tool 消息,图片 data URL 以 base64 文本形式发往上游:图片不再是视觉输入,且其字节被上游按文本计数。

修复(仅 relaykit/relayconvert/internal/oai_responses/to_oai_chat_req.go):

  • output 为内容块数组时,文本块(input_text/output_text/text)以换行拼接留在 tool 消息;媒体块(input_image/input_file/input_audio/input_video)复用现有 responsesContentPartsToChatContent 映射成 Chat 内容块。
  • 媒体块不紧跟在单条 tool 消息之后,而是在整批连续的 function_call_output 结束后(或输入结束时)合并成一条 user 消息。Chat Completions 要求同一 assistant tool_calls 的 tool 回复必须连续,并行工具调用时若在中间插 user 消息会被上游 400。
  • 无文本块时 tool 内容用 [image]、[image] [file] 这类占位,避免上游拒绝空 tool 内容。
  • 纯文本内容块数组同样拼接为纯文本(此前也被 JSON 字符串化)。字符串、对象、普通 JSON 数组、含未知块类型的数组保持原有字符串化行为。

不改 Claude→Chat、Gemini→Chat 路径,不改计费逻辑。

📸 运行证明 / Proof of Work

单元测试(relaykit 模块,GOWORK=off)

  • 新增 TestResponsesRequestToChatCompletionsRequestToolOutputContentParts(8 个形态)与 TestResponsesRequestToChatCompletionsRequestHoistsToolOutputMediaAfterToolBatch(并行工具输出保持连续、输入末尾刷出媒体)。
  • relaykit:go test ./... -count=1、go vet ./...、go build ./...、gofmt -l relayconvert/internal/oai_responses/ 全部通过。根模块:go vet ./...、go build ./... 通过;make test(根模块 + relaykit)52 个包 ok、0 失败。

本地 live 复现与验证(一次性 new-api 实例 + 捕获代理 + 本地网关上游,Advanced Custom 渠道把 /v1/responses 路由到上游 /v1/chat/completions,converter openai_responses_to_openai_chat_completions)

请求:user 提问 → 两个并行 function_call(截图、读文件)→ 截图的 function_call_output.output 为 [input_text, input_image(30 KB PNG data URL)],读文件的 output 为字符串。

二进制 用例 上游 messages 角色顺序 tool 内容 prompt_tokens 模型回答
修复前(origin/main) 非流式 developer, user, assistant, tool, tool 第一条 tool 是含 data:image/png;base64,… 的 JSON 字符串(30,147 字符),无任何 image_url 消息 21,010 Black LIVE_TOOL_MEDIA_05c4047f(颜色错误)
修复后 非流式 developer, user, assistant, tool, tool, user screenshot captured / note.txt: …;末尾 user 消息含 image_url 274 Red LIVE_TOOL_MEDIA_58224119
修复后 流式 同上 同上 274 Red LIVE_TOOL_MEDIA_f158cafc

同一张 22.5 KB 的红色噪点 PNG:修复前 base64 被上游按文本分词,一次请求消耗 21,010 prompt tokens,模型只能从 base64 文本猜颜色并答错;修复后图片按视觉输入计费(274 tokens)且颜色答对。上游 usage 与消费日志、四项额度变动一致。

✅ 提交前检查项 / Checklist

  • 人工确认: 无论描述是否由 AI 生成,我已审阅并精炼全文,仅保留可复核的要点,并声明对其准确性与完整性负责。PR 正文及后续评论均未直接粘贴未经人工过滤的大段 AI 文本。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • 新功能关联 Issue: 若此 PR 标记为 New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。
  • 事前沟通: 若改动较大或涉及方向性变更,已在关联 Issue 中与维护者沟通并达成一致。
  • 功能范围: 本 PR 不是 Coding Plan、逆向渠道、第三方封装接口,也不是对 Codex 渠道类型的改动。
  • 非透传: 本 PR 不是针对开启透传后的转发行为;透传会直接发送内容,不会经过 new-api 的逻辑处理。
  • 范围聚焦: 本 PR 为一项聚焦改动,未包含无关代码。
  • 本地验证: 已按变更路径实际验证,并写明命令与观察结果。仅声明 go build 通过或测试通过,不视为有效证明。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

A function_call_output whose output is a Responses content-part array was
stringified into the Chat tool message, so image data URLs travelled
upstream as base64 text instead of vision input.

Text parts now stay on the tool message (joined with newlines) and media
parts are mapped like regular content parts, then emitted as one user
message once the contiguous tool batch ends so parallel tool outputs stay
adjacent to their assistant tool_calls message. Media-only outputs carry
an "[image]"-style placeholder because upstreams reject empty tool
content; payloads that are not content-part arrays keep the stringified
form.

Closes #7498
@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: de590b57-8989-4c8d-bf36-94e16fff0b1c

📥 Commits

Reviewing files that changed from the base of the PR and between 54eee48 and e954db9.

📒 Files selected for processing (2)
  • relaykit/relayconvert/internal/oai_responses/to_oai_chat_req.go
  • relaykit/relayconvert/internal/oai_responses/to_oai_chat_req_test.go

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


Walkthrough

The Responses-to-Chat conversion now separates tool output text from media. Media is emitted in trailing user messages, while tool messages remain contiguous for each assistant tool-call batch. Tests cover recognized parts, fallback payloads, parallel outputs, and trailing media.

Changes

Tool output media conversion

Layer / File(s) Summary
Conversion and media hoisting
relaykit/relayconvert/internal/oai_responses/to_oai_chat_req.go
The converter preserves recognized text in tool messages, converts recognized media to Chat content, and flushes accumulated media in user messages around tool-call batches. Unsupported payload shapes retain the existing stringified behavior.
Conversion behavior validation
relaykit/relayconvert/internal/oai_responses/to_oai_chat_req_test.go
Tests verify text joining, media placeholders, fallback JSON handling, contiguous parallel tool messages, and final media flushing.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant ResponsesRequest
  participant ResponsesToChatConverter
  participant ChatCompletionsRequest
  ResponsesRequest->>ResponsesToChatConverter: function_call_output items
  ResponsesToChatConverter->>ChatCompletionsRequest: contiguous tool messages with text
  ResponsesToChatConverter->>ChatCompletionsRequest: trailing user message with media
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 2 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 #7498 的编码要求。responsesToolOutputToChat 将文本块按换行保留在 tool 消息中。图片、文件、音频和视频块转换为 Chat 内容块,并在连续的 function_call_output 批次后合并到 user 消息。纯媒体输出使用 [image]、[file] 等占位内容。字符串、对象、普通 JSON 数组和未知块类…
Out of Scope Changes check ✅ Passed 变更仅涉及 Responses→Chat 转换逻辑及其回归测试。变更直接服务于 Issue #7498 的媒体内容转换、工具消息连续性和原有输入行为保持要求。未发现 Claude→Chat、Gemini→Chat、计费逻辑或其他无关功能变更。
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确描述了主要修复:将 Responses→Chat 工具输出中的图片提升到后续 user 消息。它简洁、具体,并与代码变更和 PR 目标一致。
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • 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 sorts the tool-call stream
Text stays clear, media gleams
Tool messages hop in line
Images trail in user design
The converter thumps its feet
And sends each part complete

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

@Calcium-Ion
Calcium-Ion merged commit 6e9de44 into main Sep 21, 2026
3 checks passed
MOON-DREAM-STARS added a commit to MOON-DREAM-STARS/new-api that referenced this pull request Sep 22, 2026
Javabcd pushed a commit to Javabcd/new-api that referenced this pull request Sep 22, 2026
…age (QuantumNous#7510)

A function_call_output whose output is a Responses content-part array was
stringified into the Chat tool message, so image data URLs travelled
upstream as base64 text instead of vision input.

Text parts now stay on the tool message (joined with newlines) and media
parts are mapped like regular content parts, then emitted as one user
message once the contiguous tool batch ends so parallel tool outputs stay
adjacent to their assistant tool_calls message. Media-only outputs carry
an "[image]"-style placeholder because upstreams reject empty tool
content; payloads that are not content-part arrays keep the stringified
form.

Closes QuantumNous#7498

Co-Authored-By: MOON-DREAM-STARS <[email protected]>
GuDong2003 pushed a commit to GuDong2003/new-api that referenced this pull request Sep 25, 2026
…age (QuantumNous#7510)

A function_call_output whose output is a Responses content-part array was
stringified into the Chat tool message, so image data URLs travelled
upstream as base64 text instead of vision input.

Text parts now stay on the tool message (joined with newlines) and media
parts are mapped like regular content parts, then emitted as one user
message once the contiguous tool batch ends so parallel tool outputs stay
adjacent to their assistant tool_calls message. Media-only outputs carry
an "[image]"-style placeholder because upstreams reject empty tool
content; payloads that are not content-part arrays keep the stringified
form.

Closes QuantumNous#7498

Co-Authored-By: MOON-DREAM-STARS <[email protected]>
(cherry picked from commit 6e9de44)
Steve3184 pushed a commit to Steve3184/new-api that referenced this pull request Sep 25, 2026
…age (QuantumNous#7510)

A function_call_output whose output is a Responses content-part array was
stringified into the Chat tool message, so image data URLs travelled
upstream as base64 text instead of vision input.

Text parts now stay on the tool message (joined with newlines) and media
parts are mapped like regular content parts, then emitted as one user
message once the contiguous tool batch ends so parallel tool outputs stay
adjacent to their assistant tool_calls message. Media-only outputs carry
an "[image]"-style placeholder because upstreams reject empty tool
content; payloads that are not content-part arrays keep the stringified
form.

Closes QuantumNous#7498

Co-Authored-By: MOON-DREAM-STARS <[email protected]>
TomJKS pushed a commit to TomJKS/new-api that referenced this pull request Sep 28, 2026
…age (QuantumNous#7510)

A function_call_output whose output is a Responses content-part array was
stringified into the Chat tool message, so image data URLs travelled
upstream as base64 text instead of vision input.

Text parts now stay on the tool message (joined with newlines) and media
parts are mapped like regular content parts, then emitted as one user
message once the contiguous tool batch ends so parallel tool outputs stay
adjacent to their assistant tool_calls message. Media-only outputs carry
an "[image]"-style placeholder because upstreams reject empty tool
content; payloads that are not content-part arrays keep the stringified
form.

Closes QuantumNous#7498

Co-Authored-By: MOON-DREAM-STARS <[email protected]>
@Calcium-Ion
Calcium-Ion deleted the fix/responses-tool-output-media branch October 22, 2026 04:57
timwenx pushed a commit to timwenx/new-api that referenced this pull request Nov 18, 2026
…age (QuantumNous#7510)

A function_call_output whose output is a Responses content-part array was
stringified into the Chat tool message, so image data URLs travelled
upstream as base64 text instead of vision input.

Text parts now stay on the tool message (joined with newlines) and media
parts are mapped like regular content parts, then emitted as one user
message once the contiguous tool batch ends so parallel tool outputs stay
adjacent to their assistant tool_calls message. Media-only outputs carry
an "[image]"-style placeholder because upstreams reject empty tool
content; payloads that are not content-part arrays keep the stringified
form.

Closes QuantumNous#7498

Co-Authored-By: MOON-DREAM-STARS <[email protected]>
xiaolanawaqaq pushed a commit to xiaolanawaqaq/new-apiaaa that referenced this pull request Nov 26, 2026
…age (QuantumNous#7510)

A function_call_output whose output is a Responses content-part array was
stringified into the Chat tool message, so image data URLs travelled
upstream as base64 text instead of vision input.

Text parts now stay on the tool message (joined with newlines) and media
parts are mapped like regular content parts, then emitted as one user
message once the contiguous tool batch ends so parallel tool outputs stay
adjacent to their assistant tool_calls message. Media-only outputs carry
an "[image]"-style placeholder because upstreams reject empty tool
content; payloads that are not content-part arrays keep the stringified
form.

Closes QuantumNous#7498

Co-Authored-By: MOON-DREAM-STARS <[email protected]>
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] Responses→Chat 转换器把 function_call_output 的图片内容拍平成 base64 文本,触发上游 prompt 超限

1 participant