I'm using OpenAI's `RealtimeModel` for both STT an...
# ask-ai
m
I'm using OpenAI's
RealtimeModel
for both STT and LLM:
Copy code
llm=openai.realtime.RealtimeModel(
    model="gpt-4o-realtime-preview",
    modalities=["text"],
    input_audio_transcription=InputAudioTranscription(
        model="gpt-4o-realtime-preview",
        language="en",
    ),
),
for some reason, it is not triggering the
input_audio_transcription_completed
event even though I'm tracking it like this:
Copy code
if hasattr(self.session.llm, "session"):
    rt_session = self.session.llm.session()
    rt_session.on(
        "input_audio_transcription_completed",
        self.on_transcription_completed,
    )
I can see that it's reaching the if statement above, but the function itself is not being triggered
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! It sounds like the
input_audio_transcription_completed
event isn’t firing, and that can be frustrating. Let’s walk through some steps together to figure out what might be going on: 1. Check Your Transcription Model Setup: Make sure
input_audio_transcription
is turned on and using a valid model like
gpt-4o-mini-transcribe
or
whisper-1
. If that’s not set, the event won’t trigger. 2. Confirm Audio Is Committed: The transcription only kicks off after you call
commit_audio()
with at least 100ms of audio processed. Double-check that part! 3. Keep an Eye on Server Events: Try adding a listener for
openai_server_event_received
to see if the server is actually sending the transcription completion event. 4. Watch for Transcription Errors: Listen for
input_speech_transcription_failed
events — sometimes failures here stop the completion event from happening. 5. Turn on Debug Mode: Setting
LK_OPENAI_DEBUG=1
will log all client and server events, which can give you a clearer picture of what’s going on. Hopefully, these tips help you pinpoint where the hiccup is—whether it’s in the setup, event handling, or server side. If you hit any snags or want to chat more about it, just let me know. I’m here to help! Sources: examples/voice_agents/realtime_turn_detector.py | plugins/openai/src/realtime/realtime_model.ts | agent/main.py
m
I tried all of the above, it's still not triggering the event. What is the best way/place to setup the event tracking if my agent is extending the
Agent
class?
t
Setting up event tracking for
input_audio_transcription_completed
in an Agent class that extends
OpenAI RealtimeModel
is pretty straightforward. Here’s a friendly walkthrough to get you started: 1. Register the Event Listener: When initializing, make sure to register the listener for the transcription completion event like this:
Copy code
self._rt_session.on(
    "input_audio_transcription_completed",
    self._on_input_audio_transcription_completed
)
2. Handle the Event: Create a handler that processes the event, updates the chat context, and emits the right agent event:
Copy code
def _on_input_audio_transcription_completed(self, ev: llm.InputTranscriptionCompleted):
    self._session._user_input_transcribed(
        UserInputTranscribedEvent(transcript=ev.transcript, is_final=ev.is_final)
    )

    if ev.is_final:
        msg = llm.ChatMessage(role="user", content=[ev.transcript], id=ev.item_id)
        self._agent._chat_ctx.items.append(msg)
        self._session._conversation_item_added(msg)
3. Handle Errors: It’s a good idea to listen for transcription failures and log any errors that pop up:
Copy code
def _handle_conversion_item_input_audio_transcription_failed(self, event):
    logger.error(
        "OpenAI Realtime API failed to transcribe input audio",
        extra={"error": event.error},
    )
4. General Error Handling: Don’t forget to register a listener for general WebSocket errors too:
self._rt_session.on("error", self._handle_error)
This setup helps keep things running smoothly when handling transcription events and any errors that might come up. If you want me to walk you through any part or if something’s unclear, just let me know—I’m happy to help! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | agent/main.py | plugins/openai/src/realtime/realtime_model.ts
m
what turn detection should I use in my setup?
t
When setting things up, you’ve got two turn detection methods to choose from, depending on what works best for you: 1. Server VAD (Great for Most Situations): This is the default and budget-friendly option. It picks up turns by listening for silence in the audio. If you want quick responses and are dealing with noisy surroundings, this is your go-to. You can tweak settings like
silence_duration_ms
(try 200ms for faster replies or 500ms to avoid too many interruptions) and
threshold
(around 0.6 works well in noisy spots). 2. Semantic VAD (Better for More Complex Chats): This one looks at the transcribed text to figure out when someone’s done talking. It’s great for avoiding interruptions during longer or more detailed sentences. Choose this if having a smooth, thoughtful conversation matters more than speed. You can adjust the
eagerness
setting (go “low” to cut down on interruptions or “high” for quicker replies). I’d suggest starting with Server VAD and adjusting it to fit your environment. If you notice people getting cut off mid-sentence, switching to Semantic VAD might do the trick. And of course, I’m here if you want a hand setting any of this up! Sources: OpenAI Realtime API integration guide | LiveKit Docs | Turn detection and interruptions | LiveKit Docs | examples/voice_agents/realtime_turn_detector.py | realtime/openai-realtime-tools.py