great-tiger-31125
05/28/2026, 3:49 AMtall-airline-16756
05/28/2026, 3:49 AMtall-airline-16756
05/28/2026, 3:50 AMlanguage="multi") so incoming audio segments are transcribed and return a detected language code.
2. Disable automatic realtime LLM activity detection and use LiveKit’s turn detector with turn_handling=TurnHandlingOptions(turn_detection=MultilingualModel()) (or override stt_node) so your agent logic receives STT events and language metadata.
3. In your STT handler, read the detected language and:
◦ Update agent/TTS settings (call update_options() or equivalent) to set the correct TTS voice/language (map STT language code → TTS locale/voice).
◦ Set or update the agent instructions to tell the LLM to respond in the detected language (e.g., “Respond in the same language as the user’s last utterance”).
1. Let the LLM (Gemini Realtime) generate the response; synthesize speech with the updated TTS settings so spoken output matches the user language.
2. Optionally sync the detected language to the frontend (participant attributes) so UI and controls reflect current language.
Notes and limits:
• This pattern (STT detection → instruction + TTS switch) is the documented approach for automatic language switching.
• Gemini Realtime itself is not shown to auto-switch languages without STT-driven detection and explicit instruction/TTS updates.
If you want, tell me:
• Which STT and TTS providers you plan to use (or if you want to use Deepgram + built-in TTS), and
• Whether you’re using the LiveKit example agent flow or a custom agent.
I’ll give specific code snippets and config for that setup.
Sources: Deepgram STT | LiveKit Documentation | LiveKit turn detector plugin | LiveKit Documentation | Build a multilingual voice agent that automatically switches languages | Li..., with interactive elementsgreat-tiger-31125
05/28/2026, 3:50 AMtall-airline-16756
05/28/2026, 3:51 AMlanguage="multi"), overrides the STT node to read detected language, updates agent instructions and TTS options, and uses LiveKit’s multilingual turn detector. Adapt mappings and provider calls to your environment.
# Example: multilingual agent with Gemini Realtime + LiveKit Agents 1.5.9
# - Deepgram STT in multilingual mode (language="multi")
# - Turn detector -> override stt_node to read detected language
# - update_options() to switch TTS voice/locale
# - instruct LLM to reply in detected language
from livekit_agents import AgentSession, TurnHandlingOptions, MultilingualModel
from livekit_agents.plugins import google
from livekit_agents.types import RealtimeInputConfig, AutomaticActivityDetection
# Map STT language codes -> TTS voice/locales you support
LANG_TO_TTS = {
"en": {"voice": "en-US-Wavenet-D", "locale": "en-US"},
"es": {"voice": "es-ES-Standard-A", "locale": "es-ES"},
"fr": {"voice": "fr-FR-Standard-A", "locale": "fr-FR"},
# add more mappings as needed
}
def map_stt_lang_to_tts(stt_lang_code):
# normalize codes like "en-US" -> "en"
lang = stt_lang_code.split("-")[0]
return LANG_TO_TTS.get(lang, LANG_TO_TTS["en"])
# Create the Gemini Realtime model configuration with automatic activity detection disabled
realtime_llm = google.realtime.RealtimeModel(
model="gpt-realtime-preview", # or specific Gemini realtime model name you use
realtime_input_config=RealtimeInputConfig(
automatic_activity_detection=AutomaticActivityDetection(disabled=True)
),
# input_audio_transcription=None # we rely on external STT for language detection
)
# Use Deepgram (or another multilingual STT) and set language="multi"
stt_config = {
"provider": "deepgram",
"model": "nova-3",
"language": "multi", # enables automatic language detection in Deepgram
# provider-specific credentials / params follow...
}
# Turn handling: use the multilingual turn detector that relies on STT language metadata
turn_handling = TurnHandlingOptions(turn_detection=MultilingualModel())
# Initial agent instructions: ask model to reply in user language (will be reinforced per-turn)
base_instructions = (
"You are an assistant that replies in the same language the user spoke. "
"If the user speaks in X language, answer in X language."
)
# Create AgentSession (pseudocode - adapt to your project structure)
agent = AgentSession(
llm=realtime_llm,
stt=stt_config,
turn_handling=turn_handling,
instructions=base_instructions,
# other session options...
)
# Override/extend STT node handler to watch for language detection events
# This example assumes agent framework emits STT result events you can handle.
# Replace `on_stt_result` with actual callback registration in your setup.
def on_stt_result(event):
"""
event expected to include:
- text: transcribed text
- language: detected language code (e.g., "en-US", "es-ES")
- is_final: boolean
"""
# only react on final partial/final segments as appropriate
if not event.get("is_final"):
return
detected_lang = event.get("language")
if not detected_lang:
return
# Map to TTS options and update agent if changed
tts_opts = map_stt_lang_to_tts(detected_lang)
# Update TTS and instruction so LLM and TTS use the detected language for upcoming response
# update_options() is used in examples to change TTS/agent settings at runtime
agent.update_options({
"tts": {
"voice": tts_opts["voice"],
"locale": tts_opts["locale"],
},
# also update the instructions to explicitly tell LLM to respond in detected language
"instructions": (
f"User language detected: {detected_lang}. Reply in that language for the next response."
)
})
# Optionally sync the detected language to front-end participant attributes
# agent.set_participant_attribute("language", detected_lang)
# Register the handler (pseudocode)
agent.on("stt_result", on_stt_result)
# Start the agent session / event loop
agent.start()
Notes:
• Ensure your STT provider returns a language code per segment (Deepgram Nova models do when language="multi").
• The LLM reply language is driven by the instructions you set per-turn; switching TTS alone won't change the LLM output language.
• Use update_options() before the LLM turn begins so the LLM and TTS are both aligned for the upcoming response.
• Adjust model names, provider credentials, and event hook APIs to match your codebase.
If you tell me which STT/TTS providers and your agent framework entry points (callback names or where you handle STT events), I’ll convert this into a runnable snippet for your setup.
, with interactive elements