ConversationRelay, including the <ConversationRelay>
TwiML noun and API, uses artificial intelligence or machine learning technologies. By enabling or using any features or functionalities within Programmable Voice that Twilio identifies as using artificial intelligence or machine learning technology, you acknowledge and agree to certain terms. Your use of these features or functionalities is subject to the terms of the Predictive and Generative AI or ML Features Addendum.
ConversationRelay isn't Payment Card Industry (PCI) compliant or a HIPAA eligible service and doesn't support Voice workflows that are subject to HIPAA or PCI.
ConversationRelay is currently available as a Public Beta product, and Twilio may change the information in this document at any time. This means that some features aren't yet implemented, and others may change before the product becomes Generally Available. Public Beta products aren't covered by a Twilio Service Level Agreement. Learn more about Twilio's beta product support here.
Before using ConversationRelay, you need to complete the onboarding steps and agree to the Predictive and Generative AI/ML Features Addendum. See the ConversationRelay Onboarding Guide for more details.
The <ConversationRelay>
TwiML noun under the <Connect>
verb routes a call to our ConversationRelay service, enabling advanced AI-powered voice interactions. With ConversationRelay, we simplify voice AI applications by handling the complexities of live, synchronous voice calls. These complexities include Speech-to-Text (STT) and Text-to-Speech (TTS) conversions, session management, and low-latency communication with your application. This allows your system to focus on processing conversational AI logic and sending back responses seamlessly.
In a typical setup, <ConversationRelay>
connects to your AI application through a WebSocket, allowing real-time and event-based interaction. Your application receives transcribed caller speech in structured messages and sends responses as text, which ConversationRelay converts to speech and plays back to the caller. This setup is ideal for customer service, virtual assistants, and other scenarios where real-time, AI-driven voice interactions are crucial.
Before you can use <ConversationRelay>
, make sure you've completed the onboarding steps and configured your Twilio account accordingly.
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Connect action="https://myhttpserver.com/connect_action">4<ConversationRelay url="wss://mywebsocketserver.com/websocket" welcomeGreeting="Hi! Ask me anything!" />5</Connect>6</Response>
action
(optional): The URL that Twilio will request when the <Connect>
verb ends.url
(required): The URL of your WebSocket server (must use the wss://
protocol).welcomeGreeting
(optional): The message automatically played to the caller after we answer the call and establish the WebSocket connection.When the TwiML execution is complete, Twilio will make a callback to the action
URL with call information and the return parameters from ConversationRelay
.
The <ConversationRelay>
noun supports the following attributes:
Attribute name | Description | Default value | Required |
---|---|---|---|
url | The URL to your WebSocket server (must use wss:// ). | Required | |
welcomeGreeting | The message automatically played to the caller after we answer the call and establish the WebSocket connection. | Optional | |
welcomeGreetingInterruptible | Specifies if the caller can interrupt the welcomeGreeting with speech. | true | Optional |
language | The language code (for example, en-US ) that applies to both Speech-to-Text (STT) and Text-to-Speech (TTS). Setting this attribute is equivalent to setting both ttsLanguage and transcriptionLanguage . | en-US | Optional |
ttsLanguage | The default language code to use for TTS when the text token message doesn't specify a language. If you set both attributes, this one overrides the language attribute. You can modify this via the ttsLanguage field in the language message you send through the Service Provider Interface (SPI). | Optional | |
ttsProvider | The provider for TTS. Available choices are Google and Amazon . | Google | Optional |
voice | The voice used for TTS. Choices vary based on the ttsProvider . For details, refer to the Twilio TTS Voices. We list additional voices available for ConversationRelay below. | en-US-Journey-O (Google), Joanna-Neural (Amazon) | Optional |
transcriptionLanguage | The language code to use for STT when the session starts. If you set both attributes, this one overrides the language attribute for the transcription language. You can modify this via the transcriptionLanguage field in the language message you send through the SPI. | Optional | |
transcriptionProvider | The provider for STT (Speech Recognition). Available choices are Google and Deepgram . | Google | Optional |
speechModel | The speech model used for STT. Choices vary based on the transcriptionProvider . Refer to the provider's documentation for an accurate list. | telephony (Google), nova-2-general (Deepgram) | Optional |
profanityFilter | Specifies whether to filter profanities out of the speech transcription. | true | Optional |
interruptible | Specifies if caller speech can interrupt TTS playback. | true | Optional |
dtmfDetection | Specifies whether the system sends Dual-tone multi-frequency (DTMF) keypresses over the WebSocket. Set to true to enable DTMF events. | false | Optional |
interruptByDtmf | Specifies if DTMF keys can interrupt TTS playback. | false | Optional |
en-US-Journey-O
(default)en-IN-Journey-D
en-IN-Journey-F
en-GB-Journey-D
en-GB-Journey-F
de-DE-Journey-D
de-DE-Journey-F
fr-CA-Journey-D
fr-CA-Journey-F
fr-FR-Journey-D
fr-FR-Journey-F
it-IT-Journey-D
it-IT-Journey-F
es-US-Journey-D
es-US-Journey-F
Amy-Generative
(en-US)Matthew-Generative
(en-US)Ruth-Generative
(en-US)Olivia-Generative
(en-AU)Danielle-Generative
(en-US)Joanna-Generative
(en-US)Stephen-Generative
(en-US)Ayanda-Generative
(en-ZA)Léa-Generative
(fr-FR)Lucia-Generative
(es-ES)Mía-Generative
(es-MX)Lupe-Generative
(es-US)Vicki-Generative
(de-DE)Include nested elements within <ConversationRelay>
for more granular configuration. For more information on configuring ConversationRelay, refer to the ConversationRelay Onboarding Guide.
The <Language>
element maps a language code to specific TTS and STT settings. Use this element to configure multiple languages for your session.
Example
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Connect>4<ConversationRelay url="wss://mywebsocketserver.com/websocket">5<Language code="sv-SE" ttsProvider="amazon" voice="Elin-Neural" transcriptionProvider="google" speechModel="long"/>6<Language code="en-US" ttsProvider="google" voice="en-US-Journey-O" />7</ConversationRelay>8</Connect>9</Response>
Attributes
Attribute name | Description of attributes | Default value | Required |
---|---|---|---|
code | The language code (for example, en-US ) that applies to both STT and TTS. | Required | |
ttsProvider | The provider for TTS. Choices are Google and Amazon . | Inherited from <ConversationRelay> | Optional |
voice | The voice used for TTS. Choices vary based on the ttsProvider . | Inherited from <ConversationRelay> | Optional |
transcriptionProvider | The provider for STT. Choices are Google and Deepgram . | Inherited from <ConversationRelay> | Optional |
speechModel | The speech model used for STT. Choices vary based on the transcriptionProvider . | Inherited from <ConversationRelay> | Optional |
Notes
<ConversationRelay>
and <Language>
, the settings in <Language>
take precedence.ConversationRelay
provides default settings for commonly used languages.The <Parameter>
element allows you to send custom parameters from the TwiML directly into the initial "setup" message sent over the WebSocket. These parameters appear under the customParameters
field in the JSON message.
Example
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Connect>4<ConversationRelay url="wss://mywebsocketserver.com/websocket">5<Parameter name="foo" value="bar"/>6<Parameter name="hint" value="Annoyed customer"/>7</ConversationRelay>8</Connect>9</Response>
Resulting Setup Message
1{2"type": "setup",3"sessionId": "VX00000000000000000000000000000000",4"callSid": "CA00000000000000000000000000000000",5"...": "...",6"customParameters": {7"foo": "bar",8"hint": "Annoyed customer"9}10}
Language settings refer to configurations for both Text-to-Speech and Speech-to-Text:
ttsLanguage
ttsProvider
voice
transcriptionLanguage
transcriptionProvider
speechModel
Configure language settings in two places:
<ConversationRelay>
: These serve as the default settings used when the session starts.<Language>
Elements: Each <Language>
element configures settings for a specific language code. You can include multiple <Language>
elements to support multiple languages.<ConversationRelay>
, the ttsLanguage
attribute overrides the language
attribute for the default TTS language.<ConversationRelay>
, the transcriptionLanguage
attribute overrides the language
attribute for the STT language.<Language>
element specifies the same code
attribute as in <ConversationRelay>
, the <Language>
element's settings take precedence.Default Values
language
: Defaults to en-US
if not specified.ttsProvider
: Defaults to Google
if not specified.transcriptionProvider
: Defaults to Google
if not specified.ttsProvider
attribute without the voice
attribute, the system uses a default voice for that provider.transcriptionProvider
attribute without the speechModel
attribute, the system uses a default model for that provider.voice
attribute without the ttsProvider
attribute, the system infers the provider from the default or specified ttsProvider
.speechModel
attribute without the transcriptionProvider
attribute, the system infers the provider from the default or specified transcriptionProvider
.For Speech-to-Text (STT) settings:
transcriptionLanguage
attribute to initiate the STT session.transcriptionProvider
and speechModel
attributes is invalid, the call disconnects, and the system reports an error in the action callback and error notifications.transcriptionLanguage
attribute during the session via the language
message you send through the Service Provider Interface (SPI).For Text-to-Speech (TTS) settings:
lang
property is present in the text
token message from the SPI, the service uses it to select the TTS voice.ttsProvider
and voice
attributes is invalid, the system sends an error message over the SPI.lang
property in the text
token, the service uses the current TTS language settings.ConversationRelay
interacts with your application server via a WebSocket connection specified by the url
attribute. Messages exchanged follow this Service Provider Interface (SPI) specification.
ConversationRelay sends this message immediately after establishing the WebSocket connection.
1{2"type": "setup",3"sessionId": "VX00000000000000000000000000000000",4"callSid": "CA00000000000000000000000000000000",5"from": "+14151234567",6"to": "+18881234567",7"direction": "inbound",8"...": "...",9"customParameters" : {10"foo": "bar"11}12}
ConversationRelay sends this message when the caller says something.
1{2"type": "prompt",3"voicePrompt": "Hi! Can you tell me about life?",4"lang": "en-US",5"last": true6}
ConversationRelay sends this message when you enable DTMF detection and the caller presses a key.
1{2"type": "dtmf",3"digit": "1"4}
ConversationRelay sends this message when the caller interrupts TTS playback by speaking.
1{2"type": "interrupt",3"utteranceUntilInterrupt": "Life is a complex set of",4"durationUntilInterruptMs": "460"5}
ConversationRelay sends this message when an error occurs during the session.
1{2"type": "error",3"description": "Invalid message received: { \"foo\" : \"bar\" }"4}
Send text tokens, and ConversationRelay converts them into speech.
1{2"type": "text",3"token": "Hello world!",4"last": false5}
Best practices
"last": true
when you have sent the final token of a message.Request to play media to the caller.
1{2"type": "play",3"source": "https://api.twilio.com/cowbell.mp3",4"loop": 1,5"preemptible": false6}
source
attribute (Required): The URL of the media to play.loop
attribute (Optional, default is 1
): Number of times to play the media. A value of 0
plays it 1,000 times (maximum).preemptible
attribute (Optional, default is false
): If set to true
, subsequent text
or play
messages will stop this media playback.Request to send DTMF digits to the caller. ConversationRelay sends digits as per Twilio's <Play>
digits
attribute.
1{2"type": "sendDigits",3"digits": "9www4085551212"4}
Change the transcription and TTS language during the session.
1{2"type": "language",3"ttsLanguage": "sv-SE",4"transcriptionLanguage": "en-US"5}
This affects future TTS and STT sessions.
End the session and return control of the call to Twilio through ConversationRelay
.
1{2"type": "end",3"handoffData": "{\"reasonCode\":\"live-agent-handoff\", \"reason\": \"The caller wants to talk to a real person\"}"4}
handoffData
attribute: Optional JSON string containing data to pass back in the action callback.When an action
URL is specified in the <Connect>
verb, ConversationRelay
will make a request to that URL when the <Connect>
verb ends. The request includes call information and session details.
Example Payloads
1{2"AccountSid": "AC00000000000000000000000000000000",3"CallSid": "CA00000000000000000000000000000000",4"CallStatus": "in-progress",5"From": "client:caller",6"To": "test:voxray",7"Direction": "inbound",8"ApplicationSid": "AP00000000000000000000000000000000",9"SessionId": "VX00000000000000000000000000000000",10"SessionStatus": "ended",11"SessionDuration": "25",12"HandoffData": "{\"reason\": \"The caller requested to talk to a real person\"}"13}
1{2"AccountSid": "AC00000000000000000000000000000000",3"CallSid": "CA00000000000000000000000000000000",4"CallStatus": "in-progress",5"From": "client:caller",6"To": "test:voxray",7"Direction": "inbound",8"ApplicationSid": "AP00000000000000000000000000000000",9"SessionId": "VX00000000000000000000000000000000",10"SessionStatus": "failed",11"SessionDuration": "10",12"ErrorCode": "39001",13"ErrorMessage": "Network connection to WebSocket server failed."14}
1{2"AccountSid": "AC00000000000000000000000000000000",3"CallSid": "CA00000000000000000000000000000000",4"CallStatus": "completed",5"From": "client:caller",6"To": "test:voxray",7"Direction": "inbound",8"ApplicationSid": "AP00000000000000000000000000000000",9"SessionId": "VX00000000000000000000000000000000",10"SessionStatus": "completed",11"SessionDuration": "35"12}
"last": true
when the message is complete.error
messages sent over the SPI to handle any issues promptly.language
message to switch languages dynamically during a session.end
message to gracefully end sessions when your application logic determines it's appropriate.last=true
. This enables ConversationRelay to identify the first sayable string.When using ConversationRelay for Text-to-Speech (TTS), it's essential to manage punctuation carefully in response tokens to avoid unintended truncation. Here are some best practices to ensure smooth playback of responses:
Setting the last
flag properly
When using punctuation within text tokens, ensure that the final token in the message includes "last": true
.
If you don't set the last
flag to true
, ConversationRelay assumes additional tokens are forthcoming. It will read up to the first punctuation mark (for example, period, comma, question mark) and then wait for further tokens. Without a final "last": true
token, playback may pause unexpectedly.
Using partial completions for streaming
For real-time streaming or partial completions, send each text token incrementally with last: false
, followed by a final token with last: true
.
This setup allows ConversationRelay to process and speak each part of the message without waiting, improving response time and user experience.
Punctuation handling in longer messages
For messages with complex punctuation, consider breaking them into smaller chunks, sending each chunk with appropriate punctuation, and using last: true
only for the final chunk. This approach ensures that ConversationRelay reads the message in its entirety without delays or unexpected stops.
When to Use last: false
Use last: false
for streaming scenarios where you want ConversationRelay to process and speak partial responses as they're received.
This is beneficial in low-latency applications, where your application generates responses progressively, allowing ConversationRelay to read each word or phrase in real time, improving the perceived responsiveness.
When setting up system prompts for Large Language Models (LLMs) in ConversationRelay
, consider these best practices to ensure optimal performance with Text-to-Speech (TTS) in ConversationRelay:
These prompt adjustments help improve the LLM-generated tokens' compatibility with voice output in ConversationRelay, enhancing clarity and consistency for users.
All WebSocket messages from ConversationRelay to your API follow the strict formats defined in these docs. Your application must also adhere to these specifications when sending messages back to ConversationRelay.
Following these practices helps maintain session stability and ensures compatibility with ConversationRelay's message handling.
Text-to-Speech (TTS) voice quality varies significantly by provider and voice type. While generative voices often offer higher fidelity and more natural-sounding responses, they can introduce additional latency and process TTS at a slower rate.
Selecting the right TTS voice involves balancing quality and performance, so thorough testing is essential before production deployment.
Speech-to-Text (STT) quality and latency can vary depending on the provider and the environment. Google and Deepgram each offer unique strengths for different scenarios, such as clean versus noisy audio environments.
Optimizing STT performance requires careful selection based on environment and model capabilities, so thorough testing is essential for achieving the best results.
In the event of a WebSocket connection error in ConversationRelay
, implement reconnection logic by initiating a new <Connect><ConversationRelay>
request:
ConversationRelay
: If you lose the WebSocket connection, handle the disconnect in your <Connect>
element's action
URL callback by returning new TwiML containing <Connect><ConversationRelay>
to restore the session.ConversationRelay
: Ensure the callSid remains the same to confirm continuity of the original call session.This approach helps maintain session stability and consistency following any connection disruptions.
When you switch languages during a session via an SPI message, ConversationRelay uses the pre-set voice associated with the new language that you configured in the initial TwiML setup. If you didn't configure a specific voice for that language, ConversationRelay will use its default voice for the selected language.
This setup ensures consistent voice behavior for each language by configuring it in TwiML before the call begins.
ConversationRelay supports both streaming and non-streaming modes for sending LLM responses. Each mode has unique trade-offs in latency and response fluidity:
For errors, such as messages that ConversationRelay doesn't understand, we will respond with an error message.
If your WebSocket sends unidentified messages to ConversationRelay and the last 10 messages remain unidentified, we will terminate the connection. The status code will be 1007 with the reason "Too many consecutive malformed messages." In that case, we will report an error 64105 "WebSocket Ended."
If the WebSocket disconnects unexpectedly in ConversationRelay, we don't reconnect, and the call disconnects with a failed
status.
ConversationRelay
, including the <ConversationRelay>
TwiML nouns and APIs, use artificial intelligence or machine learning technologies.
Our AI Nutrition Facts for ConversationRelay
provide an overview of the AI feature you're using, so you can better understand how the AI is working with your data. The below AI Nutrition Label details the ConversationRelay AI qualities. For more information and the glossary regarding the AI Nutrition Facts Label, refer to our AI Nutrition Facts page.
ConversationRelay uses the Default Base Model provided by the Model Vendor. The Base Model is not trained using Customer Data.
ConversationRelay uses the Default Base Model provided by the Model Vendor. The Base Model is not trained using Customer Data.
Base Model is not trained using any Customer Data.
Customer Data is not stored or retained in the Base Model.
Customer can view and listen to the input and output in the customer's own terminal.
Compliance
Customer can view and listen to the input and output in the customer's own terminal.
Customer can view and listen to the input and output in the customer's own terminal.
Customer is responsible for human review.
Learn more about this label at nutrition-facts.ai
ConversationRelay uses the Default Base Model provided by the Model Vendor. The Base Model is not trained using Customer Data.
ConversationRelay uses the Default Base Model provided by the Model Vendor. The Base Model is not trained using Customer Data.
Base Model is not trained using any Customer Data.
Customer Data is not stored or retained in the Base Model.
Customer can view and listen to the input and output in the customer's own terminal.
Compliance
Customer can view and listen to the input and output in the customer's own terminal.
Customer can view and listen to the input and output in the customer's own terminal.
Customer is responsible for human review.
Learn more about this label at nutrition-facts.ai
ConversationRelay uses the Default Base Model provided by the Model Vendor. The Base Model is not trained using Customer Data.
ConversationRelay uses the Default Base Model provided by the Model Vendor. The Base Model is not trained using Customer Data.
Base Model is not trained using any Customer Data.
Customer Data is not stored or retained in the Base Model.
Customer can view and listen to the input and output in the customer's own terminal.
Compliance
Customer can view and listen to the input and output in the customer's own terminal.
Customer can view and listen to the input and output in the customer's own terminal.
Customer is responsible for human review.
Learn more about this label at nutrition-facts.ai