mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
Merge bf7e630af3 into d1773d96e9
This commit is contained in:
commit
f1d69643bd
3 changed files with 84 additions and 1 deletions
|
|
@ -52,6 +52,7 @@ from litellm.types.llms.vertex_ai import (
|
|||
GeminiResponseModalities,
|
||||
HttpxBlobType,
|
||||
HttpxContentType,
|
||||
TranslationConfig,
|
||||
)
|
||||
from litellm.types.realtime import (
|
||||
ALL_DELTA_TYPES,
|
||||
|
|
@ -98,6 +99,31 @@ def _gemini_live_speech_config(voice: object) -> Mapping[str, object] | None:
|
|||
return VertexGeminiConfig()._map_audio_params({"voice": voice})
|
||||
|
||||
|
||||
class _TranslationGenerationConfig(TypedDict, total=False):
|
||||
translationConfig: ReadOnly[TranslationConfig]
|
||||
|
||||
|
||||
def _translation_generation_config(value: object) -> _TranslationGenerationConfig:
|
||||
empty: Final[_TranslationGenerationConfig] = {}
|
||||
if not isinstance(value, Mapping):
|
||||
return empty
|
||||
target: Final = value.get("target_language_code", value.get("targetLanguageCode"))
|
||||
if not isinstance(target, str) or not target:
|
||||
verbose_logger.warning(
|
||||
"Gemini Realtime: translation_config without target_language_code dropped; "
|
||||
"live-translate models stream silence without one."
|
||||
)
|
||||
return empty
|
||||
echo: Final = value.get("echo_target_language", value.get("echoTargetLanguage"))
|
||||
if isinstance(echo, bool):
|
||||
with_echo: Final[_TranslationGenerationConfig] = {
|
||||
"translationConfig": TranslationConfig(targetLanguageCode=target, echoTargetLanguage=echo)
|
||||
}
|
||||
return with_echo
|
||||
config: Final[_TranslationGenerationConfig] = {"translationConfig": TranslationConfig(targetLanguageCode=target)}
|
||||
return config
|
||||
|
||||
|
||||
class _GeminiLiveSetupEnvelope(TypedDict, total=False):
|
||||
setup: ReadOnly[BidiGenerateContentSetup]
|
||||
|
||||
|
|
@ -308,6 +334,7 @@ class GeminiRealtimeConfig(BaseRealtimeConfig):
|
|||
"input_audio_transcription",
|
||||
"turn_detection",
|
||||
"voice",
|
||||
"translation_config",
|
||||
]
|
||||
|
||||
def map_openai_params(self, optional_params: dict, non_default_params: dict) -> dict:
|
||||
|
|
@ -356,6 +383,9 @@ class GeminiRealtimeConfig(BaseRealtimeConfig):
|
|||
speech_config = _gemini_live_speech_config(value)
|
||||
if speech_config:
|
||||
optional_params["generationConfig"]["speechConfig"] = speech_config
|
||||
optional_params["generationConfig"].update(
|
||||
_translation_generation_config(non_default_params.get("translation_config"))
|
||||
)
|
||||
if len(optional_params["generationConfig"]) == 0:
|
||||
optional_params.pop("generationConfig")
|
||||
return optional_params
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from enum import Enum
|
|||
from typing import Any, Final, Literal, Protocol
|
||||
|
||||
from typing_extensions import (
|
||||
ReadOnly,
|
||||
Required,
|
||||
TypedDict,
|
||||
)
|
||||
|
|
@ -231,6 +232,11 @@ class SpeechConfig(TypedDict, total=False):
|
|||
languageCode: str
|
||||
|
||||
|
||||
class TranslationConfig(TypedDict, total=False):
|
||||
targetLanguageCode: ReadOnly[str]
|
||||
echoTargetLanguage: ReadOnly[bool]
|
||||
|
||||
|
||||
class GenerationConfig(TypedDict, total=False):
|
||||
temperature: float
|
||||
top_p: float
|
||||
|
|
@ -252,6 +258,7 @@ class GenerationConfig(TypedDict, total=False):
|
|||
thinkingConfig: GeminiThinkingConfig
|
||||
mediaResolution: str
|
||||
speechConfig: SpeechConfig
|
||||
translationConfig: ReadOnly[TranslationConfig]
|
||||
|
||||
|
||||
class VertexToolName(str, Enum):
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import json
|
||||
from collections.abc import Mapping
|
||||
from typing import cast
|
||||
from typing import Final, cast
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
|
@ -2201,3 +2201,49 @@ def test_gemini_realtime_response_done_reports_no_grounding_when_none_ran():
|
|||
|
||||
assert input_details.get("web_search_requests") is None
|
||||
assert input_details.get("google_maps_grounding_requests") is None
|
||||
|
||||
|
||||
def test_map_openai_params_translation_config_snake_and_camel():
|
||||
"""``session.translation_config`` reaches ``generationConfig.translationConfig``
|
||||
whether the client spells the keys OpenAI-style or Gemini-style."""
|
||||
cfg: Final = GeminiRealtimeConfig()
|
||||
|
||||
snake: Final = cfg.map_openai_params(
|
||||
optional_params={},
|
||||
non_default_params={"translation_config": {"target_language_code": "ko", "echo_target_language": True}},
|
||||
)
|
||||
assert snake["generationConfig"]["translationConfig"] == {"targetLanguageCode": "ko", "echoTargetLanguage": True}
|
||||
|
||||
camel: Final = cfg.map_openai_params(
|
||||
optional_params={},
|
||||
non_default_params={"translation_config": {"targetLanguageCode": "pl"}},
|
||||
)
|
||||
assert camel["generationConfig"]["translationConfig"] == {"targetLanguageCode": "pl"}
|
||||
|
||||
|
||||
def test_map_openai_params_translation_config_requires_target_language():
|
||||
"""Without a target language the live-translate models stream silence, so an
|
||||
incomplete config is dropped rather than forwarded."""
|
||||
cfg: Final = GeminiRealtimeConfig()
|
||||
|
||||
for bad in ({}, {"echo_target_language": True}, {"target_language_code": ""}, "ko", None):
|
||||
mapped: Final = cfg.map_openai_params(optional_params={}, non_default_params={"translation_config": bad})
|
||||
assert "translationConfig" not in mapped.get("generationConfig", {})
|
||||
|
||||
|
||||
def test_gemini_session_update_forwards_translation_config_in_setup():
|
||||
"""End to end: the first session.update carries translationConfig into the Gemini setup."""
|
||||
config: Final = GeminiRealtimeConfig()
|
||||
session_update: Final = {
|
||||
"type": "session.update",
|
||||
"session": {"modalities": ["audio"], "translation_config": {"target_language_code": "ko"}},
|
||||
}
|
||||
|
||||
messages: Final = config.transform_realtime_request(
|
||||
json.dumps(session_update), "gemini-3.5-live-translate-preview", session_configuration_request=None
|
||||
)
|
||||
|
||||
setup: Final = json.loads(messages[0])["setup"]
|
||||
assert setup["model"] == "models/gemini-3.5-live-translate-preview"
|
||||
assert setup["generationConfig"]["translationConfig"] == {"targetLanguageCode": "ko"}
|
||||
assert setup["generationConfig"]["responseModalities"] == ["AUDIO"]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue