kind-school-76204
07/25/2025, 8:22 AMsession.aclose() when I determine that the agent cannot recover from the error — for example, inside the on_error callback like in this example.
In my tests, when the session is closed this way, the agent’s on_close handler is properly triggered. However, the client’s onDisconnect callback does not seem to be called.
What’s the recommended way to propagate this kind of error to the client?
Thanks in advance!tall-airline-16756
07/25/2025, 8:22 AMtall-airline-16756
07/25/2025, 8:23 AMsession.aclose(), you can utilize the CloseEvent that is automatically emitted when the session closes. This event includes an error property containing the error that caused the session to close, if applicable.
Here’s how you can handle this:
@session.on("close")
def on_close(ev: CloseEvent):
if ev.error:
# Handle the error that caused the session to close
print(f"Session closed due to error: {ev.error}")
# Propagate error information to your client
else:
# Normal session closure
print("Session closed normally")
This approach ensures that any errors occurring during the agent session are properly communicated to your client application when the session closes.
Sources: Events and error handling | LiveKit Docs | livekit-agents/livekit/agents/voice/room_io/room_io.py