Skip to content

Pass disconnection details to callback_end_session #892

Closed

Description

@corbin-caldwell

Description

In Conversation and AsyncConversation (src/elevenlabs/conversational_ai/conversation.py), callback_end_session is called with no arguments (Callable[[], None] / Callable[[], Awaitable[None]]).

When the WebSocket closes in _run(), the SDK catches ConnectionClosedOK and Exception, logs non-1000 errors, and calls end_session(), discarding the WebSocket code, reason, and exception. Callers cannot distinguish between:

  • User-initiated end_session() calls
  • Normal agent/server closure (1000)
  • Configuration/validation errors (1008, e.g., missing required dynamic variables)
  • Auth/session errors (3000, e.g., invalid or expired session token)
  • Session takeover on reconnect (4001, "This conversation was continued in another session.")
  • Unexpected server/network errors (1002, 1011, etc.)

Proposal

Align with onDisconnect(details: DisconnectionDetails) in @elevenlabs/client:

Pass a DisconnectionDetails object to callback_end_session containing:

  • reason: "user" | "agent" | "error"
  • close_code: Optional[int]
  • close_reason: Optional[str]
  • message: Optional[str] (for errors/exceptions)

And we could maintain backwards compatibility for existing zero-argument callback_end_session callbacks (e.g., via inspect.signature).

Code example

No response

Additional context

No response

Activity

  1. added
    bug
    on Oct 13, 2026
  2. gauravch-code commented on Oct 21, 2026

    @gauravch-code
    Contributor

    I'd like to work on this. I checked the current sync and async shutdown paths; both still call callback_end_session() without forwarding the close details.

    My proposed approach is a small DisconnectionDetails type carrying the close code, close reason, and user/agent/error classification, exposed through an optional callback_disconnect(details) callback. The existing zero-argument callback_end_session would remain unchanged. That avoids inspecting callback signatures or catching TypeError that might come from inside a user's callback.

    I'd add local WebSocket regression tests for normal server closure, error closure, and user-initiated shutdown, covering sync/async parity and existing callbacks. These shouldn't need a hosted agent or audio hardware.

    Would you prefer this separate callback, or extending callback_end_session as suggested in the issue? I'd like to confirm the public API before implementing it.

  3. corbin-caldwell commented on Oct 26, 2026

    @corbin-caldwell
    Author

    Hey @gauravch-code , adding a separate callback would work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions