Skip to content

Commit ff153a7

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Add translation_config SDK support for GEAP.
PiperOrigin-RevId: 973837432
1 parent df0ab55 commit ff153a7

4 files changed

Lines changed: 108 additions & 24 deletions

File tree

‎google/genai/_live_converters.py‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,9 @@ def _LiveConnectConfig_to_mldev(
969969
setv(
970970
parent_object,
971971
['setup', 'generationConfig', 'translationConfig'],
972-
getv(from_object, ['translation_config']),
972+
_TranslationConfig_to_mldev(
973+
getv(from_object, ['translation_config']), to_object
974+
),
973975
)
974976

975977
return to_object
@@ -1154,9 +1156,12 @@ def _LiveConnectConfig_to_vertex(
11541156
)
11551157

11561158
if getv(from_object, ['translation_config']) is not None:
1157-
raise ValueError(
1158-
'translation_config parameter is only supported in Gemini Developer API'
1159-
' mode, not in Gemini Enterprise Agent Platform mode.'
1159+
setv(
1160+
parent_object,
1161+
['setup', 'generationConfig', 'translationConfig'],
1162+
_TranslationConfig_to_vertex(
1163+
getv(from_object, ['translation_config']), to_object
1164+
),
11601165
)
11611166

11621167
return to_object
@@ -1995,6 +2000,50 @@ def _Tool_to_vertex(
19952000
return to_object
19962001

19972002

2003+
def _TranslationConfig_to_mldev(
2004+
from_object: Union[dict[str, Any], object],
2005+
parent_object: Optional[dict[str, Any]] = None,
2006+
) -> dict[str, Any]:
2007+
to_object: dict[str, Any] = {}
2008+
if getv(from_object, ['echo_target_language']) is not None:
2009+
setv(
2010+
to_object,
2011+
['echoTargetLanguage'],
2012+
getv(from_object, ['echo_target_language']),
2013+
)
2014+
2015+
if getv(from_object, ['target_language_code']) is not None:
2016+
setv(
2017+
to_object,
2018+
['targetLanguageCode'],
2019+
getv(from_object, ['target_language_code']),
2020+
)
2021+
2022+
return to_object
2023+
2024+
2025+
def _TranslationConfig_to_vertex(
2026+
from_object: Union[dict[str, Any], object],
2027+
parent_object: Optional[dict[str, Any]] = None,
2028+
) -> dict[str, Any]:
2029+
to_object: dict[str, Any] = {}
2030+
if getv(from_object, ['echo_target_language']) is not None:
2031+
setv(
2032+
to_object,
2033+
['echoTargetLanguage'],
2034+
getv(from_object, ['echo_target_language']),
2035+
)
2036+
2037+
if getv(from_object, ['target_language_code']) is not None:
2038+
setv(
2039+
to_object,
2040+
['targetLanguageCode'],
2041+
getv(from_object, ['target_language_code']),
2042+
)
2043+
2044+
return to_object
2045+
2046+
19982047
def _UsageMetadata_from_vertex(
19992048
from_object: Union[dict[str, Any], object],
20002049
parent_object: Optional[dict[str, Any]] = None,

‎google/genai/_tokens_converters.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ def _LiveConnectConfig_to_mldev(
466466
setv(
467467
parent_object,
468468
['setup', 'generationConfig', 'translationConfig'],
469-
getv(from_object, ['translation_config']),
469+
_TranslationConfig_to_mldev(
470+
getv(from_object, ['translation_config']), to_object
471+
),
470472
)
471473

472474
return to_object
@@ -699,3 +701,25 @@ def _Tool_to_mldev(
699701
)
700702

701703
return to_object
704+
705+
706+
def _TranslationConfig_to_mldev(
707+
from_object: Union[dict[str, Any], object],
708+
parent_object: Optional[dict[str, Any]] = None,
709+
) -> dict[str, Any]:
710+
to_object: dict[str, Any] = {}
711+
if getv(from_object, ['echo_target_language']) is not None:
712+
setv(
713+
to_object,
714+
['echoTargetLanguage'],
715+
getv(from_object, ['echo_target_language']),
716+
)
717+
718+
if getv(from_object, ['target_language_code']) is not None:
719+
setv(
720+
to_object,
721+
['targetLanguageCode'],
722+
getv(from_object, ['target_language_code']),
723+
)
724+
725+
return to_object

‎google/genai/tests/live/test_live.py‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,24 +1624,38 @@ async def test_bidi_setup_to_api_with_translation_config(vertexai):
16241624
},
16251625
}
16261626

1627-
with pytest_helper.exception_if_vertex(api_client, ValueError):
1628-
result = await get_connect_message(
1629-
api_client=api_client, model='test_model', config=config_dict
1630-
)
1627+
result = await get_connect_message(
1628+
api_client=api_client, model='test_model', config=config_dict
1629+
)
16311630

1632-
if not vertexai:
1631+
if vertexai:
1632+
expected_result = {
1633+
'setup': {
1634+
'model': (
1635+
'projects/test_project/locations/us-central1/publishers/google/models/test_model'
1636+
),
1637+
'generationConfig': {
1638+
'responseModalities': ['AUDIO'],
1639+
'translationConfig': {
1640+
'echoTargetLanguage': True,
1641+
'targetLanguageCode': 'es',
1642+
},
1643+
},
1644+
}
1645+
}
1646+
else:
16331647
expected_result = {
16341648
'setup': {
16351649
'model': 'models/test_model',
16361650
'generationConfig': {
16371651
'translationConfig': {
1638-
'echo_target_language': True,
1639-
'target_language_code': 'es',
1652+
'echoTargetLanguage': True,
1653+
'targetLanguageCode': 'es',
16401654
},
16411655
},
16421656
}
16431657
}
1644-
assert result == expected_result
1658+
assert result == expected_result
16451659

16461660
# Test 2: Config defined using types.LiveConnectConfig.
16471661
config = types.LiveConnectConfig(
@@ -1651,13 +1665,11 @@ async def test_bidi_setup_to_api_with_translation_config(vertexai):
16511665
)
16521666
)
16531667

1654-
with pytest_helper.exception_if_vertex(api_client, ValueError):
1655-
result = await get_connect_message(
1656-
api_client=api_client, model='test_model', config=config
1657-
)
1668+
result = await get_connect_message(
1669+
api_client=api_client, model='test_model', config=config
1670+
)
16581671

1659-
if not vertexai:
1660-
assert result == expected_result
1672+
assert result == expected_result
16611673

16621674

16631675
@pytest.mark.parametrize('vertexai', [True, False])
@@ -2303,4 +2315,3 @@ async def test_bidi_setup_replicated_voice_config_with_consent(vertexai):
23032315
replicated_sig['voice_consent_signature'].get('signature')
23042316
== 'test_sig_abc123'
23052317
)
2306-

‎google/genai/types.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11395,22 +11395,22 @@ class TranslationConfig(_common.BaseModel):
1139511395

1139611396
echo_target_language: Optional[bool] = Field(
1139711397
default=None,
11398-
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.""",
11398+
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.""",
1139911399
)
1140011400
target_language_code: Optional[str] = Field(
1140111401
default=None,
11402-
description="""Required. The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr").""",
11402+
description="""The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr").""",
1140311403
)
1140411404

1140511405

1140611406
class TranslationConfigDict(TypedDict, total=False):
1140711407
"""Config for stream translation."""
1140811408

1140911409
echo_target_language: Optional[bool]
11410-
"""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."""
11410+
"""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."""
1141111411

1141211412
target_language_code: Optional[str]
11413-
"""Required. The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr")."""
11413+
"""The target language for translation. Supported values are BCP-47 language codes (e.g. "en", "es", "fr")."""
1141411414

1141511415

1141611416
TranslationConfigOrDict = Union[TranslationConfig, TranslationConfigDict]

0 commit comments

Comments
 (0)