flaky-hospital-13625
08/11/2025, 11:52 PMtall-airline-16756
08/11/2025, 11:52 PMtall-airline-16756
08/11/2025, 11:53 PMAGENT_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 Docsflaky-hospital-13625
08/11/2025, 11:55 PMtall-airline-16756
08/11/2025, 11:56 PMimport 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
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 Details
• Partial 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