Skip to content

fix(live): propagate output token count in live API usage metadata#6064

Open
allen-stephen wants to merge 1 commit into
google:mainfrom
allen-stephen:fix/live-output-tokens
Open

fix(live): propagate output token count in live API usage metadata#6064
allen-stephen wants to merge 1 commit into
google:mainfrom
allen-stephen:fix/live-output-tokens

Conversation

@allen-stephen

Copy link
Copy Markdown
Contributor

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_metadata is always 0 (input/total are correct), so everything reading candidates_token_countreports zero output tokens for live sessions.

Root cause: live usage is a types.UsageMetadata (response_token_count / response_tokens_details), but LlmResponse.usage_metadata is a types.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 GenerateContentResponseUsageMetadata in gemini_llm_connection.receive() via a _to_generate_content_usage_metadata() helper. Updated related tests.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

$ 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 DEBUGlive_bidi_streaming_single_agent, audio stream. Confirmed live response's usage_metadata.candidates_token_count is now non-zero (was 0), input/total unchanged.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

N/A

@adk-bot adk-bot added the live [Component] This issue is related to live, voice and video chat label Jun 10, 2026
@adk-bot

adk-bot commented Jun 10, 2026

Copy link
Copy Markdown
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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

live [Component] This issue is related to live, voice and video chat

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants