I send _interrupt_ when a button is clicked i...
# ask-ai
a
I send interrupt when a button is clicked in the front end to stop the ai but sometimes it continues on it own what could be causing this. Or is there a better way for me to stop the ai agent from talking ctx.room.on("data_received") def on_data_received(data): try: payload = json.loads(data.data.decode("utf-8")) message = payload["message"] if message == "__interrupt__": logger.info("Interrupt command received. Stopping current TTS playback.") session.interrupt() except Exception as e: logger.error(f"Error processing data message: {e}")
t
Thanks for reaching out, David. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please help me understand a bit better? Is the AI agent running directly within the LiveKit environment, or is it an external service that’s integrated with LiveKit? Also, if you’ve noticed any specific logs or errors when the interrupt command doesn’t stop the AI, sharing those would be really helpful. I’m here to assist you every step of the way!
a
Its a livekit agent
t
To help you smoothly stop AI playback in LiveKit agents using the
__interrupt__
command, the
interrupt()
method is your go-to solution. Let me walk you through two friendly approaches: • Using `session.interrupt()`: This handy method stops the currently active speech, whether triggered programmatically or through external input. For example:
session.interrupt()
• Using `SpeechHandle.interrupt()`: When you start speech with
session.say()
or
generate_reply()
, you get a
SpeechHandle
. Calling
.interrupt()
on this handle will immediately stop the playback:
Copy code
handle = session.say("This is a long message...")
handle.interrupt()
Both of these methods work even if you set allow_interruptions=False when starting the speech. Plus, LiveKit is designed to automatically interrupt when it detects user speech, smoothly switching the agent to a "listening" state. If you find interruptions aren’t working as expected, please double-check that the
__interrupt__
command is properly triggering the
interrupt()
method, and take a look at the logs for any errors. We're here to help you get it working perfectly! Sources: Bringing AI avatars to voice agents | Bringing AI avatars to voice agents | Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Docs::Agents