From 02df3e345804c43ec0fd18af1b33c97a14a0df25 Mon Sep 17 00:00:00 2001 From: Krzysztof Czuszynski Date: Tue, 28 Oct 2025 23:40:38 +0100 Subject: [PATCH 1/6] fix not passing finished transcription flag --- src/google/adk/models/gemini_llm_connection.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/google/adk/models/gemini_llm_connection.py b/src/google/adk/models/gemini_llm_connection.py index f1470c0a29..e7dd3acd28 100644 --- a/src/google/adk/models/gemini_llm_connection.py +++ b/src/google/adk/models/gemini_llm_connection.py @@ -162,18 +162,12 @@ async def receive(self) -> AsyncGenerator[LlmResponse, None]: yield self.__build_full_text_response(text) text = '' yield llm_response - if ( - message.server_content.input_transcription - and message.server_content.input_transcription.text - ): + if message.server_content.input_transcription: llm_response = LlmResponse( input_transcription=message.server_content.input_transcription, ) yield llm_response - if ( - message.server_content.output_transcription - and message.server_content.output_transcription.text - ): + if message.server_content.output_transcription: llm_response = LlmResponse( output_transcription=message.server_content.output_transcription ) From b9feb2b6093803afc6cf04566f8224f74a569987 Mon Sep 17 00:00:00 2001 From: Krzysztof Czuszynski Date: Tue, 28 Oct 2025 23:42:12 +0100 Subject: [PATCH 2/6] add unittest to check the transcription finished flag is passed --- .../models/test_gemini_llm_connection.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/unittests/models/test_gemini_llm_connection.py b/tests/unittests/models/test_gemini_llm_connection.py index 2327115033..1b4309ef65 100644 --- a/tests/unittests/models/test_gemini_llm_connection.py +++ b/tests/unittests/models/test_gemini_llm_connection.py @@ -109,3 +109,53 @@ async def test_close(gemini_connection, mock_gemini_session): await gemini_connection.close() mock_gemini_session.close.assert_called_once() + + +@pytest.mark.asyncio +@pytest.mark.parametrize('tx_direction', ['input', 'output']) +async def test_receive_transcript_finished( + gemini_connection, mock_gemini_session, tx_direction +): + """Test receive_transcript_finished for input and output transcription.""" + + finished_tx = types.Transcription(finished=True) + + class Msg: + + def __init__(self): + self.server_content = mock.Mock() + sc = self.server_content + sc.model_turn = None + if tx_direction == 'input': + sc.input_transcription = finished_tx + sc.output_transcription = None + else: + sc.input_transcription = None + sc.output_transcription = finished_tx + sc.interrupted = False + sc.turn_complete = False + self.tool_call = None + self.session_resumption_update = None + + async def gen(): + yield Msg() + + mock_gemini_session.receive = mock.Mock(return_value=gen()) + + responses = [] + async for r in gemini_connection.receive(): + responses.append(r) + + if tx_direction == 'input': + tx_resps = [r for r in responses if r.input_transcription] + else: + tx_resps = [r for r in responses if r.output_transcription] + + if tx_direction == 'input': + assert tx_resps, 'Excpected input transcription response' + assert tx_resps[0].input_transcription.finished is True + assert not tx_resps[0].input_transcription.text + else: + assert tx_resps, 'Expected output transcription response' + assert tx_resps[0].output_transcription.finished is True + assert not tx_resps[0].output_transcription.text From 45c4a3bacb57be0b7b9771951ac1e19ff02f36cd Mon Sep 17 00:00:00 2001 From: Hangfei Lin Date: Tue, 4 Nov 2025 19:07:31 -0800 Subject: [PATCH 3/6] Add mock_receive_generator for testing --- tests/unittests/models/test_gemini_llm_connection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unittests/models/test_gemini_llm_connection.py b/tests/unittests/models/test_gemini_llm_connection.py index c8ada49024..657f028c05 100644 --- a/tests/unittests/models/test_gemini_llm_connection.py +++ b/tests/unittests/models/test_gemini_llm_connection.py @@ -196,6 +196,7 @@ async def test_receive_usage_metadata_and_server_content( mock_message.tool_call = None mock_message.session_resumption_update = None + async def mock_receive_generator(): yield mock_message From 3c2eaf67c114862e7f753a0ce10be074bb1058bc Mon Sep 17 00:00:00 2001 From: Hangfei Lin Date: Tue, 4 Nov 2025 19:07:52 -0800 Subject: [PATCH 4/6] Add test for usage metadata and server content Added a new test for receiving usage metadata and server content. --- tests/unittests/models/test_gemini_llm_connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittests/models/test_gemini_llm_connection.py b/tests/unittests/models/test_gemini_llm_connection.py index 657f028c05..9c25754431 100644 --- a/tests/unittests/models/test_gemini_llm_connection.py +++ b/tests/unittests/models/test_gemini_llm_connection.py @@ -160,6 +160,7 @@ async def gen(): assert tx_resps[0].output_transcription.finished is True assert not tx_resps[0].output_transcription.text + async def test_receive_usage_metadata_and_server_content( gemini_connection, mock_gemini_session ): @@ -196,7 +197,6 @@ async def test_receive_usage_metadata_and_server_content( mock_message.tool_call = None mock_message.session_resumption_update = None - async def mock_receive_generator(): yield mock_message From e7b6fa7ad1aa37b799b511caa1129e1727af55df Mon Sep 17 00:00:00 2001 From: Krzysztof Czuszynski Date: Wed, 5 Nov 2025 14:27:49 +0100 Subject: [PATCH 5/6] add simplified mocked msg and assertion logic Refactored Msg class to use a single mock instance and simplified transcription response checks. --- .../models/test_gemini_llm_connection.py | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/tests/unittests/models/test_gemini_llm_connection.py b/tests/unittests/models/test_gemini_llm_connection.py index 9c25754431..b865728173 100644 --- a/tests/unittests/models/test_gemini_llm_connection.py +++ b/tests/unittests/models/test_gemini_llm_connection.py @@ -120,25 +120,21 @@ async def test_receive_transcript_finished( finished_tx = types.Transcription(finished=True) - class Msg: - - def __init__(self): - self.server_content = mock.Mock() - sc = self.server_content - sc.model_turn = None - if tx_direction == 'input': - sc.input_transcription = finished_tx - sc.output_transcription = None - else: - sc.input_transcription = None - sc.output_transcription = finished_tx - sc.interrupted = False - sc.turn_complete = False - self.tool_call = None - self.session_resumption_update = None + msg = mock.Mock() + msg.tool_call = None + msg.session_resumption_update = None + msg.server_content.model_turn = None + msg.server_content.interrupted = False + msg.server_content.turn_complete = False + msg.server_content.input_transcription = ( + finished_tx if tx_direction == 'input' else None + ) + msg.server_content.output_transcription = ( + finished_tx if tx_direction == 'output' else None + ) async def gen(): - yield Msg() + yield msg mock_gemini_session.receive = mock.Mock(return_value=gen()) @@ -146,19 +142,13 @@ async def gen(): async for r in gemini_connection.receive(): responses.append(r) - if tx_direction == 'input': - tx_resps = [r for r in responses if r.input_transcription] - else: - tx_resps = [r for r in responses if r.output_transcription] - - if tx_direction == 'input': - assert tx_resps, 'Excpected input transcription response' - assert tx_resps[0].input_transcription.finished is True - assert not tx_resps[0].input_transcription.text - else: - assert tx_resps, 'Expected output transcription response' - assert tx_resps[0].output_transcription.finished is True - assert not tx_resps[0].output_transcription.text + attr_name = f'{tx_direction}_transcription' + tx_resps = [r for r in responses if getattr(r, attr_name)] + assert tx_resps, f'Expected {tx_direction} transcription response' + + transcription = getattr(tx_resps[0], attr_name) + assert transcription.finished is True + assert not transcription.text async def test_receive_usage_metadata_and_server_content( From d9aee2af4e102b4769cfeaa5945e458639f744f6 Mon Sep 17 00:00:00 2001 From: Krzysztof Czuszynski Date: Wed, 5 Nov 2025 14:39:32 +0100 Subject: [PATCH 6/6] update mock msg with usage_metadata --- tests/unittests/models/test_gemini_llm_connection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unittests/models/test_gemini_llm_connection.py b/tests/unittests/models/test_gemini_llm_connection.py index b865728173..23e8697fdd 100644 --- a/tests/unittests/models/test_gemini_llm_connection.py +++ b/tests/unittests/models/test_gemini_llm_connection.py @@ -122,6 +122,7 @@ async def test_receive_transcript_finished( msg = mock.Mock() msg.tool_call = None + msg.usage_metadata = None msg.session_resumption_update = None msg.server_content.model_turn = None msg.server_content.interrupted = False