twilio

Twilio Media Streams WebSocket protocol serializer for Pipecat.

class pipecat.serializers.twilio.TwilioFrameSerializer(stream_sid: str, call_sid: str | None = None, account_sid: str | None = None, auth_token: str | None = None, region: str | None = None, edge: str | None = None, params: InputParams | None = None)[source]

Bases: FrameSerializer

Serializer for Twilio Media Streams WebSocket protocol.

This serializer handles converting between Pipecat frames and Twilio’s WebSocket media streams protocol. It supports audio conversion, DTMF events, and automatic call termination.

When auto_hang_up is enabled (default), the serializer will automatically terminate the Twilio call when an EndFrame or CancelFrame is processed, but requires Twilio credentials to be provided.

class InputParams(*, ignore_rtvi_messages: bool = True, twilio_sample_rate: int = 8000, sample_rate: int | None = None, auto_hang_up: bool = True)[source]

Bases: InputParams

Configuration parameters for TwilioFrameSerializer.

Parameters:
  • twilio_sample_rate – Sample rate used by Twilio, defaults to 8000 Hz.

  • sample_rate – Optional override for pipeline input sample rate.

  • auto_hang_up – Whether to automatically terminate call on EndFrame.

  • ignore_rtvi_messages – Inherited from base FrameSerializer, defaults to True.

twilio_sample_rate: int
sample_rate: int | None
auto_hang_up: bool
__init__(stream_sid: str, call_sid: str | None = None, account_sid: str | None = None, auth_token: str | None = None, region: str | None = None, edge: str | None = None, params: InputParams | None = None)[source]

Initialize the TwilioFrameSerializer.

Parameters:
  • stream_sid – The Twilio Media Stream SID.

  • call_sid – The associated Twilio Call SID (optional, but required for auto hang-up).

  • account_sid – Twilio account SID (required for auto hang-up).

  • auth_token – Twilio auth token (required for auto hang-up).

  • region – Twilio region (e.g., “au1”, “ie1”). Must be specified with edge.

  • edge – Twilio edge location (e.g., “sydney”, “dublin”). Must be specified with region.

  • params – Configuration parameters.

async setup(frame: StartFrame)[source]

Sets up the serializer with pipeline configuration.

Parameters:

frame – The StartFrame containing pipeline configuration.

async serialize(frame: Frame) str | bytes | None[source]

Serializes a Pipecat frame to Twilio WebSocket format.

Handles conversion of various frame types to Twilio WebSocket messages. For EndFrames, initiates call termination if auto_hang_up is enabled.

Parameters:

frame – The Pipecat frame to serialize.

Returns:

Serialized data as string or bytes, or None if the frame isn’t handled.

async deserialize(data: str | bytes) Frame | None[source]

Deserializes Twilio WebSocket data to Pipecat frames.

Handles conversion of Twilio media events to appropriate Pipecat frames.

Parameters:

data – The raw WebSocket data from Twilio.

Returns:

A Pipecat frame corresponding to the Twilio event, or None if unhandled.