WARNING: the RealtimeModel uses a server-side turn...
# ask-ai
b
WARNING: the RealtimeModel uses a server-side turn detection, allow_interruptions cannot be False when using VoiceAgent.say(), disable turn_detection in the RealtimeModel and use VAD on the AgentTask/VoiceAgent instead how to handle this with this setup
Copy code
@function_tool()
async def detected_answering_machine(self, context: RunContext) -> None:
    """
    Call this tool if you have detected a voicemail system, AFTER hearing the voicemail greeting
    """

    await self.session.say(text=instructions, allow_interruptions=False)
    await asyncio.sleep(1)
    await self.hangup()
Copy code
tts = TTS.with_azure(
    model=agent_settings.AZURE_OPENAI_TTS_DEPLOYMENT,
    azure_deployment=agent_settings.AZURE_OPENAI_TTS_DEPLOYMENT,
    voice=agent_settings.LLM_VOICE,
    speed=agent_settings.TTS_SPEED,
    instructions=vibe_instructions,
)

llm = RealtimeModel.with_azure(
    azure_deployment=agent_settings.AZURE_OPENAI_REALTIME_DEPLOYMENT,
    input_audio_transcription=realtime.session.InputAudioTranscription(
        model=agent_settings.AZURE_OPENAI_SST_DEPLOYMENT
    ),
    input_audio_noise_reduction=realtime.session.InputAudioNoiseReduction(
        type="near_field"
    ),
    voice=agent_settings.LLM_VOICE,
    turn_detection=realtime.session.TurnDetection(
        type="semantic_vad",
        create_response=True,
        eagerness="auto",
        interrupt_response=True,
    ),
)
t
Thanks for reaching out, Aleh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! The warning you’re seeing happens because the RealtimeModel uses server-side turn detection, which doesn’t quite work when you set
allow_interruptions=False
. No worries though — here’s how you can fix it: • Try disabling server-side turn detection by setting
turn_detection=None
in your RealtimeModel configuration. • Then, switch to using client-side Voice Activity Detection (VAD) and turn detection. For example, LiveKit’s
MultilingualModel
works great for turn detection, and you can pair it with a VAD model like
silero.VAD
. Here’s a quick example to guide you:
Copy code
session = AgentSession(
    turn_detection=MultilingualModel(),
    vad=silero.VAD.load(),
    stt=deepgram.STT(),
    llm=openai.realtime.RealtimeModel(
        turn_detection=None  # Disables server-side turn detection
    )
)
This way, interruptions and turn-taking are handled smoothly on the client side, and that warning should go away. If you have any questions or want me to walk you through it, just let me know — I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent.py | agents/src/voice/agent_activity.ts | Voice activity detection (VAD) parameters | LiveKit Docs | Gemini Live API integration guide | LiveKit Docs | Docs::Agents
b
i want this to be applied only for target tool call
Copy code
detected_answering_machine
t
Thanks so much for your question! I want to let you know that turn detection and VAD configurations in LiveKit are applied at the session level, so they can’t be set individually for specific tool calls like
detected_answering_machine
. These settings impact the whole session and aren’t really built to be changed dynamically for particular tools. If you’re looking for different behavior on certain tool calls, you might consider handling that logic outside of LiveKit or trying to reconfigure the session dynamically. That said, this isn’t officially supported or documented, so it might take a bit of experimentation. Please feel free to reach out if you’d like some help brainstorming or exploring possible workarounds—I’m here to support you! Sources: agent.py | Docs::Agents
b
then here
Copy code
await self.session.say(text=instructions)
i want to ensure that before agent say something voicemail end it's speach
t
To make sure the voicemail finishes its speech before the agent responds, you can try the following: • Use
await self.session.say(text, allow_interruptions=False)
to gently prevent any interruptions during the agent’s speech. • Await the returned
SpeechHandle
so the audio plays fully before moving on. For example:
Copy code
handle = await self.session.say(text=instructions, allow_interruptions=False)
await handle.wait_for_playout()
This way, the voicemail will complete its message before the agent begins speaking. If you have any questions or need a bit more help, just let me know—I’m here for you! Sources: Docs::Agents | Docs::Agents | basics/uninterruptable/README.md | livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs
b
i can't use allow_interruptions=False as we saw, will it work without this?
t
I understand that sometimes
allow_interruptions=False
might not be an option due to the RealtimeModel's limitations. But don’t worry! You can still make sure the voicemail finishes speaking by using the
SpeechHandle
returned from
session.say()
. Here’s a little example to guide you:
Copy code
handle = await self.session.say(text=instructions)
await handle.wait_for_playout()
This way, the agent patiently waits for the entire audio to play before moving on, even if you can’t explicitly disable interruptions. If you have any questions or need a hand with this, please feel free to ask—I’m here to help!
b
here on agent enter i try to detect voicemail
Copy code
async def on_enter(self) -> None:
        """Wait for voicemail greeting to finish, then intelligently respond"""
        await asyncio.sleep(3)

        if self.session.user_state == "speaking":
            logger.info("Detected audio on entry")
            while self.session.user_state == "speaking":
                await asyncio.sleep(1)
            logger.info("Audio completed, analyzing for voicemail detection")

            last_user_message = self._get_last_user_message()
            await self.update_instructions(f"""
You just heard audio: '{last_user_message}' Analyze what you heard:
- If it was a voicemail greeting,
Common indicators of voicemail:
* Automated greeting messages: "You have reached the voicemail of..."
* Voicemail system prompts: "Please leave your name and number"
* Generic unavailable messages: "The person you are trying to reach is currently unavailable."

IMMEDIATELY call the detected_answering_machine tool.
- If it was a person saying hello or responding, greet them naturally.
IMPORTANT: DO NOT use the end_call in this context.
Only use detected_answering_machine.""")
        else:
            await self.update_tools(self.tools + [customer_unavailable])
            await self.update_instructions("""
CUSTOMER UNAVAILABLE: If you're told that the person exists but is not available
right now, use the customer_unavailable tool.
            """)
            await self.session.generate_reply()
maybe there is more optimal way?
t
Thanks for sharing your current approach to detecting voicemail by monitoring
self.session.user_state
and analyzing the last user message. It’s definitely working, but there are some ways we could make it clearer and more efficient. Here are a few ideas: • Instead of using
await asyncio.sleep(3)
, we might try a more event-driven approach to catch when the user starts and stops speaking. This could help cut down on any unnecessary waiting time. • Using a dedicated voice activity detection (VAD) model or turn detection logic could be a great way to pinpoint exactly when the voicemail greeting ends. This tends to be more precise than just relying on
self.session.user_state
. • We could also simplify the process by integrating the analysis of the last user message directly into the tool call, which would reduce some of the extra steps. If you'd like, I’m here to help you implement these tweaks or explore other options together. Just let me know!
b
yes help me
@alert-easter-19258