microscopic-tiger-52909
08/30/2025, 4:54 PMtall-airline-16756
08/30/2025, 4:54 PMtall-airline-16756
08/30/2025, 4:55 PM# Start the speech and wait for it to finish
handle = session.say("Let me transfer you now.")
await handle.wait_for_playout()
# Now, execute the transfer tool
await perform_tool_action()
• Using `RunContext.wait_for_playout()`: If you’re inside a function tool, this method helps you pause until the agent finishes talking before running the tool. Like this:
@function_tool
async def transfer_tool(ctx: RunContext):
# Wait for the agent to finish speaking
await ctx.wait_for_playout()
# Then do the transfer action
await perform_tool_action()
Also, to avoid any interruptions while the agent is speaking, you can set allow_interruptions=False when calling say(), or use RunContext.disallow_interruptions() inside a function tool.
If you want me to walk you through this or have any questions, just let me know—I’m here to help!
Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Agent speech and audio | LiveKit Docsmicroscopic-tiger-52909
08/30/2025, 4:56 PMtall-airline-16756
08/30/2025, 4:57 PMwait_for_playout works smoothly with real-time models like OpenAI Realtime. It makes sure the agent finishes speaking before moving on to the next steps, even when dealing with real-time speech pipelines.
With real-time models, SpeechHandle.wait_for_playout() keeps an eye on the speech’s progress and waits until the audio finishes playing or gets interrupted. This way, any follow-up actions, like running tools, happen only after the speech is completely done.
If you’re working with a function tool, you can use RunContext.wait_for_playout() to pause until the agent’s speech wraps up before running the tool—this helps avoid any tricky circular dependencies.
Feel free to ask if you want me to walk you through it with examples or if anything’s unclear!
Sources: livekit-agents/livekit/agents/beta/workflows/email_address.py | Realtime model integrations | LiveKit Docs