Labsco
openai logo

twilio-voice-conversation-relay

✓ Official4,081

by openai · part of openai/plugins

Build AI-powered voice agents using Twilio ConversationRelay. Handles real-time speech recognition (ASR), text-to-speech (TTS), and bidirectional audio streaming via WebSocket. Covers TwiML setup, WebSocket message types, LLM integration, streaming responses, and voice provider configuration. Use this skill to build voice bots, IVR replacements, or real-time AI voice assistants on Twilio calls.

🧩 One of 7 skills in the openai/plugins package — works on its own, and pairs well with its siblings.

This is the playbook your agent receives when the skill activates — you don't need to read it to use the skill, but it's here to audit before installing.

Overview

ConversationRelay connects Twilio's telephony layer to your app via a persistent WebSocket. Twilio handles ASR (speech-to-text) and TTS (text-to-speech); your app receives transcripts, calls an LLM, and sends text back for playback.

Caller ←→ Twilio (ASR/TTS) ←→ WebSocket ←→ Your App ←→ LLM

Key Patterns

WebSocket Message Types

Received from Twilio:

TypeWhenKey fields
connectedWebSocket openedcallSid, streamSid
promptUser finished speakingvoicePrompt (transcript)
interruptUser interrupted TTS
dtmfUser pressed keypad keydigit
errorAn error occurreddescription

Sent to Twilio:

TypePurposeKey fields
textSend TTS responsetoken (text), last (bool)
interruptStop current TTS
endHang up the callreason

Stream LLM Responses Token-by-Token

Lower latency by streaming as the LLM generates output — Twilio starts speaking before the full response is ready.

Python

async for chunk in llm_stream:
    await websocket.send(json.dumps({"type": "text", "token": chunk, "last": False}))
await websocket.send(json.dumps({"type": "text", "token": "", "last": True}))

Node.js

for await (const chunk of llmStream) {
    ws.send(JSON.stringify({ type: "text", token: chunk, last: false }));
}
ws.send(JSON.stringify({ type: "text", token: "", last: true }));

Voice Configuration

Python

connect.conversation_relay(
    url="wss://yourapp.com/ws/voice",
    voice="en-US-Neural2-F",
    language="en-US",
    transcription_provider="deepgram",
    speech_model="nova-2-phonecall",
    interrupt_by_dtmf=True,
)

Node.js

connect.conversationRelay({
    url: "wss://yourapp.com/ws/voice",
    voice: "en-US-Neural2-F",
    language: "en-US",
    transcriptionProvider: "deepgram",
    speechModel: "nova-2-phonecall",
    interruptByDtmf: true,
});

CANNOT

  • No raw audio access — Text in, text out only. For raw audio, use <Connect><Stream> (Media Streams).
  • Cannot mix with Media Streams<Connect><Stream> and <Connect><ConversationRelay> are mutually exclusive on the same call. No error — one is silently ignored.
  • No custom STT/TTS engines — Limited to Deepgram + Google (STT) and Deepgram + Amazon Polly + Google + ElevenLabs (TTS).
  • No mid-session voice/provider changes — Voice and provider are set at TwiML time. Only language can be switched mid-session via WebSocket message.
  • No WebSocket auto-reconnection — If the WebSocket drops, the call disconnects. Implement recovery via <Connect action> URL.
  • Voice only — No SMS/messaging support. For omnichannel, use Conversation Orchestrator.
  • No built-in memory or context — BYO conversation history and context management.
  • No LLM integration — Pure transport layer. You bring your own LLM via the WebSocket server.
  • No server-side recording via REST APIrecord:true on the Calls API is silently ignored. Must use <Start><Recording> before <Connect> in TwiML.
  • Not PCI compliant with Voice Intelligence v2 — Do not enable intelligenceService in PCI workflows.
  • ElevenLabs requires account enablement — Accounts without ElevenLabs access get error 64101. Voice IDs (not human-readable names) are required.
  • Cannot use ConversationRelay without onboarding — Not available immediately on a new account
  • Cannot use non-TLS WebSocket — Server must be reachable via wss:// (TLS required)
  • Cannot stream audio without last: true — Twilio won't play audio until it sees a last: true token in the stream

Next Steps

  • Place outbound calls: twilio-voice-outbound-calls
  • Standard IVR without AI: twilio-voice-twiml