Repository navigation
feat(relay): preserve original model name in mapped responses - #4451
zhusichuang wants to merge 1 commit into
Conversation
WalkthroughThis PR introduces client-specific model name rewriting across relay channels. A new global setting Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
relay/channel/openai/helper.go (1)
55-79:⚠️ Potential issue | 🟡 MinorApply model rewrite to
handleGeminiFormatand Gemini branch ofHandleFinalResponsefor consistency with Claude paths.
handleClaudeFormat(lines 41–43) appliesrelaycommon.ResponseModelNameForClient(info)to rewritestreamResponse.Modelafter unmarshalling, buthandleGeminiFormat(lines 55–79) and the Gemini branch ofHandleFinalResponse(lines 234–262) do not. This creates an inconsistency where Claude-format clients receive the client-requested model name while Gemini-format clients receive the upstream provider's model name.Add the same rewrite logic to both functions:
♻️ Proposed fix
func handleGeminiFormat(c *gin.Context, data string, info *relaycommon.RelayInfo) error { var streamResponse dto.ChatCompletionsStreamResponse if err := common.Unmarshal(common.StringToByteSlice(data), &streamResponse); err != nil { logger.LogError(c, "failed to unmarshal stream response: "+err.Error()) return err } + if rewriteModel, responseModel := relaycommon.ResponseModelNameForClient(info); rewriteModel { + streamResponse.Model = responseModel + } geminiResponse := service.StreamResponseOpenAI2Gemini(&streamResponse, info)In
HandleFinalResponseGemini branch:case types.RelayFormatGemini: var streamResponse dto.ChatCompletionsStreamResponse if err := common.Unmarshal(common.StringToByteSlice(lastStreamData), &streamResponse); err != nil { common.SysLog("error unmarshalling stream response: " + err.Error()) return } + if rewriteModel, responseModel := relaycommon.ResponseModelNameForClient(info); rewriteModel { + streamResponse.Model = responseModel + } geminiResponse := service.StreamResponseOpenAI2Gemini(&streamResponse, info)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@relay/channel/openai/helper.go` around lines 55 - 79, After unmarshalling in handleGeminiFormat and in the Gemini branch of HandleFinalResponse, set the stream response's Model to the client-facing name by assigning streamResponse.Model = relaycommon.ResponseModelNameForClient(info) (or equivalent on the final response struct) before converting to geminiResponse and before marshalling/sending; ensure you perform this rewrite immediately after common.Unmarshal and before calling service.StreamResponseOpenAI2Gemini (in handleGeminiFormat) and before marshalling/sending in the Gemini final-response path so Gemini clients see the client-requested model name just like Claude paths.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@setting/model_setting/global.go`:
- Around line 37-45: The default for ResponseModelOriginalEnabled currently set
to true in defaultOpenaiSettings causes a backward-incompatible change; change
ResponseModelOriginalEnabled to false in the GlobalSettings default
(defaultOpenaiSettings) so the new behavior is opt-in, and update the frontend
default settings in the corresponding ModelSetting.jsx and
SettingGlobalModel.jsx components to the same false default to keep UI and
backend consistent; if keeping true was intentional, document it clearly in the
PR description/release notes instead.
---
Outside diff comments:
In `@relay/channel/openai/helper.go`:
- Around line 55-79: After unmarshalling in handleGeminiFormat and in the Gemini
branch of HandleFinalResponse, set the stream response's Model to the
client-facing name by assigning streamResponse.Model =
relaycommon.ResponseModelNameForClient(info) (or equivalent on the final
response struct) before converting to geminiResponse and before
marshalling/sending; ensure you perform this rewrite immediately after
common.Unmarshal and before calling service.StreamResponseOpenAI2Gemini (in
handleGeminiFormat) and before marshalling/sending in the Gemini final-response
path so Gemini clients see the client-requested model name just like Claude
paths.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4db55220-01a7-4f17-86b0-0e89d9c8d341
📒 Files selected for processing (10)
relay/channel/claude/relay-claude.gorelay/channel/claude/relay_claude_test.gorelay/channel/gemini/relay-gemini.gorelay/channel/openai/chat_via_responses.gorelay/channel/openai/helper.gorelay/channel/openai/relay-openai.gorelay/common/response_model.gosetting/model_setting/global.goweb/src/components/settings/ModelSetting.jsxweb/src/pages/Setting/Model/SettingGlobalModel.jsx
| ResponseModelOriginalEnabled bool `json:"response_model_original_enabled"` | ||
| ThinkingModelBlacklist []string `json:"thinking_model_blacklist"` | ||
| ChatCompletionsToResponsesPolicy ChatCompletionsToResponsesPolicy `json:"chat_completions_to_responses_policy"` | ||
| } | ||
|
|
||
| // 默认配置 | ||
| var defaultOpenaiSettings = GlobalSettings{ | ||
| PassThroughRequestEnabled: false, | ||
| ResponseModelOriginalEnabled: true, |
There was a problem hiding this comment.
Default true introduces a backward-incompatible behavior change for existing deployments.
With model mapping in effect, existing clients have been receiving the upstream/mapped model name in responses. After this PR is deployed, every existing instance will silently start returning the client-requested model name instead, which can break downstream consumers/log pipelines/billing-side reconciliations that key off response.model.
Consider defaulting ResponseModelOriginalEnabled to false so the new behavior is opt-in (and update the corresponding frontend defaults in web/src/components/settings/ModelSetting.jsx line 43 and web/src/pages/Setting/Model/SettingGlobalModel.jsx line 71 to match). If an opt-in default is intentional, please call this out in the PR description / release notes.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@setting/model_setting/global.go` around lines 37 - 45, The default for
ResponseModelOriginalEnabled currently set to true in defaultOpenaiSettings
causes a backward-incompatible change; change ResponseModelOriginalEnabled to
false in the GlobalSettings default (defaultOpenaiSettings) so the new behavior
is opt-in, and update the frontend default settings in the corresponding
ModelSetting.jsx and SettingGlobalModel.jsx components to the same false default
to keep UI and backend consistent; if keeping true was intentional, document it
clearly in the PR description/release notes instead.
Important
📝 变更描述 / Description
新增全局设置
global.response_model_original_enabled,用于控制模型映射生效时响应中的model字段是否返回客户端请求的原始模型名。该设置开启后,OpenAI / Claude / Gemini 相关兼容响应会在返回前将客户端可见的
model字段改回请求模型名,避免暴露映射后的上游模型名。计费、token 统计和上游请求仍继续使用真实上游模型名,不影响路由和结算逻辑。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
git diff --check,无 whitespace/error 输出。go、gofmt、bun,未能运行 Go 测试和前端构建。Summary by CodeRabbit
response_model_original_enabledto control whether original model names are used in responses.