I'm using OpenAI's `RealtimeModel` for both STT+LL...
# ask-ai
m
I'm using OpenAI's
RealtimeModel
for both STT+LLM (using "text" modalities). How do I detect the End of Speech? Is there an event that gets triggered even in realtime models?
t
Thanks for reaching out, Anas. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Could you share a bit more about your setup? Like, are you using LiveKit’s integration with OpenAI’s RealtimeModel, or is this something you’ve set up on its own? Knowing this will help me give you the best advice on how to detect the End of Speech.
m
I'm using livekit's integration with OpenAI's RealtimeModel
t
Hey there! When you're working with LiveKit's integration with OpenAI's RealtimeModel, detecting the End of Speech (EOS) can be done in a few handy ways: • Server-side LLM Turn Detection: You'll see the
input_audio_buffer.speech_stopped
event pop up when speech ends. Just make sure to enable this by setting up the RealtimeModel with
turn_detection
. • VAD-based Detection: This uses silence detection tools like Silero VAD. You can tweak settings like
min_endpointing_delay
(which defaults to 500ms) and
max_endpointing_delay
(default is 6.0s) to fit your needs. • STT-based Detection: Here, the
SpeechEventType.END_OF_SPEECH
event fires once the final transcript is in and silence is confirmed. If you'd like, I can walk you through a detailed example on how to set these up and handle the events smoothly in your project. Just let me know! Sources: livekit-plugins/livekit-plugins-azure/livekit/plugins/azure/stt.py | Improving voice AI's turn detection with transformers | AI voice agents | LiveKit Docs | Speech & Audio Processing | livekit/livekit_composite | DeepWiki | agents/src/pipeline/human_input.ts
m
I have a secondary LLM model that I want to trigger as soon as EOS is detected. Meaning, the realtime model will continue through LLM, and there's a sidecar model that's being run on the side at EOS. can you walk me through how to do that?
t
Hey there! If you want to trigger a secondary LLM model right when speech ends (End of Speech or EOS) in LiveKit's integration with OpenAI's RealtimeModel, here are a few friendly ways to do it: 1. Keep an Eye on EOS Events: You can listen for the
input_audio_buffer.speech_stopped
event—that’s your cue that speech has ended. Once you catch that, you can easily trigger your secondary LLM logic. 2. Work with Final Transcripts: The
on_final_transcript
event is a great spot to grab the final transcription and send it over to your secondary LLM, based on whatever custom rules you want. 3. Try a Pipeline Setup: Instead of sticking with the RealtimeModel’s closed-loop design, you might find it helpful to build a pipeline with separate STT and LLM components. This way, you have full control over when and how multiple LLMs get called. Would you like me to walk you through a detailed example of any of these? Just let me know—I’m here to help! Sources: OpenAI Realtime API integration guide | LiveKit Docs | livekit-plugins/livekit-plugins-turn-detector/livekit/plugins/turn_detector... | Improving voice AI's turn detection with transformers | plugins/livekit/src/turn_detector.ts
m
is
on_final_transcript
from the
AgentSession
object? or what object is it from?
t
Hey there! Just a quick heads-up about the
on_final_transcript
event — it’s part of the
RecognitionHooks
protocol in LiveKit’s framework and usually helps you catch those final transcription results from Speech-to-Text (STT) systems. But here’s the thing: if you’re using OpenAI’s RealtimeModel with transcription turned on, the
on_final_transcript
event won’t actually trigger. Instead, the RealtimeModel handles transcription on its own, so you’ll want to listen for the
input_audio_transcription_completed
event on the agent session. That’s where you’ll get the final transcription straight from the RealtimeModel’s API. If you want, I’m happy to walk you through how to set this up in your project—just let me know! Sources: livekit-plugins/livekit-plugins-speechmatics/livekit/plugins/speechmatics/s...
m
how do I listen to the
input_audio_transcription_completed
event?
t
Hey there! To catch the
input_audio_transcription_completed
event when using LiveKit's integration with OpenAI's RealtimeModel, here’s a simple way to do it: • Turn on Input Audio Transcription: First, make sure transcription is switched on in your RealtimeModel setup. Like this:
Copy code
from livekit.plugins.openai import InputAudioTranscription

model = openai.realtime.RealtimeModel(
    api_key="your-api-key",
    input_audio_transcription=InputAudioTranscription(model="whisper-1")
)
• Set Up an Event Listener: Once transcription’s enabled, you can add a handler to listen for the
input_audio_transcription_completed
event:
Copy code
@session.on("input_audio_transcription_completed")
def on_input_transcription_completed(event):
    print(f"Transcription completed: {event.transcript}")
    # You can grab the item ID with event.item_id
    # And just so you know, event.is_final is always True here
Here’s what you get from the event: • `item_id`: The ID for the conversation item • `transcript`: The final transcribed text • `is_final`: Always
True
for this event If you'd like, I’m here to help you get this set up smoothly—just let me know! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | plugins/openai/src/realtime/realtime_model.ts
m
how do I play a pre-recorded wav file?
t
If you want to play a pre-recorded WAV file in LiveKit, here are a couple of easy ways to do it: • Using File Ingress: LiveKit’s File Inress feature lets you stream pre-recorded media files like WAV straight into a room. It’s a great way to broadcast recorded content smoothly. • Using SDKs: You can also manually read and publish a WAV file using LiveKit SDKs. For example, in Python:
Copy code
from agent_extensions.utils import WavPlayer

wav_player = WavPlayer()
await wav_player.play_once("notification.wav", room)
This handy utility reads your WAV file, converts it to the right format (48kHz, mono), and publishes it as an audio track for you. If you want, I’d be happy to share more details or examples for a specific SDK—just let me know! Sources: Ingress & Media Input | livekit/livekit_composite | DeepWiki | agent_extensions/agent_extensions/utils/__init__.py | basics/playing_audio.py | examples/publish-wav/index.ts | examples/play_from_disk/.gitattributes
m
how do i use ingress
t
Hey there! If you want to use File Ingress in LiveKit to play a pre-recorded WAV file, here’s a simple way to do it: 1. Set Up Ingress: First, you’ll need to configure an Ingress endpoint on your LiveKit server. This means creating an Ingress configuration where you specify the media source and the room where you want the audio to play. 2. Prepare the WAV File: Make sure your WAV file is in a compatible format—something like 16-bit PCM, 48kHz, either mono or stereo works great. 3. Stream the File: Then, use the Ingress API or the available tools to stream your WAV file into the endpoint you set up. The file will play as an audio track right in the target room. If you want, I can walk you through the details of setting up the Ingress endpoint or how to stream the file—just let me know!
m
does on_user_message still get triggered if I'm using RealtimeModel for both STT and LLM?
t
Yes, the
on_user_message
(or
on_user_turn_completed
) event still happens when you're using the RealtimeModel for both STT and LLM. Just a heads up, this event is triggered by the RealtimeModel’s own transcription process, not by an external STT service. Once the RealtimeModel finishes transcribing, it sends out the
input_audio_transcription_completed
event. That’s what kicks off the usual turn-completion flow, including the
on_user_turn_completed
callback. If you want, I’d be happy to walk you through how to handle this event in your setup—just let me know! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Pipeline nodes and hooks | LiveKit Docs | Realtime model integrations | LiveKit Docs
m
what is the most accurate way of detecting end of transcription/speech in a realtime model?
t
Hey there! The best way to tell when someone’s finished speaking in a RealtimeModel is by using Semantic VAD. It’s pretty clever—it combines understanding the meaning of what’s being said with analyzing the voice activity. So, it looks at both the audio signals and the actual content to figure out when the user’s done talking. If you want to enable Semantic VAD in Python, here’s a quick snippet:
Copy code
from livekit.plugins.openai import realtime
from openai.types.beta.realtime.session import TurnDetection

session = AgentSession(
    llm=realtime.RealtimeModel(
        turn_detection=TurnDetection(
            type="semantic_vad",
            eagerness="auto",  # Options: "low", "medium", "high", "auto"
            create_response=True,
            interrupt_response=True,
        )
    ),
)
And if you’re aiming for even better accuracy, the LiveKit Turn Detector plugin is a fantastic option. It uses a transformer-based model to detect turns semantically, which is super helpful for tricky conversations or if you’re working with multiple languages. Want me to walk you through setting up Semantic VAD or how to use the Turn Detector plugin? Just let me know! Sources: OpenAI Realtime API integration guide | LiveKit Docs | Voice activity detection (VAD) parameters | LiveKit Docs | livekit-agents/livekit/agents/voice/audio_recognition.py | Improving voice AI's turn detection with transformers | How does end-of-utterance detection work in conversations?
m
on_user_message is not being triggered with semantic_vad
t
Hey there! Just a quick heads-up: the
on_user_message
callback doesn’t get triggered when you’re using
semantic_vad
. That’s because the RealtimeModel takes care of turn detection and transcription all on its own, so it skips over the usual local STT and VAD pipelines that would normally call those callbacks. Instead, the RealtimeModel sends out events like
input_audio_transcription_completed
straight from the server. These events are handled internally, so they don’t activate
on_user_message
. If you’d like, I’m happy to help you figure out how to work with these server-side events or tweak your workflow to catch user messages more smoothly. Just let me know! Sources: OpenAI Realtime API integration guide | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | Realtime model integrations | LiveKit Docs
m
how do I watch for input_audio_transcription_completed
t
Here’s a friendly guide to help you watch for the
input_audio_transcription_completed
event in LiveKit's RealtimeModel: 1. Enable Input Audio Transcription: First, make sure transcription is turned on in your RealtimeModel configuration. Here’s a quick example:
Copy code
from livekit.plugins.openai import realtime\n\nmodel = realtime.RealtimeModel(\n    api_key=\"your-api-key\",\n    input_audio_transcription={\"model\": \"whisper-1\"}  # This turns transcription on\n)
1. Subscribe to the Event: Next, use
session.on
to listen for when transcription is done:
Copy code
@session.on(\"input_audio_transcription_completed\")\ndef on_transcription_completed(event):\n    print(f\"Transcription: {event.transcript}\")\n    print(f\"Is final: {event.is_final}\")\n    if event.is_final:\n        print(\"Final transcription received\")
Here’s what the event gives you: • `item_id`: The ID of the transcribed item • `transcript`: The actual transcribed text • `is_final`: A flag showing if this is the final transcription If you’d like, I’m happy to help you set this up or answer any questions you have! Sources: agent/main.py | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real...
m
it seems like it's still not being triggered. What's the simplest, cleanest way to detect end of transccription in a realtime model?
t
Hey there! The easiest and cleanest way to know when transcription ends in a RealtimeModel is by using the
input_audio_transcription_completed
event. This event pops up once the transcription is finalized. Just make sure you’ve got these steps set up right: 1. Enable Transcription: Double-check that
input_audio_transcription
is turned on in your RealtimeModel config. Here’s a quick example:
Copy code
from livekit.plugins.openai import realtime\n\nmodel = realtime.RealtimeModel(\n    api_key=\"your-api-key\",\n    input_audio_transcription={\"model\": \"whisper-1\"}  # Enables transcription\n)
1. Listen for the Event: Use
session.on
to catch the
input_audio_transcription_completed
event like this:
Copy code
@session.on("input_audio_transcription_completed")\ndef on_transcription_completed(event):\n    print(f"Transcription: {event.transcript}")\n    if event.is_final:\n        print("Final transcription received")
If you’re not seeing this event fire, it might mean there’s a little hiccup in the setup or configuration. Want me to help you double-check things or maybe look at some other options? Just let me know!
m
do I put that function in the AgentSession subclass?
t
Hey there! Just a quick tip: you don’t actually need to put the
input_audio_transcription_completed
event handler inside an
AgentSession
subclass. Instead, you can simply attach the event listener right to the session instance created by the RealtimeModel. Here’s a little example:
Copy code
session = realtime_model.session()  # Grab the session instance\n\n@session.on("input_audio_transcription_completed")\ndef on_transcription_completed(event):\n    print(f"Transcription: {event.transcript}")\n    if event.is_final:\n        print("Final transcription received")
This way, things stay nice and simple without needing to subclass
AgentSession
. If you want, I’m here to help clarify anything or walk you through the setup!