Twilio

class pipecat.serializers.twilio.TwilioFrameSerializer(stream_sid, call_sid=None, account_sid=None, auth_token=None, params=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.

Parameters:
  • stream_sid (str)

  • call_sid (str | None)

  • account_sid (str | None)

  • auth_token (str | None)

  • params (InputParams | None)

_stream_sid

The Twilio Media Stream SID.

_call_sid

The associated Twilio Call SID.

_account_sid

Twilio account SID for API access.

_auth_token

Twilio authentication token for API access.

_params

Configuration parameters.

_twilio_sample_rate

Sample rate used by Twilio (typically 8kHz).

_sample_rate

Input sample rate for the pipeline.

_resampler

Audio resampler for format conversion.

class InputParams(*, twilio_sample_rate=8000, sample_rate=None, auto_hang_up=True)[source]

Bases: BaseModel

Configuration parameters for TwilioFrameSerializer.

Parameters:
  • twilio_sample_rate (int)

  • sample_rate (int | None)

  • auto_hang_up (bool)

twilio_sample_rate

Sample rate used by Twilio, defaults to 8000 Hz.

Type:

int

sample_rate

Optional override for pipeline input sample rate.

Type:

int | None

auto_hang_up

Whether to automatically terminate call on EndFrame.

Type:

bool

twilio_sample_rate: int
sample_rate: int | None
auto_hang_up: bool
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

property type: FrameSerializerType

Gets the serializer type.

Returns:

The serializer type, either TEXT or BINARY.

async setup(frame)[source]

Sets up the serializer with pipeline configuration.

Parameters:

frame (StartFrame) – The StartFrame containing pipeline configuration.

async serialize(frame)[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 (Frame) – The Pipecat frame to serialize.

Returns:

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

Return type:

str | bytes | None

async deserialize(data)[source]

Deserializes Twilio WebSocket data to Pipecat frames.

Handles conversion of Twilio media events to appropriate Pipecat frames.

Parameters:

data (str | bytes) – The raw WebSocket data from Twilio.

Returns:

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

Return type:

Frame | None