Skip to content

fix(vertex): convert Anthropic Messages to Gemini for Gemini upstreams - #6812

Draft
zkasuran wants to merge 1 commit into
QuantumNous:mainfrom
zkasuran:fix/vertex-messages-convert-6715
Draft

fix(vertex): convert Anthropic Messages to Gemini for Gemini upstreams#6812
zkasuran wants to merge 1 commit into
QuantumNous:mainfrom
zkasuran:fix/vertex-messages-convert-6715

Conversation

@zkasuran

@zkasuran zkasuran commented Aug 13, 2026

Copy link
Copy Markdown

📝 变更描述 / Description

Vertex 渠道当上游模型为 Gemini 时,调用 Anthropic Messages 接口 POST /v1/messages
会返回 HTTP 400(Vertex 报 Unknown name anthropic_version / messages / max_tokens)。

原因在 relay/channel/vertex/adaptor.goConvertClaudeRequest:它无论上游是
Claude 还是 Gemini,都把请求包装成 VertexAIClaudeRequestanthropic_version +
messages + max_tokens)。但 Gemini 上游走的是 generateContent URL,需要的是
contents + generationConfig,所以被 Vertex 拒绝。/v1/chat/completions 路径没有这个
问题,因为 ConvertOpenAIRequest 已经按 RequestMode 分支,对 Gemini 调用
service.ConvertRequest(..., RelayFormatGemini, ...)。只有 /v1/messages 入口缺了这个分支。

本 PR 在 ConvertClaudeRequest 补上同样的分支:当 RequestMode == RequestModeGemini
时,用已注册的 claude_messages_to_gemini_generate_content 转换器(经
service.ConvertRequest)把 Anthropic Messages 请求转成 Gemini generateContent,返回
*dto.GeminiChatRequest;Claude 上游保持原有 Anthropic 包装不变。响应侧无需改动:
RelayFormatClaude 下共享的 gemini.GeminiChatHandler 已经把 Gemini 响应转回 Claude
Messages 格式。

关于渠道测试误报成功:渠道测试对 Anthropic 端点走的是同一个 adaptor.ConvertClaudeRequest
并且会检查上游状态码与错误体。它之所以“成功”,是因为默认渠道测试用的是 OpenAI 端点
/v1/chat/completions),那条路径转换本就正确,从未触发 /v1/messages 的缺陷。修复后,
对 Anthropic 端点做渠道测试会经过同一段修正后的转换逻辑,产生真正的 Gemini 请求体,不再掩盖问题。
因此测试代码本身无需改动。

🚀 变更类型 / Type of change

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

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

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

AI disclosure

This change was written with AI assistance (Claude). I reviewed the code and the
Vertex/Anthropic request shapes and verified it locally. Verified before
submitting: go build ./... (exit 0), go vet ./relay/channel/vertex/...
(clean), go test ./relay/channel/vertex/... (pass, including the new
regression test). The author is not a repo core developer, so this disclosure is
included per contribution policy.

📸 运行证明 / Proof of Work

New regression test TestConvertClaudeRequestByRequestMode fails before the fix
and passes after.

Before the fix (fix reverted, test kept):

--- FAIL: TestConvertClaudeRequestByRequestMode/gemini_upstream_converts_to_generateContent
    map[anthropic_version:vertex-2023-10-16 max_tokens:16 messages:[...]] should not contain "max_tokens"
    expected *dto.GeminiChatRequest, got *vertex.VertexAIClaudeRequest
FAIL

After the fix:

=== RUN   TestConvertClaudeRequestByRequestMode
--- PASS: TestConvertClaudeRequestByRequestMode (0.00s)
    --- PASS: TestConvertClaudeRequestByRequestMode/gemini_upstream_converts_to_generateContent (0.00s)
    --- PASS: TestConvertClaudeRequestByRequestMode/claude_upstream_keeps_anthropic_messages_body (0.00s)
PASS
ok  	github.com/QuantumNous/new-api/relay/channel/vertex	0.012s

A Vertex channel whose upstream is Gemini sent the raw Anthropic Messages
body to the Gemini generateContent URL, so Vertex returned HTTP 400 for
unknown fields anthropic_version, messages and max_tokens. ConvertClaudeRequest
now routes Gemini upstreams through the existing Claude to Gemini converter and
only wraps the Anthropic body for Claude upstreams. The channel test dispatches
through the same ConvertClaudeRequest for the Anthropic endpoint, so it now
exercises the real converted path instead of reporting a false success.

Closes QuantumNous#6715
@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 579929c6-8ea2-4b4b-a551-68fa2cae266c

📥 Commits

Reviewing files that changed from the base of the PR and between ccd535e and aa46c8e.

📒 Files selected for processing (2)
  • relay/channel/vertex/adaptor.go
  • relay/channel/vertex/adaptor_test.go

Walkthrough

The Vertex adaptor now converts Claude requests to Gemini requests when Gemini mode is active. Tests verify Gemini conversion and preserve Claude request behavior.

Changes

Vertex request conversion

Layer / File(s) Summary
Request conversion and regression coverage
relay/channel/vertex/adaptor.go, relay/channel/vertex/adaptor_test.go
Gemini-mode Claude requests now use service.ConvertRequest, validate the converted DTO type, set request_model, and return conversion errors. Tests verify Gemini fields and Claude metadata separately.

Estimated code review effort: 2 (Simple) | ~10 minutes

Mergeability Score: ⚪ Minimal · up to aa46c

This localized request-conversion change is merge-ready after normal checks and review; no actionable merge-blocking risk remains.

Possibly related PRs

Suggested reviewers: calcium-ion

Poem

A rabbit saw Gemini hop,
While Claude requests changed their stop.
Contents bloom, tokens align,
Anthropic fields stay in line.
Tests guard each careful way,
Conversion works today.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR fixes Claude-to-Gemini request conversion and adds regression tests, but it does not update the Anthropic channel test to use dto.ClaudeRequest and the production path [#6715]. Update the Anthropic channel test to construct dto.ClaudeRequest and execute ConvertClaudeRequest through the same production conversion path.
✅ Passed checks (4 passed)
Check name Status Explanation
Out of Scope Changes check ✅ Passed The changes are limited to Vertex request conversion and related regression tests, which match the linked issue objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes converting Anthropic Messages requests to Gemini for Vertex Gemini upstreams.
✨ 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.

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] Vertex AI Gemini 的 /v1/messages 请求未转换,渠道测试却误报转换成功

1 participant