Skip to content

fix(relayconvert): Claude tool_result 图片提升到 user 消息;Chat→Responses 流补齐 reasoning part 事件并按段切分 item - #7512

Merged
Calcium-Ion merged 1 commit into
mainfrom
fix/relayconvert-tool-result-media-and-reasoning-parts
Sep 21, 2026
Merged

Calcium-Ion merged 1 commit into
mainfrom
fix/relayconvert-tool-result-media-and-reasoning-parts

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

两处 relaykit 转换器修复,都只改 relaykit/relayconvert/internal/ 下的代码。

1. Claude Messages → Chat:tool_result 图片不再被字符串化(claude_messages/to_oai_chat_req.go)

tool_result.content 为内容块数组时,此前整体 JSON 序列化进 tool 消息,图片 base64 以文本形式发往上游,既不是视觉输入又按文本计 token。现在:

  • text 块换行拼接留在 tool 消息;image 块(base64 或 url source)转成 image_url,追加到这条 Claude user 消息本身转换出的 user 消息里。该 user 消息天然位于整批 tool 消息之后,并行 tool_use 的 tool 回复保持连续。
  • 无文本块时 tool 内容用 [image] 占位,避免上游拒绝空内容。
  • 数组里出现 text/image 之外的块类型(如 document)时整体回退到原有字符串化,不丢数据。
  • 不像 fix(relaykit): hoist tool_result images instead of stringifying them #7444 那样把 assistant 的 text 块改成 user 消息,[text, tool_use] 形态的 assistant 消息行为不变。

2. Chat 流 → Responses 流:reasoning summary part 生命周期(oai_chat/to_oai_responses_stream_resp.go)

不改计费逻辑,不改 Gemini 路径。

📸 运行证明 / Proof of Work

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

  • 新增 claude_messages/to_oai_chat_req_test.go:6 种 tool_result 形态 + 并行 tool_use 的消息顺序。
  • oai_chat/to_oai_responses_resp_test.go 新增两个用例:单段流的完整事件序列;reasoning → text → tool_call → finish_reason → reasoning → text → stop 两段流的 item 生命周期与最终 output(5 个 item,ID 各不相同)。
  • relaykit go test ./...、go vet ./...、go build ./...、gofmt -l 通过;golden 测试通过无需更新快照。根模块 go vet ./...、go build ./... 通过;make test 仅 controller 包的 TestSecurityAccountDeletionConcurrentRequestsHaveOneWinner 因 SQLite 锁竞争失败一次,单独重跑 -count=3 通过,与本 PR 无关。

本地 live 验证(一次性 new-api 实例 + 捕获代理,Claude 用例走真实 OpenAI 协议上游,reasoning 用例走本地 mock Chat 上游)

Claude 用例:/v1/messages 请求,assistant 为 [text, tool_use×2],两个 tool_result 中一个含 22.5 KB 红色噪点 PNG,渠道为 OpenAI 类型。

二进制 用例 上游 messages 角色 tool 内容 prompt_tokens 回答
修复前 非流式 developer, user, assistant, tool, tool, user 第一条 tool 是含 base64 的 JSON 字符串(30,145 字符),无 image_url 21,034 Gray(错)
修复前 流式 同上 同上 21,030 Gray(错)
修复后 非流式 同上 screenshot captured / note.txt: …;末尾 user 消息含 image_url 288 Red
修复后 流式 同上 同上 290 Red

Reasoning 用例:本地 mock 上游按固定顺序回放 Chat 分片(网关内没有模型会在 Chat 流里输出 reasoning_content),Advanced Custom 渠道把 /v1/responses 路由到 mock 的 /v1/chat/completions。

二进制 分片形态 part.added / part.done 无 active item 的 delta 最终 output
修复前 reasoning → text → stop 0 / 0 0 reasoning, message
修复前 reasoning → text → tool_call → finish_reason=tool_calls → reasoning → text → stop 0 / 0 2 reasoning(round 1round 2), message(callingfinal …), function_call
修复后 reasoning → text → stop 1 / 1 0 reasoning, message
修复后 两段形态 2 / 2 0 reasoning(round 1), message(calling), function_call, reasoning(round 2), message(final …)

所有用例消费日志 prompt/completion 与上游 usage 一致,用户额度变动等于日志 quota。

未验证 / 附带观察: Claude 流式用例的 message_start 预估 input_tokens(约 21,950)与最终 213 左右不一致,修复前后相同,来源是 message_start 使用的本地预估 GetEstimatePromptTokens() 把 tool_result 里的图片 base64 当文本计数,属于 #7148 范围,本 PR 未处理。未对真实上游验证「finish_reason 后继续下发 reasoning」形态,该形态来自 #7495 的报告,本 PR 用 mock 回放复现。

✅ 提交前检查项 / Checklist

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

Summary by CodeRabbit

  • New Features

    • Tool results can now include images alongside text when converted to OpenAI chat requests.
    • Streaming Responses API output now supports multiple message and reasoning segments after intermediate tool calls.
    • Reasoning streams emit clear summary-part lifecycle events.
    • Completed reasoning and message segments retain their ordering and content in the final response.
  • Bug Fixes

    • Improved handling of parallel tool results, keeping tool messages contiguous while attaching images to the subsequent user message.

…ses reasoning items

Claude Messages -> Chat: a tool_result whose content is a block array was
stringified into the Chat tool message, so image base64 travelled upstream
as text instead of vision input. Text blocks now stay on the tool message
(joined with newlines) and image blocks become image_url parts on the user
message the same Claude message produces, which lands after the tool batch
and keeps parallel tool messages contiguous. Image-only results carry an
"[image]" placeholder; arrays with other block types keep the stringified
form.

Chat stream -> Responses stream: reasoning items now emit
response.reasoning_summary_part.added and .done around the summary text,
and deltas that arrive after a mid-stream finish_reason open a new
reasoning or message item instead of appending to the closed one. Closed
items are snapshotted so the completed response lists every segment.

Closes #7407
Closes #7495
@coderabbitai

coderabbitai Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 727d0cc2-7cf4-42b6-9d9e-034713615654

📥 Commits

Reviewing files that changed from the base of the PR and between 6e9de44 and 8b1b51f.

📒 Files selected for processing (5)
  • relaykit/relayconvert/internal/claude_messages/to_oai_chat_req.go
  • relaykit/relayconvert/internal/claude_messages/to_oai_chat_req_test.go
  • 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
 ___________________________
< Debugging like it's 2099. >
 ---------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
📝 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

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

@Calcium-Ion
Calcium-Ion merged commit 4eb3b91 into main Sep 21, 2026
2 of 3 checks passed
Calcium-Ion added a commit that referenced this pull request Sep 21, 2026
…7512

Co-Authored-By: ByebyDoggy <ByebyDoggy@users.noreply.github.com>
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
…ses reasoning items (QuantumNous#7512)

Claude Messages -> Chat: a tool_result whose content is a block array was
stringified into the Chat tool message, so image base64 travelled upstream
as text instead of vision input. Text blocks now stay on the tool message
(joined with newlines) and image blocks become image_url parts on the user
message the same Claude message produces, which lands after the tool batch
and keeps parallel tool messages contiguous. Image-only results carry an
"[image]" placeholder; arrays with other block types keep the stringified
form.

Chat stream -> Responses stream: reasoning items now emit
response.reasoning_summary_part.added and .done around the summary text,
and deltas that arrive after a mid-stream finish_reason open a new
reasoning or message item instead of appending to the closed one. Closed
items are snapshotted so the completed response lists every segment.

Closes QuantumNous#7407
Closes QuantumNous#7495

Co-Authored-By: MOON-DREAM-STARS <110315488+MOON-DREAM-STARS@users.noreply.github.com>
Javabcd pushed a commit to Javabcd/new-api that referenced this pull request Sep 22, 2026
…landed in QuantumNous#7512

Co-Authored-By: ByebyDoggy <ByebyDoggy@users.noreply.github.com>
GuDong2003 pushed a commit to GuDong2003/new-api that referenced this pull request Sep 25, 2026
…ses reasoning items (QuantumNous#7512)

Claude Messages -> Chat: a tool_result whose content is a block array was
stringified into the Chat tool message, so image base64 travelled upstream
as text instead of vision input. Text blocks now stay on the tool message
(joined with newlines) and image blocks become image_url parts on the user
message the same Claude message produces, which lands after the tool batch
and keeps parallel tool messages contiguous. Image-only results carry an
"[image]" placeholder; arrays with other block types keep the stringified
form.

Chat stream -> Responses stream: reasoning items now emit
response.reasoning_summary_part.added and .done around the summary text,
and deltas that arrive after a mid-stream finish_reason open a new
reasoning or message item instead of appending to the closed one. Closed
items are snapshotted so the completed response lists every segment.

Closes QuantumNous#7407
Closes QuantumNous#7495

Co-Authored-By: MOON-DREAM-STARS <110315488+MOON-DREAM-STARS@users.noreply.github.com>
(cherry picked from commit 4eb3b91)
Steve3184 pushed a commit to Steve3184/new-api that referenced this pull request Sep 25, 2026
…ses reasoning items (QuantumNous#7512)

Claude Messages -> Chat: a tool_result whose content is a block array was
stringified into the Chat tool message, so image base64 travelled upstream
as text instead of vision input. Text blocks now stay on the tool message
(joined with newlines) and image blocks become image_url parts on the user
message the same Claude message produces, which lands after the tool batch
and keeps parallel tool messages contiguous. Image-only results carry an
"[image]" placeholder; arrays with other block types keep the stringified
form.

Chat stream -> Responses stream: reasoning items now emit
response.reasoning_summary_part.added and .done around the summary text,
and deltas that arrive after a mid-stream finish_reason open a new
reasoning or message item instead of appending to the closed one. Closed
items are snapshotted so the completed response lists every segment.

Closes QuantumNous#7407
Closes QuantumNous#7495

Co-Authored-By: MOON-DREAM-STARS <110315488+MOON-DREAM-STARS@users.noreply.github.com>
TomJKS pushed a commit to TomJKS/new-api that referenced this pull request Sep 28, 2026
…ses reasoning items (QuantumNous#7512)

Claude Messages -> Chat: a tool_result whose content is a block array was
stringified into the Chat tool message, so image base64 travelled upstream
as text instead of vision input. Text blocks now stay on the tool message
(joined with newlines) and image blocks become image_url parts on the user
message the same Claude message produces, which lands after the tool batch
and keeps parallel tool messages contiguous. Image-only results carry an
"[image]" placeholder; arrays with other block types keep the stringified
form.

Chat stream -> Responses stream: reasoning items now emit
response.reasoning_summary_part.added and .done around the summary text,
and deltas that arrive after a mid-stream finish_reason open a new
reasoning or message item instead of appending to the closed one. Closed
items are snapshotted so the completed response lists every segment.

Closes QuantumNous#7407
Closes QuantumNous#7495

Co-Authored-By: MOON-DREAM-STARS <110315488+MOON-DREAM-STARS@users.noreply.github.com>
TomJKS pushed a commit to TomJKS/new-api that referenced this pull request Sep 28, 2026
…landed in QuantumNous#7512

Co-Authored-By: ByebyDoggy <ByebyDoggy@users.noreply.github.com>
Javabcd pushed a commit to Javabcd/new-api that referenced this pull request Oct 9, 2026
…landed in QuantumNous#7512

Co-Authored-By: ByebyDoggy <ByebyDoggy@users.noreply.github.com>
Javabcd pushed a commit to Javabcd/new-api that referenced this pull request Oct 9, 2026
…landed in QuantumNous#7512

Co-Authored-By: ByebyDoggy <ByebyDoggy@users.noreply.github.com>
@Calcium-Ion
Calcium-Ion deleted the fix/relayconvert-tool-result-media-and-reasoning-parts branch October 22, 2026 04:57
timwenx pushed a commit to timwenx/new-api that referenced this pull request Nov 18, 2026
…ses reasoning items (QuantumNous#7512)

Claude Messages -> Chat: a tool_result whose content is a block array was
stringified into the Chat tool message, so image base64 travelled upstream
as text instead of vision input. Text blocks now stay on the tool message
(joined with newlines) and image blocks become image_url parts on the user
message the same Claude message produces, which lands after the tool batch
and keeps parallel tool messages contiguous. Image-only results carry an
"[image]" placeholder; arrays with other block types keep the stringified
form.

Chat stream -> Responses stream: reasoning items now emit
response.reasoning_summary_part.added and .done around the summary text,
and deltas that arrive after a mid-stream finish_reason open a new
reasoning or message item instead of appending to the closed one. Closed
items are snapshotted so the completed response lists every segment.

Closes QuantumNous#7407
Closes QuantumNous#7495

Co-Authored-By: MOON-DREAM-STARS <110315488+MOON-DREAM-STARS@users.noreply.github.com>
timwenx pushed a commit to timwenx/new-api that referenced this pull request Nov 18, 2026
…landed in QuantumNous#7512

Co-Authored-By: ByebyDoggy <ByebyDoggy@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant