I'm using on_user_turn_completed() method with ses...
# ask-ai
c
I'm using on_user_turn_completed() method with session.say to make the agent speak a specific text. To know if the user has interrupted this, I'm awaiting the session.say handle and checking if interruption happened using the handle.interrupted. but the handle has to be awaited for this becuase of which the llm call which should happen as a part of voicepipeline is being blocked and it's waiting for the session.say in on_user_turn_completed to be finished. how do I handle the interruption case without blocking the actual llm call?
t
Thanks for reaching out, Bharath. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’m glad you asked about handling interruptions in
session.say
without blocking the LLM call in
on_user_turn_completed
. Here’s a friendly approach that should help you manage this smoothly: • Make sure
allow_interruptions=True
(which is the default) in your
session.say
. This setting lets the system automatically detect and handle any interruptions. • Right after the
session.say
call, you can check the
interrupted
property of the
SpeechHandle
to see if the speech was interrupted. • To keep your LLM call from being blocked, it’s best to structure your code so that you handle the interruption check after you’ve started the LLM call. Here’s a simple example to illustrate this:
Copy code
async def on_user_turn_completed(session):
    handle = await session.say("Your message here", allow_interruptions=True)

    # Proceed with the LLM call
    llm_response = await session.generate_reply("User input")

    # Check if the speech was interrupted
    if handle.interrupted:
        print("Speech was interrupted")
This way, your LLM call won’t be delayed, and you’ll still be able to respond to interruptions effectively. If you have any more questions or want to explore this further, feel free to ask—I’m here to help! Sources: agents/src/voice/speech_handle.ts | livekit-agents/livekit/agents/voice/agent_activity.py | Turn detection and interruptions | LiveKit Docs | Agent speech and audio | LiveKit docs | basics/uninterruptable.py
c
But in this, you are waiting session.say right, won't this block the llm call of the voiicepipeline clasS?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!