fix(gemini): forward stream chunks that carry only UsageMetadata#2848
Merged
dgageot merged 1 commit intoMay 21, 2026
Merged
Conversation
dgageot
approved these changes
May 21, 2026
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.
Summary
Gemini 3 family models (e.g.
gemini-3-flash-preview,gemini-3.1-flash-lite-preview) emitusageMetadataon stream chunks thatcontain no text and no function calls. The current
gemini.StreamAdapterdrops those chunks before they reach the runtime, so all token counts and
costs come back as zero. Gemini 2.5 happens to fit its
usageMetadataintothe same chunk as the final text, which is why it has worked so far.
Root cause
Two places in
pkg/model/provider/gemini/adapter.go:NewStreamAdapterforwarded only chunks withhasText || hasFuncs.Usage-only chunks were silently discarded before reaching the channel.
Recv()extractedUsageMetadataonly in theelse if res.resp != nilbranch, so even when the synthesised "done" event carried the last
response, the usage was not surfaced.
Fix
UsageMetadataeven when text and tool calls areabsent (
hasUsagecheck added to the OR).Usageextraction out of theelse ifbranch so the done eventalso exposes the usage of the last response.
Reproduction
gemini-3-flash-previewVertex AI response containsusageMetadata(verifiedwith a direct curl against
:generateContentand:streamGenerateContent),but
docker agent run --exec --yolorecordsusage = {input:0, output:0}in
session_items.message_jsonandsessions.cost = 0in the SQLite store.After the fix the same run records non-zero tokens and the cost computed by
the runtime against the
models.devcatalog.gemini-3-flash-previewgemini-2.5-flashTests
pkg/model/provider/gemini/adapter_test.gogains three sub-tests underTestStreamAdapter_GeminiUsageMetadata:forwards chunks containing only UsageMetadata— directly guards thedropped-chunk regression.
done event carries usage from last response— guards the second fix(extracting usage on the done branch).
trackUsage=false suppresses usage extraction— confirms the existingopt-out still works.
The existing
TestStreamAdapter_FunctionCallscontinues to pass.