Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(gemini-realtime): drop OpenAI stock voice names instead of pairing them to Gemini voices

Yucheng Zhu27207659f8db
diff --git a/litellm/llms/gemini/realtime/transformation.py b/litellm/llms/gemini/realtime/transformation.py
index ca6258c5cb3..f00cd2c713d 100644
--- a/litellm/llms/gemini/realtime/transformation.py
+++ b/litellm/llms/gemini/realtime/transformation.py
@@ -5,7 +5,6 @@ This file contains the transformation logic for the Gemini realtime API.
import json
from collections import OrderedDict
from collections.abc import Mapping
-from types import MappingProxyType
from typing import Any, Final, cast
import litellm
@@ -74,22 +73,28 @@ MAP_GEMINI_FIELD_TO_OPENAI_EVENT: Final[dict[str, OpenAIRealtimeEventTypes | Res
_KNOWN_GEMINI_TOP_LEVEL_KEYS: Final[set] = {map_key.split(".", 1)[0] for map_key in MAP_GEMINI_FIELD_TO_OPENAI_EVENT}
-GEMINI_LIVE_VOICE_MAPPINGS: Final[Mapping[str, str]] = MappingProxyType(
- {
- "alloy": "Zephyr",
- "ash": "Charon",
- "ballad": "Fenrir",
- "cedar": "Charon",
- "coral": "Aoede",
- "echo": "Puck",
- "marin": "Aoede",
- "sage": "Kore",
- "shimmer": "Leda",
- "verse": "Orus",
- }
+OPENAI_STOCK_REALTIME_VOICES: Final[frozenset[str]] = frozenset(
+ {"alloy", "ash", "ballad", "cedar", "coral", "echo", "marin", "sage", "shimmer", "verse"}
)
+def _gemini_live_speech_config(voice: object) -> Mapping[str, object] | None:
+ """Build the Gemini Live speechConfig for a client-requested voice.
+
+ OpenAI stock voice names have no Gemini equivalent and Gemini Live closes
+ the session on an unknown voice, so they are dropped with a warning and
+ the model keeps its default voice. Every other name is forwarded verbatim.
+ """
+ if isinstance(voice, str) and voice.lower() in OPENAI_STOCK_REALTIME_VOICES:
+ verbose_logger.warning(
+ "Gemini Realtime: voice %s is an OpenAI voice with no Gemini equivalent; "
+ "dropping it so the session keeps the model's default voice.",
+ voice,
+ )
+ return None
+ return VertexGeminiConfig()._map_audio_params({"voice": voice})
+
+
class GeminiRealtimeConfig(BaseRealtimeConfig):
_TOOL_CALL_ID_TO_NAME_MAX = 256 # LRU cap for call_id→name mapping
@@ -307,13 +312,7 @@ class GeminiRealtimeConfig(BaseRealtimeConfig):
automaticActivityDetection=transformed_audio_activity_config
)
elif key == "voice":
- from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import (
- VertexGeminiConfig,
- )
-
- vertex_gemini_config = VertexGeminiConfig()
- gemini_voice = GEMINI_LIVE_VOICE_MAPPINGS.get(value.lower(), value) if isinstance(value, str) else value
- speech_config = vertex_gemini_config._map_audio_params({"voice": gemini_voice})
+ speech_config = _gemini_live_speech_config(value)
if speech_config:
optional_params["generationConfig"]["speechConfig"] = speech_config
if len(optional_params["generationConfig"]) == 0:
diff --git a/tests/test_litellm/llms/gemini/realtime/test_gemini_realtime_transformation.py b/tests/test_litellm/llms/gemini/realtime/test_gemini_realtime_transformation.py
index dc2e3963dfc..e80724f3347 100644
--- a/tests/test_litellm/llms/gemini/realtime/test_gemini_realtime_transformation.py
+++ b/tests/test_litellm/llms/gemini/realtime/test_gemini_realtime_transformation.py
@@ -1867,15 +1867,15 @@ def test_is_setup_message_and_is_content_message():
assert config.is_content_message({"setup": {}}) is False
-def test_map_openai_params_maps_stock_voice_case_insensitively():
- """Regression: OpenAI stock voices map to Gemini prebuilt voices regardless of casing.
+def test_map_openai_params_drops_stock_voice_case_insensitively():
+ """Regression: OpenAI stock voices are dropped regardless of casing so Gemini Live keeps its default voice.
- Unknown names pass through verbatim.
+ Non-OpenAI names pass through verbatim.
"""
cfg = GeminiRealtimeConfig()
- mapped = cfg.map_openai_params(optional_params={}, non_default_params={"voice": "Alloy"})
- assert mapped["generationConfig"]["speechConfig"]["voiceConfig"]["prebuiltVoiceConfig"]["voiceName"] == "Zephyr"
+ dropped = cfg.map_openai_params(optional_params={}, non_default_params={"voice": "Alloy"})
+ assert "speechConfig" not in dropped.get("generationConfig", {})
passthrough = cfg.map_openai_params(optional_params={}, non_default_params={"voice": "Kore"})
assert passthrough["generationConfig"]["speechConfig"]["voiceConfig"]["prebuiltVoiceConfig"]["voiceName"] == "Kore"
diff --git a/tests/test_litellm/llms/vertex_ai/realtime/test_vertex_ai_realtime_transformation.py b/tests/test_litellm/llms/vertex_ai/realtime/test_vertex_ai_realtime_transformation.py
index f2689975592..5a45a762e0b 100644
--- a/tests/test_litellm/llms/vertex_ai/realtime/test_vertex_ai_realtime_transformation.py
+++ b/tests/test_litellm/llms/vertex_ai/realtime/test_vertex_ai_realtime_transformation.py
@@ -513,11 +513,12 @@ def test_google_ai_studio_native_audio_still_strips_voice(patch_native_audio_cos
assert "speechConfig" not in generation_config
-def test_vertex_native_audio_maps_openai_stock_voice(patch_native_audio_cost_map_entry):
- """Regression: OpenAI stock voice names must be mapped, not forwarded verbatim.
+def test_vertex_native_audio_drops_openai_stock_voice(patch_native_audio_cost_map_entry):
+ """Regression: OpenAI stock voice names must be dropped, not forwarded verbatim.
Vertex Live closes the socket with 1007 on an unknown voice name, so a
client sending OpenAI's default voice would lose the session entirely.
+ Dropping the voice keeps the session alive on the model's default voice.
"""
cfg = VertexAIRealtimeConfig(
access_token="tok", project="my-proj", location="us-central1"
@@ -534,7 +535,7 @@ def test_vertex_native_audio_maps_openai_stock_voice(patch_native_audio_cost_map
)
generation_config = json.loads(messages[0])["setup"]["generationConfig"]
- assert generation_config["speechConfig"]["voiceConfig"]["prebuiltVoiceConfig"]["voiceName"] == "Zephyr"
+ assert "speechConfig" not in generation_config
def test_vertex_native_audio_unmapped_voice_passes_through(patch_native_audio_cost_map_entry):