what is the meaning of `stt.StreamAdapter.` does...
# ask-ai
a
what is the meaning of
stt.StreamAdapter.
does it simply mocking realtime?
t
Thanks for reaching out, Jaihee. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
stt.StreamAdapter
in LiveKit is a helpful wrapper class created to bring streaming functionality to Speech-to-Text (STT) models that don’t naturally support real-time streaming input. It’s not just simulating real-time—it actually adapts non-streaming STT models to work smoothly in streaming scenarios by teaming up with Voice Activity Detection (VAD). Here’s a friendly breakdown of how it works: • It gently buffers audio input while using VAD to spot when someone is speaking. • It sends out streaming events like
START_OF_SPEECH
and
END_OF_SPEECH
based on what VAD detects. • When VAD signals the end of speech, it processes the complete speech segment by calling the STT model’s
recognize
method. This approach is especially handy for STT providers like Whisper that don’t support streaming input directly. By using
StreamAdapter
, you get genuine streaming capabilities by smartly segmenting audio and coordinating between VAD and the non-streaming STT models. If you’d like more details or some examples to help you along, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/stt/fallback_adapter.py | agents/src/voice/agent.ts | Speech-to-text (STT) models | LiveKit Docs
a
GPT-4o Transcribe is realtime right?
t
Yes, GPT-4o Transcribe does support real-time transcription in LiveKit! It works hand-in-hand with the OpenAI Realtime API to deliver low-latency, time-aligned transcription, which helps create smooth and natural interactions. This feature is included in LiveKit's Multimodal Agent API and is similar to the real-time experience you get with ChatGPT's Advanced Voice feature. If you'd like, I’m here to help you set it up or answer any other questions you might have—just let me know! Sources: OpenAI STT plugin guide | LiveKit Docs | Azure OpenAI STT plugin guide | LiveKit Docs | OpenAI and LiveKit | LiveKit Docs | OpenAI and LiveKit partner to turn Advanced Voice into an API | examples/other/transcription/README.md
a
if i set realtime =True, i don't need to use streamAdapter.
t
You're absolutely right! When you set
realtime=True
for GPT-4o Transcribe, there's no need to use
StreamAdapter
. This setting activates OpenAI's built-in streaming capabilities, so it naturally supports real-time transcription without needing extra layers like
StreamAdapter
. If you have any more questions or need a bit more help, please don't hesitate to reach out—I’m here for you! Sources: OpenAI and LiveKit partner to turn Advanced Voice into an API | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/util... | Speech-to-text (STT) models | LiveKit Docs
a
what should be the turn complete event handlers look like for open ai realtime
t
For OpenAI Realtime transcription in LiveKit, you can set up a "turn complete" event handler by using either the
response_done
event or the
on_user_turn_completed
lifecycle hook, depending on which part of the interaction you'd like to manage. 1. Using
response_done
Event:
This event fires when the assistant's response is fully finished. Here's a simple example to guide you:
Copy code
from livekit.plugins import openai

@session.on("response_done")
def on_response_done(response: openai.realtime.RealtimeResponse):
    """Called when an assistant's response is complete."""
    if response.status == "incomplete":
        # Handle incomplete response
        print("Response incomplete: Max token limit reached")
    elif response.status == "failed":
        # Handle failed response
        print("Response failed: Check rate limits")
    else:
        # Successful completion
        print("Response completed successfully")
2. Using
on_user_turn_completed
Lifecycle Hook:
This hook triggers right after the user finishes speaking, signaling the end of their turn. Here's how you can use it:
Copy code
from livekit import agents

class MyAgent(agents.Agent):
    def on_user_turn_completed(self) -> None:
        """Called when the user has finished speaking."""
        # Perform actions immediately after user turn ends
        print("User turn completed")
You can definitely use these handlers together to smoothly manage the entire interaction—from recognizing when the user’s turn ends to wrapping up the assistant’s response. If you have any questions or want to explore this further, just let me know—I’m here to help! Sources: agent/main.py | Pipeline nodes and hooks | LiveKit docs
a
for open ai should i use turn_detection": "realtime_llm"?
t
Absolutely! You can set
turn_detection="realtime_llm"
in the
AgentSession
when using OpenAI's Realtime API. This allows you to take advantage of OpenAI's built-in server-side turn detection, which is both efficient and cost-effective for handling user turns. To make sure everything runs smoothly, you'll also want to configure the
RealtimeModel
with a valid
turn_detection
parameter, like this:
Copy code
llm = openai.realtime.RealtimeModel(
    voice="alloy",
    turn_detection="server_vad"  # or "semantic_vad"
)
If you'd rather use LiveKit's own turn detection models (such as
MultilingualModel
), just set
turn_detection=None
in the
RealtimeModel
and adjust the
AgentSession
accordingly. Please feel free to reach out if you'd like me to walk you through any part of the setup or if you have any other questions—I'm here to help! Sources: OpenAI Realtime API plugin guide | LiveKit docs | examples/src/basic_agent.ts | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Turn detection and interruptions | LiveKit Docs
a
can we filterout non target language using can we use async def on_final_transcript(self, event: SpeechEvent): transcript = event.alternatives[0].text language = event.alternatives[0].language # e.g., "en", "es", "fr" print(f"Detected language: {language}")?
t
Absolutely, you can filter out non-target languages using the
on_final_transcript
callback in LiveKit. The
SpeechEvent
object includes a
language
field, which you can access via
event.alternatives[0].language
. This makes it easy to compare the detected language with your target language(s) and decide whether to process or skip the transcript. Here’s a simple example to guide you:
Copy code
async def on_final_transcript(self, event: SpeechEvent):
    transcript = event.alternatives[0].text
    language = event.alternatives[0].language  # e.g., "en", "es", "fr"
    print(f"Detected language: {language}")

    # Define your target language(s)
    target_languages = ["en"]  # Example: English only

    if language in target_languages:
        # Process the transcript
        print(f"Processing transcript: {transcript}")
    else:
        # Skip or discard non-target languages
        print(f"Discarding transcript in language: {language}")
This way, you can be confident that only transcripts in the languages you care about get processed. If you have any questions or want to explore this further, just let me know—I’m here to help! Sources: Gladia STT plugin guide | LiveKit Docs | Speech-to-text (STT) models | LiveKit Docs | Events and error handling | LiveKit docs | livekit-plugins/livekit-plugins-deepgram/livekit/plugins/deepgram/stt.py
a
can we filter out those on on_user_turn_completed?
t
Absolutely, you can filter out non-target languages using the
on_user_turn_completed
hook. This hook gives you access to the
new_message
parameter, which includes transcription data along with the detected language (as long as your STT model supports it). Here’s a friendly example to help you get started:
Copy code
from livekit.agents import ChatContext, ChatMessage
from livekit.agents.voice import StopResponse

async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
    # Grab the detected language from the transcription
    detected_lang = new_message.language  # Just make sure your STT model provides this

    # Set your target language(s)
    target_languages = ["en"]  # For example, English only

    if detected_lang not in target_languages:
        # Skip the agent's response if the language isn't what you're targeting
        raise StopResponse()
This method depends on your STT model’s language detection feature. If the detected language isn’t in your target list, you can easily prevent the agent from responding or manage the input in another way. Feel free to reach out if you'd like me to walk you through this further or if you have any other questions! Sources: Pipeline nodes and hooks | LiveKit docs | LiveKit turn detector plugin | LiveKit docs | tests/test_agent_session.py