-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix/ensure correct transcript finished #3871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e8864b1
5901190
ce4e269
38cc1c2
c0f9d62
ed863a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -593,3 +593,109 @@ async def mock_receive_generator(): | |
| assert responses[2].output_transcription.text == 'How can I help?' | ||
| assert responses[2].output_transcription.finished is True | ||
| assert responses[2].partial is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize('tx_direction', ['input', 'output']) | ||
| @pytest.mark.parametrize( | ||
| 'fragments', | ||
| [ | ||
| ('That', "'s great", "That's great"), | ||
| ("That'", 's great', "That's great"), | ||
| ("That's", 'great', "That's great"), | ||
| ("That's", ' great', "That's great"), | ||
| ("That's ", 'great', "That's great"), | ||
| ("Great", '! Good to hear', 'Great! Good to hear'), | ||
| ("Great!", 'Good to hear', 'Great! Good to hear'), | ||
| ("Great! ", 'Good to hear', 'Great! Good to hear'), | ||
| ("Great! Good", 'to hear', 'Great! Good to hear'), | ||
| ("Great! Good ", 'to hear', 'Great! Good to hear'), | ||
| ("Great! Good", ' to hear', 'Great! Good to hear'), | ||
| ], | ||
| ) | ||
| async def test_receive_final_transcription_space_between_fragments( | ||
| gemini_connection, mock_gemini_session, tx_direction, fragments | ||
| ): | ||
| """Test receive final transcription fragments are joined with a space between words.""" | ||
| fragment1, fragment2, expected = fragments | ||
|
|
||
| message1 = mock.Mock() | ||
| message1.usage_metadata = None | ||
| message1.server_content = mock.Mock() | ||
| message1.server_content.model_turn = None | ||
| message1.server_content.interrupted = False | ||
| message1.server_content.turn_complete = False | ||
| message1.server_content.generation_complete = False | ||
| message1.tool_call = None | ||
| message1.session_resumption_update = None | ||
| message1.server_content.input_transcription = ( | ||
| types.Transcription(text=fragment1, finished=False) | ||
| if tx_direction == 'input' | ||
| else None | ||
| ) | ||
| message1.server_content.output_transcription = ( | ||
| types.Transcription(text=fragment1, finished=False) | ||
| if tx_direction == 'output' | ||
| else None | ||
| ) | ||
|
|
||
| message2 = mock.Mock() | ||
| message2.usage_metadata = None | ||
| message2.server_content = mock.Mock() | ||
| message2.server_content.model_turn = None | ||
| message2.server_content.interrupted = False | ||
| message2.server_content.turn_complete = False | ||
| message2.server_content.generation_complete = False | ||
| message2.tool_call = None | ||
| message2.session_resumption_update = None | ||
| message2.server_content.input_transcription = ( | ||
| types.Transcription(text=fragment2, finished=False) | ||
| if tx_direction == 'input' | ||
| else None | ||
| ) | ||
| message2.server_content.output_transcription = ( | ||
| types.Transcription(text=fragment2, finished=False) | ||
| if tx_direction == 'output' | ||
| else None | ||
| ) | ||
|
|
||
| message3 = mock.Mock() | ||
| message3.usage_metadata = None | ||
| message3.server_content = mock.Mock() | ||
| message3.server_content.model_turn = None | ||
| message3.server_content.interrupted = False | ||
| message3.server_content.turn_complete = False | ||
| message3.server_content.generation_complete = False | ||
| message3.tool_call = None | ||
| message3.session_resumption_update = None | ||
| message3.server_content.input_transcription = ( | ||
| types.Transcription(text=None, finished=True) | ||
| if tx_direction == 'input' | ||
| else None | ||
| ) | ||
| message3.server_content.output_transcription = ( | ||
| types.Transcription(text=None, finished=True) | ||
| if tx_direction == 'output' | ||
| else None | ||
| ) | ||
|
Comment on lines
+622
to
+680
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The setup for For example, you could define a helper within the test file: def _create_mock_transcription_message(text: str | None, finished: bool, direction: str) -> mock.Mock:
msg = mock.Mock()
msg.usage_metadata = None
msg.server_content = mock.Mock()
msg.server_content.model_turn = None
msg.server_content.interrupted = False
msg.server_content.turn_complete = False
msg.server_content.generation_complete = False
msg.tool_call = None
msg.session_resumption_update = None
transcription = types.Transcription(text=text, finished=finished)
if direction == 'input':
msg.server_content.input_transcription = transcription
msg.server_content.output_transcription = None
else:
msg.server_content.input_transcription = None
msg.server_content.output_transcription = transcription
return msgThen, you could simplify the test setup significantly: message1 = _create_mock_transcription_message(fragment1, False, tx_direction)
message2 = _create_mock_transcription_message(fragment2, False, tx_direction)
message3 = _create_mock_transcription_message(None, True, tx_direction) |
||
|
|
||
| async def mock_receive_generator(): | ||
| yield message1 | ||
| yield message2 | ||
| yield message3 | ||
|
|
||
| receive_mock = mock.Mock(return_value=mock_receive_generator()) | ||
| mock_gemini_session.receive = receive_mock | ||
|
|
||
| responses = [resp async for resp in gemini_connection.receive()] | ||
|
|
||
| # find the finished transcription response | ||
| attr_name = f'{tx_direction}_transcription' | ||
| finished_resps = [ | ||
| r | ||
| for r in responses | ||
| if getattr(r, attr_name) and getattr(r, attr_name).finished | ||
| ] | ||
| assert finished_resps, 'Expected finished transcription response' | ||
| transcription = getattr(finished_resps[0], attr_name) | ||
| assert transcription.text == expected | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for stitching transcription chunks is duplicated for both
input_transcription(here) andoutput_transcription(lines 226-240). To improve maintainability and adhere to the Don't Repeat Yourself (DRY) principle, this logic should be extracted into a private helper method.For example, you could create a method like this:
Then you could call it like so:
self._input_transcription_text = self._stitch_transcription_chunk(self._input_transcription_text, new_input_transcription_chunk)