How can I get the current speech handle to check i...
# ask-ai
w
How can I get the current speech handle to check if the agent is speaking or not, and then do something only if the agent isn't already speaking?
t
Thanks for reaching out, Alex. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to see whether the agent is currently speaking, you can easily check the
current_speech
property of the
AgentSession
. Here’s a quick example to help you out:
Copy code
current_speech = ctx.session.current_speech
if current_speech and not current_speech.done():
    print("Agent is currently speaking.")
else:
    print("Agent is not speaking.")
Here’s a little breakdown: • `current_speech`: This gives you the active
SpeechHandle
, or
None
if the agent isn’t speaking at the moment. • `done()`: This tells you if the speech has finished. If it returns
False
, that means the agent is still talking. So, with this, you can easily check if the agent is speaking and decide what to do next. Hope this helps!
w
How can I wait on the speech to finish?
t
Here’s how you can wait for the speech to finish in two simple ways: 1. *Just
await
the `SpeechHandle`:*`handle = session.say("Your message here.") await handle`This will pause things until the speech is done playing. 2. *Or use the
wait_for_playout()
method:*`handle = session.generate_reply(instructions="Your instructions here.") await handle.wait_for_playout()`This one waits for the whole assistant turn to finish playing, including any wrap-up steps. Either way, you’ll make sure the speech finishes before moving on. Sources: Agent speech and audio | LiveKit Docs | Agent speech | LiveKit Docs