Hi all, I'm currently working on handling agent e...
# ask-ai
k
Hi all, I'm currently working on handling agent errors, and I plan to call
session.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!
t
Thanks for reaching out, Jex. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To propagate errors to the client when using
session.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:
Copy code
@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