Skip to main content

Overview

TwilioFrameSerializer enables integration with Twilio’s Media Streams WebSocket protocol, allowing your Pipecat application to handle phone calls via Twilio’s voice services with bidirectional audio conversion, DTMF event handling, and automatic call termination.

Installation

The TwilioFrameSerializer does not require any additional dependencies beyond the core Pipecat library:
pip install "pipecat-ai"

Prerequisites

Twilio Account Setup

Before using TwilioFrameSerializer, you need:
  1. Twilio Account: Sign up at Twilio Console
  2. Phone Number: Purchase a Twilio phone number with voice capabilities
  3. Media Streams: Configure your phone number to use Media Streams
  4. Webhook Configuration: Set up webhook endpoints for call handling

Required Environment Variables

  • TWILIO_ACCOUNT_SID: Your Twilio Account SID for authentication
  • TWILIO_AUTH_TOKEN: Your Twilio Auth Token for API operations

Required Configuration

  • Stream SID: Provided by Twilio during Media Stream connection
  • Call SID: Required for automatic call termination (optional)

Key Features

  • Bidirectional Audio: Convert between Pipecat and Twilio audio formats
  • DTMF Handling: Process touch-tone events from callers
  • Auto Hang-up: Terminate calls via Twilio’s REST API
  • μ-law Encoding: Handle Twilio’s standard audio encoding format

Configuration

stream_sid
str
required
The Twilio Media Stream SID.
call_sid
str
default:"None"
The associated Twilio Call SID. Required when auto_hang_up is enabled.
account_sid
str
default:"None"
Twilio account SID. Required when auto_hang_up is enabled.
auth_token
str
default:"None"
Twilio auth token. Required when auto_hang_up is enabled.
region
str
default:"None"
Twilio region (e.g., "au1", "ie1"). Must be specified together with edge.
edge
str
default:"None"
Twilio edge location (e.g., "sydney", "dublin"). Must be specified together with region.
params
InputParams
default:"None"
Configuration parameters for audio and hang-up behavior. See InputParams below.

InputParams

ParameterTypeDefaultDescription
twilio_sample_rateint8000Sample rate used by Twilio (Hz).
sample_rateintNoneOptional override for pipeline input sample rate. When None, uses the pipeline’s configured rate.
auto_hang_upboolTrueWhether to automatically terminate the call on EndFrame or CancelFrame.
ignore_rtvi_messagesboolTrueWhether to ignore RTVI protocol messages during serialization.

Usage

Basic Setup

from pipecat.serializers.twilio import TwilioFrameSerializer
from pipecat.transports.network.websocket_server import WebSocketServerTransport

serializer = TwilioFrameSerializer(
    stream_sid=stream_sid,
    call_sid=call_sid,
    account_sid=os.getenv("TWILIO_ACCOUNT_SID"),
    auth_token=os.getenv("TWILIO_AUTH_TOKEN"),
)

transport = WebSocketServerTransport(
    params=WebSocketServerParams(
        audio_out_enabled=True,
        add_wav_header=False,
        serializer=serializer,
    )
)

Without Auto Hang-up

serializer = TwilioFrameSerializer(
    stream_sid=stream_sid,
    params=TwilioFrameSerializer.InputParams(
        auto_hang_up=False,
    ),
)

Notes

  • Auto hang-up requires credentials: When auto_hang_up is enabled (the default), you must provide call_sid, account_sid, and auth_token. A ValueError is raised at initialization if any are missing.
  • Region and edge pairing: If either region or edge is specified, both must be provided. Twilio’s API uses the FQDN format api.{edge}.{region}.twilio.com.
  • Audio format: Twilio uses 8kHz mu-law (PCMU) audio encoding. The serializer automatically converts between this format and Pipecat’s PCM audio.
  • DTMF support: Touch-tone digit events from callers are converted to InputDTMFFrame objects.