fix(vertex): convert Anthropic Messages to Gemini for Gemini upstreams - #6812
fix(vertex): convert Anthropic Messages to Gemini for Gemini upstreams#6812zkasuran wants to merge 1 commit into
Conversation
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
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
WalkthroughThe Vertex adaptor now converts Claude requests to Gemini requests when Gemini mode is active. Tests verify Gemini conversion and preserve Claude request behavior. ChangesVertex request conversion
Estimated code review effort: 2 (Simple) | ~10 minutes Mergeability Score: ⚪ Minimal · up to This localized request-conversion change is merge-ready after normal checks and review; no actionable merge-blocking risk remains. Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
📝 变更描述 / Description
Vertex 渠道当上游模型为 Gemini 时,调用 Anthropic Messages 接口
POST /v1/messages会返回 HTTP 400(Vertex 报
Unknown name anthropic_version / messages / max_tokens)。原因在
relay/channel/vertex/adaptor.go的ConvertClaudeRequest:它无论上游是Claude 还是 Gemini,都把请求包装成
VertexAIClaudeRequest(anthropic_version+messages+max_tokens)。但 Gemini 上游走的是generateContentURL,需要的是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 请求转成 GeminigenerateContent,返回*dto.GeminiChatRequest;Claude 上游保持原有 Anthropic 包装不变。响应侧无需改动:RelayFormatClaude下共享的gemini.GeminiChatHandler已经把 Gemini 响应转回 ClaudeMessages 格式。
关于渠道测试误报成功:渠道测试对 Anthropic 端点走的是同一个
adaptor.ConvertClaudeRequest,并且会检查上游状态码与错误体。它之所以“成功”,是因为默认渠道测试用的是 OpenAI 端点
(
/v1/chat/completions),那条路径转换本就正确,从未触发/v1/messages的缺陷。修复后,对 Anthropic 端点做渠道测试会经过同一段修正后的转换逻辑,产生真正的 Gemini 请求体,不再掩盖问题。
因此测试代码本身无需改动。
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。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 newregression test). The author is not a repo core developer, so this disclosure is
included per contribution policy.
📸 运行证明 / Proof of Work
New regression test
TestConvertClaudeRequestByRequestModefails before the fixand passes after.
Before the fix (fix reverted, test kept):
After the fix: