When should you await a session.say and when shoul...
# ask-ai
f
When should you await a session.say and when should you not?
t
Thanks for reaching out, Abdullah. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’m glad you asked about when to await
session.say()
! It’s best to await it when you want to make sure the spoken message finishes before moving on to the next steps. This is really helpful in situations where what happens next depends on the message being fully delivered—like closing a session or switching states. Here’s a simple example:
Copy code
await self.customer_session.say(
    "You are on the line with my supervisor. I'll be hanging up now."
)
await self.customer_session.aclose()
On the flip side, if the message doesn’t need to block what comes next and you want to keep things moving smoothly, you don’t have to await
session.say()
. For example, inside event handlers like
on_user_turn_completed
, you can just call
session.say()
without
await
so the event loop keeps running without delay:
Copy code
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage) -> None:
    self.session.say("session.say from on_user_turn_completed")
Also, it’s great to know that
session.say()
returns a
SpeechHandle
. This lets you keep an eye on playback status or handle interruptions, giving you the flexibility to use it in a non-blocking way while still staying in control. Sources: livekit-agents/livekit/agents/voice/agent_session.py | Agent speech and audio | LiveKit docs