Skip to content

fix: convert OpenAI tool result images for Claude - #6261

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

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 has been reviewed by the submitter and marked ready for 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 输出。
  • 非重复提交: 我已搜索现有的 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 sending OpenAI tool results containing images or PDFs to Claude.
    • Media content is converted into Claude-compatible formats with the correct type and embedded data.
    • Non-media tool content remains unchanged.
  • Bug Fixes

    • Conversion errors for invalid media references are now surfaced instead of producing incomplete requests.

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Tool-result content conversion now resolves OpenAI image_url items into Claude image or document media entries, while preserving non-image content. The Claude request converter applies the result in both tool-result message paths and adds resolver-backed tests.

Changes

Tool result media conversion

Layer / File(s) Summary
Resolve tool-result image content
service/relayconvert/internal/oai_chat/to_claude_messages_req.go
Adds conversion of resolvable image_url content into Claude media messages with base64 data and MIME-based media types.
Apply conversion to Claude tool results
service/relayconvert/internal/oai_chat/to_claude_messages_req.go, service/relayconvert/internal/oai_chat/to_claude_messages_req_test.go
Uses converted content for merged and newly appended tool results, and tests image conversion plus preservation of non-image content.

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

Sequence Diagram(s)

sequenceDiagram
  participant OpenAIRequest
  participant ClaudeConverter
  participant MediaResolver
  participant ClaudeToolResult
  OpenAIRequest->>ClaudeConverter: provide tool-result image_url content
  ClaudeConverter->>MediaResolver: resolve source and MIME type
  MediaResolver-->>ClaudeConverter: return base64 data
  ClaudeConverter->>ClaudeToolResult: set image or document media content
Loading

Possibly related issues

Suggested reviewers: seefs001

Poem

A rabbit found a picture bright,
And wrapped it up for Claude just right.
Base64 carrots, MIME hay,
Non-images stayed along the way.
Tool results now hop in flight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: converting OpenAI tool result images for Claude.
✨ 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
@Maoyitt
Maoyitt marked this pull request as ready for review July 20, 2026 01:09
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