Repository navigation
fix(relaykit): Gemini stream token details, Responses tool-output media on Gemini, Opus 5.5 default effort - #7687
Merged
Conversation
Gemini usageMetadata on every stream frame is a cumulative snapshot, but mergeGeminiTokenDetails added each frame's per-modality counts to the previous ones. Prompt, tool-use prompt and candidates details were multiplied by the number of frames carrying usage, and converted streams multiplied them again because the same sidecar is remembered at several pipeline steps. A streamed image prompt was billed as 3 x 1089 image tokens. A modality reported by the incoming frame now replaces all earlier entries for it, matching how the scalar counts are merged; modalities the frame omits keep their earlier value, and duplicate entries inside one snapshot are still summed at settlement.
…ne data A function_call_output or custom_tool_call_output holding a Responses content-part array was stringified into functionResponse.response, so Gemini tokenized the base64 media as text (an image tool output cost 24,366 prompt tokens and the model could not see it). Text parts now stay in response and images and documents become inlineData resolved through the SSRF-protected media resolver. Gemini 3 documents multimodal function responses, so the media go into functionResponse.parts there and for any model id not known to be older. Gemini 1.x and 2.x get them as ordinary inlineData parts after every functionResponse of the same turn, which keeps parallel calls grouped. Media a target cannot carry (audio or video inside functionResponse.parts, unsupported MIME types, failed loads) is dropped with an unsupported_media_type diagnostic for the tool-loss policy. The inline-data MIME allowlist now follows the Blob.mimeType list in the Gemini API reference, adding image/gif, image/avif, audio/*, video/* and the listed text and application types, so ordinary Chat and Responses messages with those files are no longer rejected locally.
Claude Opus 5.5 runs at medium effort when a request sets none; every other model, including Opus 5, Sonnet 5.5 and Fable 5.1, runs at high (platform.claude.com/docs/en/build-with-claude/effort). The converter assumed high everywhere, so Opus 5.5 requests without an effort were logged as high, and a Claude client on claude-opus-5-5 routed to an OpenAI upstream sent reasoning effort high instead of the model's default. Claude capabilities now carry a per-model default effort, used when the client gave none: the no-intent and include_thoughts-only paths, adaptive or enabled thinking without a strength, the native passthrough label, and ResolveClaudeDefault for Claude-to-OpenAI conversions. Explicit efforts and budget mappings are unchanged. The value feeds the consume-log reasoning_effort field, OpenAI parameter filtering and the RelayInfo debug string; billing does not read it.
Contributor
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing (10)
✨ Finishing Touches📝 Generate docstrings
🧪 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 |
Javabcd
pushed a commit
to Javabcd/new-api
that referenced
this pull request
Oct 9, 2026
…ia on Gemini, Opus 5.5 default effort (QuantumNous#7687) * fix(relaykit): stop summing cumulative Gemini stream token details Gemini usageMetadata on every stream frame is a cumulative snapshot, but mergeGeminiTokenDetails added each frame's per-modality counts to the previous ones. Prompt, tool-use prompt and candidates details were multiplied by the number of frames carrying usage, and converted streams multiplied them again because the same sidecar is remembered at several pipeline steps. A streamed image prompt was billed as 3 x 1089 image tokens. A modality reported by the incoming frame now replaces all earlier entries for it, matching how the scalar counts are merged; modalities the frame omits keep their earlier value, and duplicate entries inside one snapshot are still summed at settlement. * fix(relayconvert): send Responses tool-output media to Gemini as inline data A function_call_output or custom_tool_call_output holding a Responses content-part array was stringified into functionResponse.response, so Gemini tokenized the base64 media as text (an image tool output cost 24,366 prompt tokens and the model could not see it). Text parts now stay in response and images and documents become inlineData resolved through the SSRF-protected media resolver. Gemini 3 documents multimodal function responses, so the media go into functionResponse.parts there and for any model id not known to be older. Gemini 1.x and 2.x get them as ordinary inlineData parts after every functionResponse of the same turn, which keeps parallel calls grouped. Media a target cannot carry (audio or video inside functionResponse.parts, unsupported MIME types, failed loads) is dropped with an unsupported_media_type diagnostic for the tool-loss policy. The inline-data MIME allowlist now follows the Blob.mimeType list in the Gemini API reference, adding image/gif, image/avif, audio/*, video/* and the listed text and application types, so ordinary Chat and Responses messages with those files are no longer rejected locally. * fix(relayconvert): use medium as the Opus 5.5 default effort Claude Opus 5.5 runs at medium effort when a request sets none; every other model, including Opus 5, Sonnet 5.5 and Fable 5.1, runs at high (platform.claude.com/docs/en/build-with-claude/effort). The converter assumed high everywhere, so Opus 5.5 requests without an effort were logged as high, and a Claude client on claude-opus-5-5 routed to an OpenAI upstream sent reasoning effort high instead of the model's default. Claude capabilities now carry a per-model default effort, used when the client gave none: the no-intent and include_thoughts-only paths, adaptive or enabled thinking without a strength, the native passthrough label, and ResolveClaudeDefault for Claude-to-OpenAI conversions. Explicit efforts and budget mappings are unchanged. The value feeds the consume-log reasoning_effort field, OpenAI parameter filtering and the RelayInfo debug string; billing does not read it.
1 of 5 tasks
1 of 4 tasks
timwenx
pushed a commit
to timwenx/new-api
that referenced
this pull request
Nov 18, 2026
…ia on Gemini, Opus 5.5 default effort (QuantumNous#7687) * fix(relaykit): stop summing cumulative Gemini stream token details Gemini usageMetadata on every stream frame is a cumulative snapshot, but mergeGeminiTokenDetails added each frame's per-modality counts to the previous ones. Prompt, tool-use prompt and candidates details were multiplied by the number of frames carrying usage, and converted streams multiplied them again because the same sidecar is remembered at several pipeline steps. A streamed image prompt was billed as 3 x 1089 image tokens. A modality reported by the incoming frame now replaces all earlier entries for it, matching how the scalar counts are merged; modalities the frame omits keep their earlier value, and duplicate entries inside one snapshot are still summed at settlement. * fix(relayconvert): send Responses tool-output media to Gemini as inline data A function_call_output or custom_tool_call_output holding a Responses content-part array was stringified into functionResponse.response, so Gemini tokenized the base64 media as text (an image tool output cost 24,366 prompt tokens and the model could not see it). Text parts now stay in response and images and documents become inlineData resolved through the SSRF-protected media resolver. Gemini 3 documents multimodal function responses, so the media go into functionResponse.parts there and for any model id not known to be older. Gemini 1.x and 2.x get them as ordinary inlineData parts after every functionResponse of the same turn, which keeps parallel calls grouped. Media a target cannot carry (audio or video inside functionResponse.parts, unsupported MIME types, failed loads) is dropped with an unsupported_media_type diagnostic for the tool-loss policy. The inline-data MIME allowlist now follows the Blob.mimeType list in the Gemini API reference, adding image/gif, image/avif, audio/*, video/* and the listed text and application types, so ordinary Chat and Responses messages with those files are no longer rejected locally. * fix(relayconvert): use medium as the Opus 5.5 default effort Claude Opus 5.5 runs at medium effort when a request sets none; every other model, including Opus 5, Sonnet 5.5 and Fable 5.1, runs at high (platform.claude.com/docs/en/build-with-claude/effort). The converter assumed high everywhere, so Opus 5.5 requests without an effort were logged as high, and a Claude client on claude-opus-5-5 routed to an OpenAI upstream sent reasoning effort high instead of the model's default. Claude capabilities now carry a per-model default effort, used when the client gave none: the no-intent and include_thoughts-only paths, adaptive or enabled thinking without a strength, the native passthrough label, and ResolveClaudeDefault for Claude-to-OpenAI conversions. Explicit efforts and budget mappings are unchanged. The value feeds the consume-log reasoning_effort field, OpenAI parameter filtering and the RelayInfo debug string; billing does not read it.
xiaolanawaqaq
pushed a commit
to xiaolanawaqaq/new-apiaaa
that referenced
this pull request
Nov 26, 2026
…ia on Gemini, Opus 5.5 default effort (QuantumNous#7687) * fix(relaykit): stop summing cumulative Gemini stream token details Gemini usageMetadata on every stream frame is a cumulative snapshot, but mergeGeminiTokenDetails added each frame's per-modality counts to the previous ones. Prompt, tool-use prompt and candidates details were multiplied by the number of frames carrying usage, and converted streams multiplied them again because the same sidecar is remembered at several pipeline steps. A streamed image prompt was billed as 3 x 1089 image tokens. A modality reported by the incoming frame now replaces all earlier entries for it, matching how the scalar counts are merged; modalities the frame omits keep their earlier value, and duplicate entries inside one snapshot are still summed at settlement. * fix(relayconvert): send Responses tool-output media to Gemini as inline data A function_call_output or custom_tool_call_output holding a Responses content-part array was stringified into functionResponse.response, so Gemini tokenized the base64 media as text (an image tool output cost 24,366 prompt tokens and the model could not see it). Text parts now stay in response and images and documents become inlineData resolved through the SSRF-protected media resolver. Gemini 3 documents multimodal function responses, so the media go into functionResponse.parts there and for any model id not known to be older. Gemini 1.x and 2.x get them as ordinary inlineData parts after every functionResponse of the same turn, which keeps parallel calls grouped. Media a target cannot carry (audio or video inside functionResponse.parts, unsupported MIME types, failed loads) is dropped with an unsupported_media_type diagnostic for the tool-loss policy. The inline-data MIME allowlist now follows the Blob.mimeType list in the Gemini API reference, adding image/gif, image/avif, audio/*, video/* and the listed text and application types, so ordinary Chat and Responses messages with those files are no longer rejected locally. * fix(relayconvert): use medium as the Opus 5.5 default effort Claude Opus 5.5 runs at medium effort when a request sets none; every other model, including Opus 5, Sonnet 5.5 and Fable 5.1, runs at high (platform.claude.com/docs/en/build-with-claude/effort). The converter assumed high everywhere, so Opus 5.5 requests without an effort were logged as high, and a Claude client on claude-opus-5-5 routed to an OpenAI upstream sent reasoning effort high instead of the model's default. Claude capabilities now carry a per-model default effort, used when the client gave none: the no-intent and include_thoughts-only paths, adaptive or enabled thinking without a strength, the native passthrough label, and ResolveClaudeDefault for Claude-to-OpenAI conversions. Explicit efforts and budget mappings are unchanged. The value feeds the consume-log reasoning_effort field, OpenAI parameter filtering and the RelayInfo debug string; billing does not read it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 关联任务 / Related Issue
🚀 变更类型 / Type of change
📝 变更描述 / Description
1. Gemini 流式按模态重复计费(
relaykit/dto/usage_merge.go)Gemini 每个流式帧的
usageMetadata都是累计值,mergeGeminiTokenDetails却把每帧的 prompt / toolUsePrompt / candidates 分模态 token 累加。转换流还会在多个管线步骤重复 remember 同一个 sidecar,所以一个带图片的 Responses 流式请求按 3 × 1089 个图片 token 计费。改为与标量字段相同的覆盖语义:新帧报告的模态替换旧值,未报告的保留,同一快照内的重复条目仍在结算时相加。所有调用方(host Gemini 流、转换流的MergeBillingUsageNonZero)喂进来的都是累计快照,没有按帧增量的生产者。2. Responses → Gemini 的 function_call_output 媒体(
oai_responses/to_gemini_chat_req.go)内容数组形式的工具输出原来被整体序列化进
functionResponse.response,base64 被当文本分词。现在文本留在response,input_image/input_file经relaymedia.ResolveBase64Data(SSRF 防护路径)转为 inlineData;custom_tool_call_output同样处理。按代际选择文档里的形状:functionResponse.parts(Gemini API "For Gemini 3 series models";Vertex 列出 png/jpeg/webp/pdf/text-plain)。音频、视频在这里丢弃并记 error 级unsupported_media_type,交给 ToolLossPolicy:文档列表没有,实测 3.7 返回 200 但不计 AUDIO token,2.5 返回 400 "function_response.partsis not supported for this model"。models/后)以gemini-1./gemini-2.开头:作为普通 inlineData 放在同一 user 轮所有 functionResponse 之后,并行调用的分组和顺序不变,音频视频也照常携带。同时把
SupportedMimeTypes对齐 Blob.mimeType 参考:补上 image/gif、image/avif、audio/、video/ 以及列出的 text / application 类型。原来的白名单过小,普通 Chat / Responses 消息里带 GIF 等文件会被本地直接拒绝。普通消息路径其余逻辑不变。golden 文件无变化。3. Opus 5.5 默认 effort 是 medium(
reasoning/claude.go、shared/claude/reasoning.go)effort 文档:"Most Claude models default to high effort ... Claude Opus 5.5 defaults to medium"。Claude 能力表新增按模型的
defaultEffort(claude-opus-5-5为 medium,其余 high),用于客户端未给 effort 的路径:无推理意图、仅 include_thoughts、adaptive / enabled 无强度、原生透传标注、ResolveClaudeDefault。最后一处会让 Claude 客户端经 OpenAI 上游时真的发送reasoning_effort: high,消耗推理 token。显式 effort 和 budget 映射不变。该值只被service/log_info_generate.go(消费日志reasoning_effort)、relay/channel/openai/adaptor.go(OpenAI 参数能力判断)和 RelayInfo 调试输出读取,计费不读取。📸 运行证明 / Proof of Work
新测试在旧代码上的结果:
usage_merge_test.go的多帧用例在旧实现下得到 IMAGE 3267(3 × 1089)、TEXT 126;intent_test.go的默认 effort 表只有 opus-5-5 的 4 个用例失败,opus-5 / sonnet-5-5 不变。实测:同一 worktree 分别构建修复前(973cf8ef4)和修复后的二进制,独立 SQLite、干净 cwd,经 AstrLink。
Fix 1,gemini-3.7-flash,提示词带一张图(上游 prompt 约 1130,其中 IMAGE 1089);括号内为按日志 token 与倍率重算的期望值:
修复后流式与非流式的差额来自输出长度不同。
Fix 2,修复前 → 修复后(上游 prompt token,回答):
functionResponse.parts{"content":"[audio]"},记unsupported_media_typeFix 3(日志
reasoning_effort/ 上游实际发送):未验证:claude-sonnet-5-5 的原生透传标注只有单测。AstrLink 这段时间对该模型多次返回自身的 500"可用渠道不存在",claude-opus-5 在 Claude 渠道上同样不可用。
✅ 提交前检查项 / Checklist
New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。go build通过或测试通过,不视为有效证明。🤖 Generated with Claude Code
Summary by CodeRabbit