Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions google/genai/_live_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 25 additions & 1 deletion google/genai/_tokens_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
41 changes: 26 additions & 15 deletions google/genai/tests/live/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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])
Expand Down Expand Up @@ -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'
)

8 changes: 4 additions & 4 deletions google/genai/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11395,22 +11395,22 @@ 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").""",
)


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]
Expand Down
Loading