feat: Include model ID with token usage for live events

This allows users to track token usage data per model and fixes https://github.com/google/adk-python/issues/4084.

Co-authored-by: Kathy Wu <wukathy@google.com>
PiperOrigin-RevId: 853925212
This commit is contained in:
Kathy Wu
2026-01-08 16:22:56 -08:00
committed by Copybara-Service
parent b8917bc80e
commit 23d330eef1
4 changed files with 39 additions and 15 deletions
@@ -19,6 +19,8 @@ from google.adk.utils.variant_utils import GoogleLLMVariant
from google.genai import types
import pytest
MODEL_VERSION = 'gemini-2.5-pro'
@pytest.fixture
def mock_gemini_session():
@@ -30,7 +32,9 @@ def mock_gemini_session():
def gemini_connection(mock_gemini_session):
"""GeminiLlmConnection instance with mocked session."""
return GeminiLlmConnection(
mock_gemini_session, api_backend=GoogleLLMVariant.VERTEX_AI
mock_gemini_session,
api_backend=GoogleLLMVariant.VERTEX_AI,
model_version=MODEL_VERSION,
)
@@ -38,7 +42,9 @@ def gemini_connection(mock_gemini_session):
def gemini_api_connection(mock_gemini_session):
"""GeminiLlmConnection instance with mocked session for Gemini API."""
return GeminiLlmConnection(
mock_gemini_session, api_backend=GoogleLLMVariant.GEMINI_API
mock_gemini_session,
api_backend=GoogleLLMVariant.GEMINI_API,
model_version=MODEL_VERSION,
)
@@ -215,6 +221,7 @@ async def test_receive_usage_metadata_and_server_content(
usage_response = next((r for r in responses if r.usage_metadata), None)
assert usage_response is not None
assert usage_response.model_version == MODEL_VERSION
content_response = next((r for r in responses if r.content), None)
assert content_response is not None
+18 -11
View File
@@ -705,20 +705,27 @@ async def test_connect_without_custom_headers(gemini_llm, llm_request):
mock_live_client.aio.live.connect.return_value = MockLiveConnect()
async with gemini_llm.connect(llm_request) as connection:
# Verify that the connect method was called with the right config
mock_live_client.aio.live.connect.assert_called_once()
call_args = mock_live_client.aio.live.connect.call_args
config_arg = call_args.kwargs["config"]
with mock.patch(
"google.adk.models.google_llm.GeminiLlmConnection"
) as MockGeminiLlmConnection:
async with gemini_llm.connect(llm_request) as connection:
# Verify that the connect method was called with the right config
mock_live_client.aio.live.connect.assert_called_once()
call_args = mock_live_client.aio.live.connect.call_args
config_arg = call_args.kwargs["config"]
# Verify that http_options remains None since no custom headers were provided
assert config_arg.http_options is None
# Verify that http_options remains None since no custom headers were provided
assert config_arg.http_options is None
# Verify that system instruction and tools were still set
assert config_arg.system_instruction is not None
assert config_arg.tools == llm_request.config.tools
# Verify that system instruction and tools were still set
assert config_arg.system_instruction is not None
assert config_arg.tools == llm_request.config.tools
assert isinstance(connection, GeminiLlmConnection)
MockGeminiLlmConnection.assert_called_once_with(
mock_live_session,
api_backend=gemini_llm._api_backend,
model_version=llm_request.model,
)
@pytest.mark.parametrize(