Skip to content

fix(provider): Fix OpenAI compatible tool result message format#2

Merged
wutongci merged 2 commits into
astercloud:mainfrom
lwmacct:date/251127-01-15
Nov 27, 2025
Merged

fix(provider): Fix OpenAI compatible tool result message format#2
wutongci merged 2 commits into
astercloud:mainfrom
lwmacct:date/251127-01-15

Conversation

@lwmacct

@lwmacct lwmacct commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

Summary

Fix ToolResultBlock conversion to OpenAI format for OpenRouter/Anthropic compatibility.

Problem: When using OpenRouter to access Anthropic models, tool call results were not being properly formatted, causing 403 "Request not allowed" errors on subsequent API calls.

Root Cause: The OpenAICompatibleProvider.convertMessages() function was not correctly handling:

  1. ToolResultBlock - needs to be converted to independent role: "tool" messages
  2. ToolUseBlock in ContentBlocks - needs to be extracted for assistant messages

Changes:

  • Convert ToolResultBlock to independent role: "tool" messages (OpenAI API spec)
  • Extract ToolUseBlock from ContentBlocks for assistant messages
  • Ensure assistant messages with tool_calls have a content field (required by OpenRouter → Anthropic)

Test Results

Added OpenRouter demo with verification tests:

============================================================
📊 测试结果总结
============================================================
  ✅ PASS  创建测试文件                          (3.16s)
  ✅ PASS  读取文件内容                          (407ms)
  ✅ PASS  执行 Bash 命令                      (2.438s)
  ✅ PASS  Agent 状态检查                      (0s)
------------------------------------------------------------
  总计: 4 通过, 0 失败, 耗时 6.006s
  🎉 所有测试通过!
============================================================

Tested Models:

  • anthropic/claude-sonnet-4.5 via OpenRouter ✅
  • anthropic/claude-haiku-4.5 via OpenRouter ✅

Test Plan

  • Run go run demo/1000.openrouter/main.go with valid OPENROUTER_API_KEY
  • Verify file creation test passes
  • Verify file read test passes
  • Verify bash command test passes
  • Verify Agent status check passes

🤖 Generated with Claude Code

Fix ToolResultBlock conversion to OpenAI format for OpenRouter/Anthropic compatibility.

Changes:
- Convert ToolResultBlock to independent role:"tool" messages (OpenAI API spec)
- Extract ToolUseBlock from ContentBlocks for assistant messages
- Ensure assistant messages with tool_calls have content field

Add OpenRouter demo with verification tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment

Thanks for integrating Codecov - We've got you covered ☂️

@wutongci

Copy link
Copy Markdown
Contributor

不要新增demo 目录,在examples 目录实现

- Move demo/1000.openrouter/ → examples/openrouter/
- Remove demo/ directory to maintain consistent project structure
- Update providers README with reference to complete example
- Keep core OpenAI compatible provider fix unchanged

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cosohuang

Copy link
Copy Markdown

感谢你的贡献!🎉

审核结果

✅ 核心修复代码

pkg/provider/openai_compatible.go 的修复非常好:

  • 正确识别并解决了 OpenRouter 访问 Anthropic 模型时的工具调用格式问题
  • 测试充分,所有单元测试通过
  • 代码质量高,符合项目规范

📁 目录结构调整

为了保持项目一致性,我对目录结构做了小幅调整:

  • demo/1000.openrouter/examples/openrouter/
  • 这样与现有 40+ 个示例目录保持统一的结构
  • 同时在 examples/providers/README.md 添加了完整示例的引用链接

✅ 验证通过

  • 所有单元测试通过
  • 编译验证通过
  • Pre-commit 检查全部通过(go fmt, go vet, golangci-lint)

如果你同意这些调整,我可以直接合并这个 PR。再次感谢你的优质贡献!💯

@wutongci
wutongci merged commit deaf1ef into astercloud:main Nov 27, 2025
4 checks passed
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.

4 participants