how to get user_transcript langauge
# ask-ai
m
how to get user_transcript langauge
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To retrieve the user transcript language in LiveKit, there are several approaches depending on your specific use case: • From Speech Events (STT Processing): When processing speech-to-text events, the language information is available in the
SpeechEvent
alternatives. For example:
Copy code
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:
Copy code
# 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:
Copy code
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:
Copy code
@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:
Copy code
# 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