fix: gemini原生格式请求,非流情况下,不计算reasoning_tokens - #1254
Conversation
WalkthroughThe code modifies the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant GeminiTextGenerationHandler
participant UsageStruct
Client->>GeminiTextGenerationHandler: Send text generation request
GeminiTextGenerationHandler->>UsageStruct: Calculate TotalTokens, PromptTokens
GeminiTextGenerationHandler->>UsageStruct: Set CompletionTokens = TotalTokens - PromptTokens
GeminiTextGenerationHandler-->>Client: Return response with updated usage
Assessment against linked issues
Assessment against linked issues: Out-of-scope changes(No out-of-scope changes found.) Poem
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
relay/channel/gemini/relay-gemini-native.go (1)
52-56: Redundant initialisation ofCompletionTokens
CompletionTokensis assigned here withCandidatesTokenCountbut is overwritten 15-20 lines later (line 69).
Keeping two conflicting assignments complicates reasoning and risks future regressions if the later block is moved or removed.- usage := dto.Usage{ - PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount, - CompletionTokens: geminiResponse.UsageMetadata.CandidatesTokenCount, - TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount, - } + usage := dto.Usage{ + PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount, + TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount, + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
relay/channel/gemini/relay-gemini-native.go(1 hunks)
🔇 Additional comments (1)
relay/channel/gemini/relay-gemini-native.go (1)
58-66:ReasoningTokensstill populated – verify business ruleThe PR description says “非流情况下,不计算 reasoning_tokens” (do not count reasoning tokens for non-stream requests).
Even if the tokens are no longer added toCompletionTokens, you still expose them viaCompletionTokenDetails.ReasoningTokens, which may be interpreted upstream as “countable”.Confirm whether this field must be zeroed / omitted for non-stream responses; otherwise the objective is only half-met.
|
|
||
| // 计算最终使用量 | ||
| usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens | ||
|
|
There was a problem hiding this comment.
Image token logic is clobbered in stream path
A similar Total-Prompt reassignment exists in the stream handler (lines 141-143).
When imageCount != 0, you set usage.CompletionTokens = imageCount * 258 (line 137) and then overwrite it with the diff, nullifying the special-case logic.
Guard the second assignment, e.g.:
- usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
+ // Only overwrite when we have a normal text response
+ if imageCount == 0 {
+ usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
+ }Apply the same guard (or remove the earlier assignment) in the non-stream block to keep semantics consistent.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // 计算最终使用量 | |
| usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens | |
| // 计算最终使用量 | |
| // Only overwrite when we have a normal text response | |
| if imageCount == 0 { | |
| usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens | |
| } |
🤖 Prompt for AI Agents
In relay/channel/gemini/relay-gemini-native.go around lines 67 to 70, the
assignment of usage.CompletionTokens as usage.TotalTokens minus
usage.PromptTokens overwrites the special-case logic for image tokens set
earlier in the stream handler (lines 137 and 141-143). To fix this, add a guard
condition around the second assignment so it only runs when imageCount is zero,
preserving the image token calculation. Also, apply the same guard or remove the
earlier assignment in the non-stream block to maintain consistent semantics.
fix: #1249
Summary by CodeRabbit