diff --git a/google/genai/_live_converters.py b/google/genai/_live_converters.py index 5d6648a99..b53370506 100644 --- a/google/genai/_live_converters.py +++ b/google/genai/_live_converters.py @@ -969,7 +969,9 @@ def _LiveConnectConfig_to_mldev( setv( parent_object, ['setup', 'generationConfig', 'translationConfig'], - getv(from_object, ['translation_config']), + _TranslationConfig_to_mldev( + getv(from_object, ['translation_config']), to_object + ), ) return to_object @@ -1154,9 +1156,12 @@ def _LiveConnectConfig_to_vertex( ) if getv(from_object, ['translation_config']) is not None: - raise ValueError( - 'translation_config parameter is only supported in Gemini Developer API' - ' mode, not in Gemini Enterprise Agent Platform mode.' + setv( + parent_object, + ['setup', 'generationConfig', 'translationConfig'], + _TranslationConfig_to_vertex( + getv(from_object, ['translation_config']), to_object + ), ) return to_object @@ -1995,6 +2000,50 @@ def _Tool_to_vertex( return to_object +def _TranslationConfig_to_mldev( + from_object: Union[dict[str, Any], object], + parent_object: Optional[dict[str, Any]] = None, +) -> dict[str, Any]: + to_object: dict[str, Any] = {} + if getv(from_object, ['echo_target_language']) is not None: + setv( + to_object, + ['echoTargetLanguage'], + getv(from_object, ['echo_target_language']), + ) + + if getv(from_object, ['target_language_code']) is not None: + setv( + to_object, + ['targetLanguageCode'], + getv(from_object, ['target_language_code']), + ) + + return to_object + + +def _TranslationConfig_to_vertex( + from_object: Union[dict[str, Any], object], + parent_object: Optional[dict[str, Any]] = None, +) -> dict[str, Any]: + to_object: dict[str, Any] = {} + if getv(from_object, ['echo_target_language']) is not None: + setv( + to_object, + ['echoTargetLanguage'], + getv(from_object, ['echo_target_language']), + ) + + if getv(from_object, ['target_language_code']) is not None: + setv( + to_object, + ['targetLanguageCode'], + getv(from_object, ['target_language_code']), + ) + + return to_object + + def _UsageMetadata_from_vertex( from_object: Union[dict[str, Any], object], parent_object: Optional[dict[str, Any]] = None, diff --git a/google/genai/_tokens_converters.py b/google/genai/_tokens_converters.py index a331e0c12..045c1b4a9 100644 --- a/google/genai/_tokens_converters.py +++ b/google/genai/_tokens_converters.py @@ -466,7 +466,9 @@ def _LiveConnectConfig_to_mldev( setv( parent_object, ['setup', 'generationConfig', 'translationConfig'], - getv(from_object, ['translation_config']), + _TranslationConfig_to_mldev( + getv(from_object, ['translation_config']), to_object + ), ) return to_object @@ -699,3 +701,25 @@ def _Tool_to_mldev( ) return to_object + + +def _TranslationConfig_to_mldev( + from_object: Union[dict[str, Any], object], + parent_object: Optional[dict[str, Any]] = None, +) -> dict[str, Any]: + to_object: dict[str, Any] = {} + if getv(from_object, ['echo_target_language']) is not None: + setv( + to_object, + ['echoTargetLanguage'], + getv(from_object, ['echo_target_language']), + ) + + if getv(from_object, ['target_language_code']) is not None: + setv( + to_object, + ['targetLanguageCode'], + getv(from_object, ['target_language_code']), + ) + + return to_object diff --git a/google/genai/tests/live/test_live.py b/google/genai/tests/live/test_live.py index 2a69b61e3..233bd7aa7 100644 --- a/google/genai/tests/live/test_live.py +++ b/google/genai/tests/live/test_live.py @@ -1624,24 +1624,38 @@ async def test_bidi_setup_to_api_with_translation_config(vertexai): }, } - with pytest_helper.exception_if_vertex(api_client, ValueError): - result = await get_connect_message( - api_client=api_client, model='test_model', config=config_dict - ) + result = await get_connect_message( + api_client=api_client, model='test_model', config=config_dict + ) - if not vertexai: + if vertexai: + expected_result = { + 'setup': { + 'model': ( + 'projects/test_project/locations/us-central1/publishers/google/models/test_model' + ), + 'generationConfig': { + 'responseModalities': ['AUDIO'], + 'translationConfig': { + 'echoTargetLanguage': True, + 'targetLanguageCode': 'es', + }, + }, + } + } + else: expected_result = { 'setup': { 'model': 'models/test_model', 'generationConfig': { 'translationConfig': { - 'echo_target_language': True, - 'target_language_code': 'es', + 'echoTargetLanguage': True, + 'targetLanguageCode': 'es', }, }, } } - assert result == expected_result + assert result == expected_result # Test 2: Config defined using types.LiveConnectConfig. config = types.LiveConnectConfig( @@ -1651,13 +1665,11 @@ async def test_bidi_setup_to_api_with_translation_config(vertexai): ) ) - with pytest_helper.exception_if_vertex(api_client, ValueError): - result = await get_connect_message( - api_client=api_client, model='test_model', config=config - ) + result = await get_connect_message( + api_client=api_client, model='test_model', config=config + ) - if not vertexai: - assert result == expected_result + assert result == expected_result @pytest.mark.parametrize('vertexai', [True, False]) @@ -2303,4 +2315,3 @@ async def test_bidi_setup_replicated_voice_config_with_consent(vertexai): replicated_sig['voice_consent_signature'].get('signature') == 'test_sig_abc123' ) - diff --git a/google/genai/types.py b/google/genai/types.py index 87aec0a50..24d739ee8 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -11395,11 +11395,11 @@ class TranslationConfig(_common.BaseModel): echo_target_language: Optional[bool] = Field( default=None, - description="""Optional. If true, the model will generate audio when the target language is spoken, essentially it will parrot the input. If false, we will not produce audio for the target language.""", + description="""If true, the model will generate audio when the target language is spoken, essentially it will parrot the input. If false, we will not produce audio for the target language.""", ) target_language_code: Optional[str] = Field( default=None, - description="""Required. The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr").""", + description="""The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr").""", ) @@ -11407,10 +11407,10 @@ class TranslationConfigDict(TypedDict, total=False): """Config for stream translation.""" echo_target_language: Optional[bool] - """Optional. If true, the model will generate audio when the target language is spoken, essentially it will parrot the input. If false, we will not produce audio for the target language.""" + """If true, the model will generate audio when the target language is spoken, essentially it will parrot the input. If false, we will not produce audio for the target language.""" target_language_code: Optional[str] - """Required. The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr").""" + """The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr").""" TranslationConfigOrDict = Union[TranslationConfig, TranslationConfigDict]