fix(live): propagate output token count in live API usage metadata#6064
Open
allen-stephen wants to merge 1 commit into
Open
fix(live): propagate output token count in live API usage metadata#6064allen-stephen wants to merge 1 commit into
allen-stephen wants to merge 1 commit into
Conversation
Collaborator
|
Response from ADK Triaging Agent Hello @allen-stephen, thank you for submitting this pull request! We appreciate the detailed explanation of the problem, root cause, and the tests you have run. To fully follow our contribution guidelines, could you please provide a brief snippet of the console logs or a screenshot showing the non-zero token count after applying your fix? This will help reviewers understand and verify the fix more quickly. Thank you! |
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
Problem:
Live API output token count in
usage_metadatais always0(input/total are correct), so everything readingcandidates_token_countreports zero output tokens for live sessions.Root cause: live usage is a
types.UsageMetadata(response_token_count/response_tokens_details), butLlmResponse.usage_metadatais atypes.GenerateContentResponseUsageMetadata(candidates_token_count/candidates_tokens_details). The raw object was assigned directly; Pydantic re-validated into the target type and silently dropped the two renamed fields.Solution:
Remap the live usage into
GenerateContentResponseUsageMetadataingemini_llm_connection.receive()via a_to_generate_content_usage_metadata()helper. Updated related tests.Testing Plan
Unit Tests:
$ uv run pytest tests/unittests/models/test_gemini_llm_connection.py -q
45 passed, 17 warnings in 1.25s
Manual End-to-End (E2E) Tests:
uv run adk web contributing/samples/live --log_level DEBUG→live_bidi_streaming_single_agent, audio stream. Confirmed live response'susage_metadata.candidates_token_countis now non-zero (was0), input/total unchanged.Checklist
Additional context
N/A