Skip to content

fix: convert OpenAI tool result images for Claude#6261

Draft
Maoyitt wants to merge 1 commit into
QuantumNous:mainfrom
Maoyitt:agent/fix-claude-tool-result-image
Draft

fix: convert OpenAI tool result images for Claude#6261
Maoyitt wants to merge 1 commit into
QuantumNous:mainfrom
Maoyitt:agent/fix-claude-tool-result-image

Conversation

@Maoyitt

@Maoyitt Maoyitt commented Jul 17, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

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

Note

This change was AI-assisted. The scope, root cause, generated payload, and test results were reviewed during preparation; the PR remains a draft for final human review.

📝 变更描述 / Description

OpenAI Chat Completions 的普通消息在转换到 Claude Messages 时,会将 image_url 解析为 Claude 原生的 image/source。但 role: tool 分支此前直接把 message.Content 写入 tool_result.content,导致工具返回的图片仍以 OpenAI image_url 结构发送给 Claude 兼容上游并触发参数校验错误。

本修改仅转换工具结果内容中的 image_url:沿用现有媒体解析逻辑读取 URL 或 Base64 数据,并输出 Claude image(PDF 输出为 document)内容块。文本、已经是 Claude 格式的图片块及未知内容保持原样。

🚀 变更类型 / Type of change

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

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

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

📸 运行证明 / Proof of Work

go test ./service/relayconvert/internal/oai_chat
ok  github.com/QuantumNous/new-api/service/relayconvert/internal/oai_chat

go test ./service/relayconvert/...
ok  github.com/QuantumNous/new-api/service/relayconvert
ok  github.com/QuantumNous/new-api/service/relayconvert/internal/oai_chat
ok  github.com/QuantumNous/new-api/service/relayconvert/internal/oai_responses

新增回归测试验证:

  • tool_result.content 中的 OpenAI image_url 会序列化为 Claude image/source,最终请求不再包含 image_url
  • 文本、已有 Claude 图片块和未知内容保持不变。

Summary by CodeRabbit

  • New Features

    • Improved compatibility when tool results include images or PDFs, converting them into Claude-compatible media content.
    • Resolved media is embedded as base64 data, while non-image tool content remains unchanged.
  • Bug Fixes

    • Prevented original image URL fields from being included in converted Claude requests.

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review 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: Pro

Run ID: 04e594b2-3b12-401e-a59f-c35f2fec3e9d

📥 Commits

Reviewing files that changed from the base of the PR and between a63364d and 86e2743.

📒 Files selected for processing (2)
  • service/relayconvert/internal/oai_chat/to_claude_messages_req.go
  • service/relayconvert/internal/oai_chat/to_claude_messages_req_test.go
 _________________________________________________________________________________________________________________
< Estimate the order of your algorithms. Get a feel for how long things are likely to take before you write code. >
 -----------------------------------------------------------------------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ 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.

xcrong added a commit to WebAgentLoop/nextrouter that referenced this pull request Jul 17, 2026
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.

OpenAI image_url blocks are not converted inside Claude tool_result content

1 participant