Repository navigation
A streamed Gemini frame keeps the usage and stop reason it carried - #129
Merged
Merged
Conversation
fylorn
force-pushed
the
fix/gemini-stream-usage
branch
from
September 23, 2026 08:03
861958e to
f170ed4
Compare
`stream_chat_completion` built every chunk with `usage: None` and `finish_reason: None` hardcoded. Gemini puts a cumulative `usageMetadata` on every frame and a `finishReason` on the last one, and `GeminiResponse` already deserializes both — the streaming path parsed them and threw them away. The non-streaming path reads the same two fields correctly, so the two disagreed about the same response. Dropping the usage does not cost a number, it costs the billing. A caller with no usage falls back to estimating from character counts, so every streaming request through a Gemini upstream has been accounted for by estimate rather than by what Gemini actually charged. The frame-to-chunk mapping moves into `convert_chunk` so it can be tested without standing up an HTTP stream, and the two mappings the whole-response path already had (`finish_reason_of`, `usage_of`) are now shared rather than written twice — being written twice is how these drifted apart in the first place. Every frame fills usage, not just the last one: the counts are cumulative and callers keep the last chunk that carried any, so that chunk holds the final total. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fylorn
force-pushed
the
fix/gemini-stream-usage
branch
from
September 23, 2026 08:22
f170ed4 to
d6c1649
Compare
Merged
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.
Found while auditing what the enterprise gateway loses on the way to
tw-wire's usage sniffer.The bug
GoogleProvider::stream_chat_completionbuilt every chunk like this:Both fields hardcoded. Gemini puts a cumulative
usageMetadataon every frame and afinishReasonon the last one, andGeminiResponsealready deserializes both — the streaming path parsed them and dropped them on the floor. The non-streaming path reads the same two fields correctly, so the two disagreed about the same response.Why it matters more than a missing field
A caller with no usage falls back to estimating from character counts. In the enterprise gateway that is
stream_usage_or_estimate, whose own doc comment frames the estimate as a fallback for clients that don't opt intostream_options.include_usage. But for a Gemini upstream it was never the fallback — it was the only path, on every streaming request, because the adapter guaranteedusagewould beNone.So streaming traffic through Gemini has been billed by character estimate rather than by what Gemini reported. The other three adapters extract usage; this was the only one that didn't.
The fix
convert_chunk, testable without standing up an HTTP streamfinish_reason_ofandusage_ofare extracted and shared with the whole-response path, which had both mappings inline — written twice is how they drifted apart to begin withTests
google.rshad no test module. Five added, including one that pins the whole-response and streaming paths to the same numbers for the same input — the invariant that was broken.cargo fmt --check,clippy --workspace --all-targets -- -D warnings, and the full suite (85 test binaries) green.🤖 Generated with Claude Code