Skip to content

fix(eval): preserve live usage without extra inference calls - #7323

Closed
yang0228 wants to merge 11 commits into
google:mainfrom
yang0228:codex/7321-live-eval-usage
Closed

yang0228 wants to merge 11 commits into
google:mainfrom
yang0228:codex/7321-live-eval-usage

Conversation

@yang0228

@yang0228 yang0228 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Closes #7321. Related: #7351; dependent PR #7427.

Problem: Gemini Live may send token usage separately from response content. Evaluation conversion drops these reports. Retaining each report as another model event would also inflate the existing inference count.

Solution: Preserve usage-bearing events, including empty and nontext content, and merge standalone usage into a compatible model event without changing input events or overwriting existing usage. Match author, model, and Live connection identity; retain unmatched usage independently. This PR preserves the existing per-event/chunk inference count. #7427 changes logical-request counting separately.

2026-10-19 refresh: merged main 04b8b6011ed6335febeef77631f137613d471348 at 2b2f249c2b33953fe1020c79cde05ed8a98b8c64, preserving published history. Resolved the test import and end-of-file conflicts by retaining both this PR's Live usage regressions and upstream's model/tool-duration integration test. The original PR delta remains two files, 324 insertions / 3 deletions; the refresh adds no new product behavior.

Testing Plan

Fresh checks at 2b2f249c2 with the repository's Python 3.12 constraints (google-genai 2.30.0):

  • 216 related tests passed, including upstream duration coverage; 28 warnings. Passed against source and against the installed wheel.
  • All configured pre-commit checks and the actual merge commit hooks passed. The documented NO_UNIT_GUIDE reason records that flagged new modules belong to upstream main; this PR adds no Python modules.
  • Mypy on evaluation_generator.py with the constrained Python 3.12 environment: success, no issues. Unchanged main passes the same command.
  • Built a wheel, installed it in an independent clean environment, and copied the regression module outside the checkout: 19 passed (18 usage cases plus the upstream duration integration test). ADK imported from site-packages.
  • Rechecked the 18 usage regressions against unchanged main 04b8b6011: 16 failed / 2 passed, confirming that the reported bug remains reproducible. The two passing cases are the no-usage controls.
PYTHONPATH="$PWD/src" python -m pytest -q \
  tests/unittests/evaluation/test_evaluation_generator.py \
  tests/unittests/evaluation/test_efficiency_evaluators.py \
  tests/unittests/models/test_gemini_llm_connection.py \
  tests/unittests/live/test_live_llm_flow.py \
  tests/unittests/flows/llm_flows/test_base_llm_flow_realtime.py
# 216 passed, 28 warnings

The real GeminiLlmConnection.receive() adapter, conversion, and efficiency evaluators run with deterministic transport. Before/after/combined/absent usage is covered for one and two chunks. Reported usage remains 15 tokens, final text remains Hello, and the inference count remains the chunk count. Nontext content, separate model calls/invocations, author/model/connection mismatches, and input immutability are covered.

Full-suite verification and limits

The repository's tox.ini configures Python 3.11–3.14. Three full suites completed with the committed per-version constraints:

Python Passed Failed Skipped
3.11.16 20,173 54 98
3.12.13 20,164 54 99
3.13.14 20,164 54 99
3.14.6 Full setup incomplete Not run —

Each completed full suite additionally reported 19 xfailed, 2 xpassed, and 42 passed subtests. All 54 failure IDs were individually rerun on unchanged main 04b8b6011 in each corresponding environment, and all 54 failed there too. None is in the changed evaluation modules. These are not all-green full suites.

The full environments use google-genai 2.30.0, both google-cloud-agentplatform and google-cloud-aiplatform 2.3.0, and Pydantic 2.13.4, as pinned by the repository.

Python 3.14: the pinned litellm==1.95.0 needs a source build on this host. Its Rust toolchain bootstrap did not complete; the stalled setup was stopped. A separate focused installation of the base wheel and the dependencies used by these tests, excluding optional GEPA/LiteLLM, passed 216 related source tests / 29 warnings and 19 installed-wheel tests / 4 warnings on Python 3.14.6 with google-genai 2.30.0. This verifies the affected paths, not the full [test] or [eval] environment on 3.14.

No uv.lock is present, so the following temporary overrides were used without changing repository configuration:

tox -p 2 -e py311,py312,py313,py314 \
  -x 'testenv.runner=uv-venv-runner' \
  -x 'testenv.deps=.[test]' \
  -x 'testenv.install_command=uv pip install -c constraints-{py_dot_ver}.txt {packages}' \
  -x 'testenv.commands=pytest tests/unittests -n 2 -q'

Initial concurrent installs hit a 300-second shared-cache lock timeout; the failed environments were retried with a longer lock timeout and reduced download concurrency. The completed results above are retained, including their baseline failures.

The common 54 failures are distributed as follows:

File under tests/unittests/ Failures
integrations/vmaas/test_agent_engine_environment.py 1
labs/antigravity/test_antigravity_agent.py 11
labs/antigravity/test_event_converter.py 37
test_release_dependencies.py 1
tools/mcp_tool/test_mcp_session_manager.py 1
tools/test_load_web_page.py 3
Exact failure IDs reproduced on unchanged main for Python 3.11, 3.12, and 3.13
tests/unittests/integrations/vmaas/test_agent_engine_environment.py::test_reading_directory_raises
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_a_client_tool_call_is_answered_whichever_order_it_arrives_in[after_stream]
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_a_client_tool_call_is_answered_whichever_order_it_arrives_in[before_done]
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_a_failed_client_tool_is_answered_with_an_error_response[after_stream]
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_a_failed_client_tool_is_answered_with_an_error_response[before_done]
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_a_single_turn_agent_answers_its_client_tool_calls_too[after_stream]
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_a_single_turn_agent_answers_its_client_tool_calls_too[before_done]
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_each_conversation_gets_its_own_capture
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_output_reaches_the_parent_through_node_runner
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_sdk_subagent_steps_preserve_author_and_hierarchy
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_subagent_client_tool_preserves_and_appends_thinking
tests/unittests/labs/antigravity/test_antigravity_agent.py::test_text_less_run_outputs_empty_string_not_none
tests/unittests/labs/antigravity/test_event_converter.py::test_a_buffered_result_becomes_a_dict_payload[any_json_object_passes_through]
tests/unittests/labs/antigravity/test_event_converter.py::test_a_buffered_result_becomes_a_dict_payload[dict]
tests/unittests/labs/antigravity/test_event_converter.py::test_a_buffered_result_becomes_a_dict_payload[json_array_is_wrapped]
tests/unittests/labs/antigravity/test_event_converter.py::test_a_buffered_result_becomes_a_dict_payload[json_str]
tests/unittests/labs/antigravity/test_event_converter.py::test_a_buffered_result_becomes_a_dict_payload[malformed_json_degrades_to_the_raw_string]
tests/unittests/labs/antigravity/test_event_converter.py::test_a_buffered_result_becomes_a_dict_payload[none_is_not_null]
tests/unittests/labs/antigravity/test_event_converter.py::test_a_buffered_result_becomes_a_dict_payload[wrapped_json_object_is_unwrapped]
tests/unittests/labs/antigravity/test_event_converter.py::test_a_built_in_is_answered_from_its_step_and_its_hook_copy_is_inert
tests/unittests/labs/antigravity/test_event_converter.py::test_a_built_in_tool_call_and_result_pair_up_across_its_two_steps
tests/unittests/labs/antigravity/test_event_converter.py::test_a_built_in_tool_is_answered_with_no_buffer_at_all
tests/unittests/labs/antigravity/test_event_converter.py::test_a_client_tool_is_answered_from_the_buffered_result
tests/unittests/labs/antigravity/test_event_converter.py::test_a_client_tool_result_arriving_after_its_done_step_is_flushed
tests/unittests/labs/antigravity/test_event_converter.py::test_a_client_tool_result_drained_at_its_step_is_not_flushed_again
tests/unittests/labs/antigravity/test_event_converter.py::test_a_failed_built_in_tool_step_reports_the_error
tests/unittests/labs/antigravity/test_event_converter.py::test_a_failed_client_tool_reports_the_error_the_error_hook_captured
tests/unittests/labs/antigravity/test_event_converter.py::test_a_result_for_a_call_that_was_never_emitted_is_not_drained
tests/unittests/labs/antigravity/test_event_converter.py::test_a_stripped_step_with_nothing_buffered_yields_nothing
tests/unittests/labs/antigravity/test_event_converter.py::test_a_tool_step_with_json_dict_content_unwraps_the_dict
tests/unittests/labs/antigravity/test_event_converter.py::test_active_step_with_partial_thinking_and_tool_call_waits_for_done
tests/unittests/labs/antigravity/test_event_converter.py::test_active_thinking_cached_in_latest_thoughts_attaches_on_done_flush
tests/unittests/labs/antigravity/test_event_converter.py::test_active_tool_call_multiple_updates_does_not_duplicate_buffered_calls
tests/unittests/labs/antigravity/test_event_converter.py::test_active_tool_call_with_thinking_buffers_until_done
tests/unittests/labs/antigravity/test_event_converter.py::test_active_tool_call_without_thinking_emits_immediately
tests/unittests/labs/antigravity/test_event_converter.py::test_active_tool_call_without_thinking_emits_immediately_on_active
tests/unittests/labs/antigravity/test_event_converter.py::test_built_in_tool_with_step_id_uses_captured_hook_result
tests/unittests/labs/antigravity/test_event_converter.py::test_completed_model_text_maps_to_one_model_text_event
tests/unittests/labs/antigravity/test_event_converter.py::test_completed_model_text_with_thinking_includes_thought_part_first
tests/unittests/labs/antigravity/test_event_converter.py::test_duplicate_tool_call_emitted_once
tests/unittests/labs/antigravity/test_event_converter.py::test_function_call_maps_to_function_call_event
tests/unittests/labs/antigravity/test_event_converter.py::test_incomplete_text_step_produces_no_final_event
tests/unittests/labs/antigravity/test_event_converter.py::test_non_streaming_omits_partial_deltas
tests/unittests/labs/antigravity/test_event_converter.py::test_parallel_tool_calls_without_ids_receive_distinct_synthesized_ids
tests/unittests/labs/antigravity/test_event_converter.py::test_partial_model_text_produces_no_event
tests/unittests/labs/antigravity/test_event_converter.py::test_remaining_thinking_suffix_appends_after_early_tool_response
tests/unittests/labs/antigravity/test_event_converter.py::test_run_command_done_step_extracts_combined_output_over_tool_summary
tests/unittests/labs/antigravity/test_event_converter.py::test_streaming_completed_step_emits_partial_then_final
tests/unittests/labs/antigravity/test_event_converter.py::test_streaming_emits_partial_thinking_then_text_deltas
tests/unittests/test_release_dependencies.py::test_inclusive_upper_bounds_name_an_installed_release
tests/unittests/tools/mcp_tool/test_mcp_session_manager.py::TestMCPSessionManager::test_google_auth_async_transport_handle_request
tests/unittests/tools/test_load_web_page.py::test_load_web_page_allows_public_nat64_ip
tests/unittests/tools/test_load_web_page.py::test_load_web_page_fetches_public_urls_by_pinning_the_resolved_ip
tests/unittests/tools/test_load_web_page.py::test_load_web_page_tries_another_resolved_address_after_connect_error

Checklist

  • Read CONTRIBUTING.md and link the issue.
  • Preserve both sides of the test conflict and review the final two-file PR diff.
  • Run configured pre-commit hooks and type checking.
  • Build and test an independently installed wheel.
  • Verify the reported bug still reproduces on current main.
  • Complete the full unit suite on every configured Python version (3.14 setup limitation above).
  • All full unit suites pass locally (54 reproduced unchanged-main failures).
  • Manually test against a remote Gemini service.

Landing order

Please land this usage-only fix before #7427. That dependent PR changes == len(chunks) to == 1 when logical-request grouping is introduced. #7427 was not changed during this refresh; it will need synchronization and removal of the inherited diff after this prerequisite lands.

The refreshed head is conflict-free on GitHub. Maintainer review and approval of the external-contributor workflow runs remain pending. The deterministic checks do not establish remote Gemini ordering, billing semantics, or release integration.

Merge standalone Live usage into compatible model events during evaluation
conversion, retaining unmatched reports without mutating session events.

Fixes google#7321
Replace the implementation-specific tag with a TODO describing the existing quadratic scan and indexing boundary. No counting or usage behavior changes.
Keep both the Live usage regressions and the upstream model/tool duration test. The PR still changes only the evaluation converter and its existing test module.

NO_UNIT_GUIDE=Merge-only sync; flagged modules are unchanged upstream additions, and this PR introduces no Python modules.
copybara-service Bot pushed a commit that referenced this pull request Oct 31, 2026
Merge #7323

Gemini Live may send token usage separately from response content, and
evaluation conversion dropped those reports. Preserve usage-bearing events,
including empty and non-text content, and merge standalone usage into a
compatible model event (same author, model and Live connection) without
mutating input events or adding inference calls. A report with no compatible
model event is kept and counts as the one model call it represents.

Fixes #7321

Co-authored-by: Yi Liu <yiliuly@google.com>
COPYBARA_INTEGRATE_REVIEW=#7323 from yang0228:codex/7321-live-eval-usage 2d2d71d
PiperOrigin-RevId: 1011019927
@adk-bot

adk-bot commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

Thank you @yang0228 for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 8cd3b12.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 31, 2026
@adk-bot adk-bot closed this Oct 31, 2026
yang0228 added a commit to yang0228/adk-python that referenced this pull request Nov 8, 2026
Merge upstream main 1cb926d after the prerequisite google#7323 landed via
Copybara. Keep its standalone-usage matching and all upstream regressions,
track surviving source events for logical Live counts, and adapt the
receiver test helper to the current Live module.

Refs google#7351

NO_UNIT_GUIDE=Merge imports already-published upstream modules; this PR adds no source modules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Live evaluation drops standalone usage metadata, so token_usage_v1 reports n/a

3 participants