websocket_service

Base websocket service with automatic reconnection and error handling.

class pipecat.services.websocket_service.ReportErrorCallback(*args, **kwargs)[source]

Bases: Protocol

Reports an error a websocket service ran into.

Implemented by the owning service, which usually pairs pushing the error frame with a connection-error event of its own.

class pipecat.services.websocket_service.WebsocketService(*, reconnect_backoff_min_wait: float = 4.0, reconnect_backoff_max_wait: float = 10.0, reconnect_on_error: bool = True, ws_close_timeout: float = 2.0, ws_send_timeout: float | None = 5.0, **kwargs)[source]

Bases: ABC

Base class for websocket-based services with automatic reconnection.

Provides websocket connection management, automatic reconnection with exponential backoff, connection verification, and error handling. Subclasses implement service-specific connection and message handling logic.

Reconnection gives up in two ways, both leaving the service unusable: the provider rejects the configuration, which no amount of retrying will fix, or the attempts are exhausted. Errors are reported through a report_error callback, which takes the same optional force_treat_as_permanent flag as push_error_frame(), so that giving up and saying so are the same act.

__init__(*, reconnect_backoff_min_wait: float = 4.0, reconnect_backoff_max_wait: float = 10.0, reconnect_on_error: bool = True, ws_close_timeout: float = 2.0, ws_send_timeout: float | None = 5.0, **kwargs)[source]

Initialize the websocket service.

Parameters:
  • reconnect_backoff_min_wait – Minimum time, in seconds, to wait between reconnection attempts.

  • reconnect_backoff_max_wait – Maximum time, in seconds, to wait between reconnection attempts.

  • reconnect_on_error – Whether to automatically reconnect on connection errors.

  • ws_close_timeout – Maximum time, in seconds, to wait for the peer to acknowledge the websocket closing handshake before dropping the connection. Applied to connections opened through _websocket_connect(). Increase it for peers that need longer to complete a graceful close.

  • ws_send_timeout – Maximum time, in seconds, a single send may wait on a peer that has stopped reading before the connection is dropped and the send raises ConnectionClosedError. Applied to connections opened through _websocket_connect(). Pass None to wait indefinitely.

  • **kwargs – Additional arguments (unused, for compatibility).

async send_with_retry(message, report_error: ReportErrorCallback)[source]

Attempt to send a message, retrying after reconnect if necessary.

Deprecated since version 1.14.0: No replacement. Will be removed in 2.0.0. Send on the websocket directly and leave reconnecting to the receive loop.