what is the difference between add_done_callback a...
# ask-ai
e
what is the difference between add_done_callback and wait_for_playout
t
Thanks for reaching out, Sameer. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
add_done_callback and wait_for_playout are both used with
SpeechHandle
objects in LiveKit to coordinate when speech playout is complete, but they serve different purposes: • add_done_callback: Registers a callback to be executed when playout finishes. It is non-blocking and event-driven, allowing your code to continue executing while the callback runs automatically once the speech completes. • wait_for_playout: Is an
async
method that must be awaited, causing your code to pause until the speech playout is fully done (blocking, in an async sense). Use this when you need to guarantee that speech has finished before continuing. Both work with the same underlying completion logic, but add_done_callback fits event/callback patterns, while wait_for_playout fits async/await, pausing execution until completion. You can find example usage in the source code and documentation:
Copy code
# Registering a callback
handle.add_done_callback(lambda _: print("speech done"))

# Waiting for playout to finish
await handle.wait_for_playout()
Choose based on whether you want to act on completion asynchronously (callback) or need to block until completion (
await
). • Citation: Sources: livekit-agents/livekit/agents/voice/agent_activity.py