lively-coat-10905
11/04/2025, 9:58 AMtall-airline-16756
11/04/2025, 9:58 AMtall-airline-16756
11/04/2025, 9:59 AMconversation_item_added and render the agent’s full text when the agent turn is committed (even if audio was interrupted). Guide
• Manually flush the agent turn
◦ On interruption, call await session.aclose() to flush the incomplete agent turn to history, then read it from session.history and render that full text. Guide Recording/transcripts
Notes:
• The TTS-aligned transcript will remain truncated by design when interrupted. Use it for timing, but use history/committed items for full text. Text & transcriptions
• If you need to know when audio actually finishes (when not interrupted), subscribe to playback_finished on audio output. Detect finish
Would you like a minimal code snippet showing conversation_item_added handling and the interrupt → session.aclose() flow?
Sources: Pipeline nodes and hooks | LiveKit docs | How to capture real-time agent transcriptions during conversations | How to detect when an agent has finished speaking | Text and transcriptions | LiveKit docs | ElevenLabs TTS plugin guide | LiveKit Docslively-coat-10905
11/04/2025, 10:01 AMtall-airline-16756
11/04/2025, 10:02 AMconversation_item_added, and flushing on interrupt to render the complete message.
from livekit.agents import Agent, AgentSession
# 1) Capture committed items (use this to display full agent text)
def on_item_added(ev):
if ev.item.role == "assistant":
# Render/store the full agent text for your UI
render_full_agent_text(ev.item.text)
async def run_session():
session = AgentSession(
# optional: enable tighter TTS-aligned transcripts if supported
# use_tts_aligned_transcript=True,
)
session.on("conversation_item_added", on_item_added)
# optional: if you want a realtime feed too, capture fragments
class MyAgent(Agent):
async def transcription_node(self, task, out):
async for fragment in await super().transcription_node(task, out):
save_fragment(fragment) # interim fragments if you want them
yield fragment
agent = MyAgent(session=session)
await agent.start()
return session, agent
# 2) On user interrupt: flush the incomplete turn so history has the full text
async def handle_interrupt(session: AgentSession):
await session.aclose() # flush incomplete agent turn to history
# Then pull from history (or rely on on_item_added handler)
last = session.history[-1] if session.history else None
if last and last.role == "assistant":
render_full_agent_text(last.text)
# 3) (Optional) If you need to know when audio actually finished
def on_playback_finished(ev):
notify_clients_audio_done({"interrupted": ev.interrupted})
# subscribe if needed:
# session.output.audio.on("playback_finished", on_playback_finished)
References:
• Text and transcriptions: docs
• Capture real-time transcripts and committed items: guide
• Pipeline nodes and hooks (transcription_node): docs
• Turn interruption behavior: docs
• Detect when audio finishes: guide
Want this adapted for Node.js or with a concrete UI render example?
Sources: Text and transcriptions | LiveKit docs | How to capture real-time agent transcriptions during conversations | Pipeline nodes and hooks | LiveKit docs | Turn detection and interruptions | LiveKit Docs | Understanding Transcript Timing in Voice Conversationslively-coat-10905
11/04/2025, 10:09 AMtall-airline-16756
11/04/2025, 10:09 AMconversation_item_added, or call session.aclose() on interrupt and read from session.history). This is what ensures you have the complete message even if TTS was interrupted. Guide Turns
• Frontend: Yes—display the text you receive from the backend (either via your event/RPC from conversation_item_added or after the interrupt flush). If you also care about audio-finish state, listen for a backend signal of playback_finished. Detect finish
If you want, I can outline the minimal event payload you can emit from the backend for the frontend to render.