Repository navigation
test(llminternal): pin keep-last semantics for streaming usage metadata - #1249
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
c81204a to
6b591fb
Compare
|
Thanks for reviewing. I have signed the Google CLA and the check is now passing. The fix preserves UsageMetadata across streaming chunks when later chunks don't include usage metadata. I have also added a regression test covering the dropped usage scenario. |
UsageMetadata in streamingResponseAggregator.aggregateResponse was unconditionally overwritten on every chunk. When a later chunk carries nil UsageMetadata (as seen with Vertex AI + grounding streams), the real usage data from an earlier chunk is silently wiped out. Close() then returns a final LLMResponse with UsageMetadata == nil despite tokens having been consumed. This applies the same nil-guard (keep-last-non-nil) pattern already used for GroundingMetadata and CitationMetadata. Also adds two regression tests: - TestUsageMetadataNotDroppedByLaterChunkWithNone: verifies earlier usage survives a later nil-usage chunk - TestUsageMetadataUpdatedByLaterChunkWithValue: verifies a later non-nil usage correctly overwrites an earlier one Fixes the bug described in the issue where AfterModelCallback-based cost tracking reads zero/missing tokens for turns that consumed them.
6b591fb to
814b823
Compare
|
Hi, could one of the maintainers/contributors please review this PR when you get a chance? The changes are ready for review. Thanks! |
|
Hi @baptmont |
|
Thanks for chasing this one down — you filed #1248 and the fix, and the The diff is now a single file: So the description no longer describes the change. It says "This applies the That is worth correcting rather than closing, because one of the two tests Which of the two tests earns its placeI mutation-tested rather than reasoning from the names. Mutation 1 — drop the guard entirely (
Mutation 2 — keep-first instead of keep-last
The value is the opposite way round from what the names suggest.
Reproduce with: # mutation 1
go test -count=1 -run UsageMetadata ./internal/llminternal/
# then re-apply the guard as `&& s.usageMetadata == nil` for mutation 2Suggested shape
Smaller things
None of this is a reason to lose the work — the keep-last test is a genuine gap |
|
@baptmont Thanks for the detailed review and mutation testing. I’ve updated the PR to be test-only since #1257 already landed the production fix. I removed the production-code diff and the redundant test, and kept TestUsageMetadataUpdatedByLaterChunkWithValue to pin the keep-last semantics for streaming UsageMetadata. I also rebased onto main, updated the PR title/description, and verified the relevant tests locally. |
|
Hi @baptmont |
wolo-lab
left a comment
There was a problem hiding this comment.
The PR description says the redundant test for a later nil UsageMetadata was removed, but the diff still adds TestUsageMetadataNotDroppedByLaterChunkWithNone. It covers the same case as TestUsageMetadataRetainedWhenLaterChunkHasNoUsageMetadata, which landed with #1257: usage on an earlier chunk, then a chunk with nil usage, then a check that the usage survives. Removing the nil guard in aggregateResponse makes both fail, and switching to keep-first leaves both passing, so the new test catches nothing the existing one doesn't. Could you drop it, as the description intended?
TestUsageMetadataUpdatedByLaterChunkWithValue is the useful part of this PR. It is the only test in the package that fails if the aggregator keeps the first usage value instead of the latest one, so it is worth keeping as is.
|
Thanks @wolo-lab, fixed. I removed the redundant Verified locally with: go test -count=1 -run UsageMetadata ./internal/llminternal |
…2071, #1249, #2070, #2190, #2301, #2310, #2308) (#2350) * fix(llminternal): keep a streamed text chunk's thought signature on the merged part (#2071) (cherry picked from commit 28201d6) * test(llminternal): pin keep-last semantics for streaming usage metadata (#1249) (cherry picked from commit afe6c86) * fix(llminternal): keep ModelVersion on the aggregated streaming response (#2070) (cherry picked from commit 8be9129) * fix(llminternal): drop nil parts in the streaming aggregator (#2190) (cherry picked from commit 46ff99b) * fix(llminternal): skip live responses that carry nothing (#2301) (cherry picked from commit 8cddb9d) * fix(llminternal): keep responses that carry only grounding metadata (#2310) (cherry picked from commit fc6de07) * fix(llminternal): keep streamed function-call and event ids stable (#2308) (cherry picked from commit 3188db9)
Description
This PR adds unit test coverage for streaming
UsageMetadataaggregation ininternal/llminternal.The production fix for preserving
UsageMetadataacross chunks, originally reported in #1248, was merged in #1257.This PR now focuses on the remaining test gap by pinning the keep-last-non-nil behavior when later streaming chunks contain updated
UsageMetadata.Because Gemini usage metadata in streaming responses is cumulative, the latest non-nil
UsageMetadatashould overwrite earlier metadata values.Changes
TestUsageMetadataUpdatedByLaterChunkWithValue.Related: #1248
Testing
Ran locally: