miniature-nail-35141
07/29/2025, 7:20 AMtall-airline-16756
07/29/2025, 7:21 AMtall-airline-16756
07/29/2025, 7:21 AMSpeechEvent alternatives. For example: async def _on_stt_event(self, ev: stt.SpeechEvent) -> None:
if ev.type == stt.SpeechEventType.FINAL_TRANSCRIPT:
transcript = ev.alternatives[0].text
language = ev.alternatives[0].language # Language detected from transcript
confidence = ev.alternatives[0].confidence
logger.debug(
"received user transcript",
extra={"user_transcript": transcript, "language": language},
)
• From Audio Recognition System: The AudioRecognition class tracks the last detected language automatically. For example: # The system maintains _last_language internally
if not self._last_language or (
language and len(transcript) > MIN_LANGUAGE_DETECTION_LENGTH
):
self._last_language = language
• From Participant Attributes: You can access language information stored in participant attributes. For example: async def entrypoint(ctx: JobContext):
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
participant = await ctx.wait_for_participant()
# Get current language attribute
language = participant.attributes.get("user.language")
# Listen for language changes
@ctx.room.on("participant_attributes_changed")
def on_participant_attributes_changed(changed_attrs: dict[str, str], p: rtc.Participant):
if p == participant:
language = p.attributes.get("user.language")
print(f"participant {p.identity} changed language to {language}")
• In Live Translation Applications: For applications with language selection (like live captioning), you can access the selected language through participant attributes. For example: @job.room.on("participant_attributes_changed")
def on_attributes_changed(
changed_attributes: dict[str, str], participant: rtc.Participant
):
lang = changed_attributes.get("captions_language", None)
if lang:
# Handle language change for captions/translation
print(f"Captions language changed to: {lang}")
• STT Configuration: When configuring STT providers, you can set the expected language or enable language detection. For example: # OpenAI STT with language detection
stt = openai.STT(
language="en", # Default language
detect_language=True # Enable automatic language detection
)
The most appropriate method depends on whether you need real-time language detection from the audio stream, user-selected language preferences, or both.
Sources: livekit-plugins/livekit-plugins-baseten/livekit/plugins/baseten/stt.py | Job lifecycle | LiveKit Docs | server/main.py