How to detect at which point of the agent speech t...
# ask-ai
f
How to detect at which point of the agent speech the user interrupted, so that agent can repeat what was missed
t
Thanks for reaching out, Amjad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To catch when a user interrupts the agent's speech and help the agent repeat what was missed, you can use LiveKit's APIs like this: • Listen for Interruption Events: Keep an ear out for the
AGENT_SPEECH_INTERRUPTED
event to spot interruptions. Here’s a quick example in JavaScript:
agent.on(VPAEvent.AGENT_SPEECH_INTERRUPTED, (msg) => { console.log('Agent was interrupted:', msg); });
Access Playback Position: When an interruption happens, LiveKit gives you a
PlaybackFinishedEvent
object that tells you exactly where the playback stopped. For example, in Python:
def handle_playback_finished(event): if event.interrupted: playback_position = event.playback_position print(f"Interrupted at position: {playback_position} seconds")
Track Speech with SpeechHandle: Use the
SpeechHandle
you get from
say()
or
generate_reply()
to keep an eye on interruptions and access the original speech content. • Automatic Context Management: LiveKit smartly saves the interruption context in its
ChatContext
, so you can pick up right where you left off. By combining these features, you can easily detect interruptions, figure out what was missed, and generate a reply to repeat that part. If you want, I’m here to help you with the next steps! Sources: agents/src/pipeline/pipeline_agent.ts | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Agent speech | LiveKit Docs | Turn detection and interruptions | LiveKit Docs
f
Share examples for python. Also does chat ctx contain reference to where the agent stopped or the entire speech.
t
Hey there! Here are some Python examples to help you detect interruptions and smoothly resume agent speech in LiveKit: 1. Basic Interruption Detection
Copy code
import asyncio
from livekit.agents import AgentSession
from livekit.agents.voice.events import UserInputTranscribedEvent

async def detect_interruptions():
    session = AgentSession(allow_interruptions=True)

    @session.on("user_input_transcribed")
    def on_user_input(event: UserInputTranscribedEvent):
        if event.is_final and len(event.transcript.strip()) > 0:
            print(f"User said: {event.transcript}")

    @session.on("conversation_item_added")
    def on_conversation_item_added(event):
        if event.item.interrupted:
            print(f"Agent was interrupted. Partial speech: '{event.item.text_content}'")

    await session.start()
2. Resume After Interruption
Copy code
async def resume_after_interruption(session):
    last_message = session.history.items[-1] if session.history.items else None
    if last_message and last_message.interrupted:
        print(f"Resuming from interrupted speech: '{last_message.text_content}'")
        await session.say(f"Let me continue: {last_message.text_content}")
ChatContext DetailsPartial Speech Storage: When an interruption happens,
ChatContext
keeps just the part of the speech that actually got played to the user, not the whole thing. • Interrupted Flag: Messages in
ChatContext
get tagged with
interrupted=True
if they were cut off mid-speech. • Automatic Handling: The session takes care of tracking interruptions and updates the context for you, so you don’t have to worry about it. If you want me to walk you through anything or need more examples, just let me know—I’m happy to help! Sources: VoicePipelineAgent | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py