Skip to content

A streamed Gemini frame keeps the usage and stop reason it carried - #129

Merged
fylorn merged 1 commit into
mainfrom
fix/gemini-stream-usage
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/gemini-stream-usage

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Found while auditing what the enterprise gateway loses on the way to tw-wire's usage sniffer.

The bug

GoogleProvider::stream_chat_completion built every chunk like this:

yield Ok(ChatCompletionChunk {
    // …
    choices: vec![ChunkChoice { index: 0, delta: json!({"content": text}), finish_reason: None }],
    usage: None,
});

Both fields 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 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 into stream_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 guaranteed usage would be None.

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

  • frame → chunk moves into convert_chunk, testable without standing up an HTTP stream
  • finish_reason_of and usage_of are extracted and shared with the whole-response path, which had both mappings inline — written twice is how they drifted apart to begin with
  • every frame fills usage, not just the last: counts are cumulative and callers keep the last chunk carrying any, so that one holds the final total

Tests

google.rs had 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

@fylorn
fylorn force-pushed the fix/gemini-stream-usage branch from 861958e to f170ed4 Compare September 23, 2026 08:03
`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
fylorn force-pushed the fix/gemini-stream-usage branch from f170ed4 to d6c1649 Compare September 23, 2026 08:22
@fylorn
fylorn merged commit 36e1545 into main Sep 23, 2026
2 checks passed
@fylorn
fylorn deleted the fix/gemini-stream-usage branch September 23, 2026 08:32
@fylorn fylorn mentioned this pull request Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant