From 47753147a30c10138f1a4e46c1c8e8069ae8e86d Mon Sep 17 00:00:00 2001 From: Mark Daoust Date: Tue, 2 Jun 2026 12:24:00 -0700 Subject: [PATCH] feat: Add TranslationConfig for live translation. PiperOrigin-RevId: 925512170 --- google/genai/_live_converters.py | 12 ++++++------ google/genai/_tokens_converters.py | 6 +++--- google/genai/tests/live/test_live.py | 8 ++++---- google/genai/tests/tokens/test_create.py | 19 +++++++++++++++++++ google/genai/types.py | 16 +++++++--------- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/google/genai/_live_converters.py b/google/genai/_live_converters.py index 0bffd0ec9..64d4267df 100644 --- a/google/genai/_live_converters.py +++ b/google/genai/_live_converters.py @@ -973,11 +973,11 @@ def _LiveConnectConfig_to_mldev( ], ) - if getv(from_object, ['stream_translation_config']) is not None: + if getv(from_object, ['translation_config']) is not None: setv( parent_object, - ['setup', 'generationConfig', 'streamTranslationConfig'], - getv(from_object, ['stream_translation_config']), + ['setup', 'generationConfig', 'translationConfig'], + getv(from_object, ['translation_config']), ) return to_object @@ -1160,10 +1160,10 @@ def _LiveConnectConfig_to_vertex( [item for item in getv(from_object, ['safety_settings'])], ) - if getv(from_object, ['stream_translation_config']) is not None: + if getv(from_object, ['translation_config']) is not None: raise ValueError( - 'stream_translation_config parameter is only supported in Gemini' - ' Developer API mode, not in Gemini Enterprise Agent Platform mode.' + 'translation_config parameter is only supported in Gemini Developer API' + ' mode, not in Gemini Enterprise Agent Platform mode.' ) return to_object diff --git a/google/genai/_tokens_converters.py b/google/genai/_tokens_converters.py index a22306cfd..580058ab9 100644 --- a/google/genai/_tokens_converters.py +++ b/google/genai/_tokens_converters.py @@ -474,11 +474,11 @@ def _LiveConnectConfig_to_mldev( ], ) - if getv(from_object, ['stream_translation_config']) is not None: + if getv(from_object, ['translation_config']) is not None: setv( parent_object, - ['setup', 'generationConfig', 'streamTranslationConfig'], - getv(from_object, ['stream_translation_config']), + ['setup', 'generationConfig', 'translationConfig'], + getv(from_object, ['translation_config']), ) return to_object diff --git a/google/genai/tests/live/test_live.py b/google/genai/tests/live/test_live.py index 9298f2553..046b08a0a 100644 --- a/google/genai/tests/live/test_live.py +++ b/google/genai/tests/live/test_live.py @@ -1612,12 +1612,12 @@ async def test_bidi_setup_to_api_with_transparent_session_resumption(vertexai): @pytest.mark.parametrize('vertexai', [True, False]) @pytest.mark.asyncio -async def test_bidi_setup_to_api_with_stream_translation_config(vertexai): +async def test_bidi_setup_to_api_with_translation_config(vertexai): api_client = mock_api_client(vertexai=vertexai) # Test 1: Config defined using dict representation. config_dict = { - 'stream_translation_config': { + 'translation_config': { 'echo_target_language': True, 'target_language_code': 'es', }, @@ -1633,7 +1633,7 @@ async def test_bidi_setup_to_api_with_stream_translation_config(vertexai): 'setup': { 'model': 'models/test_model', 'generationConfig': { - 'streamTranslationConfig': { + 'translationConfig': { 'echo_target_language': True, 'target_language_code': 'es', }, @@ -1644,7 +1644,7 @@ async def test_bidi_setup_to_api_with_stream_translation_config(vertexai): # Test 2: Config defined using types.LiveConnectConfig. config = types.LiveConnectConfig( - stream_translation_config=types.StreamTranslationConfig( + translation_config=types.TranslationConfig( echo_target_language=True, target_language_code='es', ) diff --git a/google/genai/tests/tokens/test_create.py b/google/genai/tests/tokens/test_create.py index 4b7abca27..6129209c6 100644 --- a/google/genai/tests/tokens/test_create.py +++ b/google/genai/tests/tokens/test_create.py @@ -53,6 +53,25 @@ ), exception_if_vertex='only supported in the Gemini Developer client', ), + pytest_helper.TestTableItem( + name='test_create_with_streaming_translation_config', + parameters=types.CreateAuthTokenParameters( + config={ + 'http_options': {'api_version': 'v1alpha'}, + 'uses': 2, + 'live_connect_constraints': { + 'model': _MODEL, + 'config': { + 'translation_config': { + 'target_language_code': 'es', + 'echo_target_language': True, + }, + }, + }, + }, + ), + exception_if_vertex='only supported in the Gemini Developer client', + ), pytest_helper.TestTableItem( name='test_create_lock_non_null_fields', parameters=types.CreateAuthTokenParameters( diff --git a/google/genai/types.py b/google/genai/types.py index 708519471..57e41991b 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -19801,7 +19801,7 @@ class RealtimeInputConfigDict(TypedDict, total=False): RealtimeInputConfigOrDict = Union[RealtimeInputConfig, RealtimeInputConfigDict] -class StreamTranslationConfig(_common.BaseModel): +class TranslationConfig(_common.BaseModel): """Config for stream translation.""" echo_target_language: Optional[bool] = Field( @@ -19817,7 +19817,7 @@ class StreamTranslationConfig(_common.BaseModel): ) -class StreamTranslationConfigDict(TypedDict, total=False): +class TranslationConfigDict(TypedDict, total=False): """Config for stream translation.""" echo_target_language: Optional[bool] @@ -19830,9 +19830,7 @@ class StreamTranslationConfigDict(TypedDict, total=False): language codes (e.g. "en", "es", "fr").""" -StreamTranslationConfigOrDict = Union[ - StreamTranslationConfig, StreamTranslationConfigDict -] +TranslationConfigOrDict = Union[TranslationConfig, TranslationConfigDict] class LiveConnectConfig(_common.BaseModel): @@ -19973,8 +19971,8 @@ class LiveConnectConfig(_common.BaseModel): response. """, ) - stream_translation_config: Optional[StreamTranslationConfig] = Field( - default=None, description="""Config for stream translation.""" + translation_config: Optional[TranslationConfig] = Field( + default=None, description="""Config for translation.""" ) @@ -20094,8 +20092,8 @@ class LiveConnectConfigDict(TypedDict, total=False): response. """ - stream_translation_config: Optional[StreamTranslationConfigDict] - """Config for stream translation.""" + translation_config: Optional[TranslationConfigDict] + """Config for translation.""" LiveConnectConfigOrDict = Union[LiveConnectConfig, LiveConnectConfigDict]