Using real-time streaming
AssemblyAI's Real-Time Transcription service allows you to transcribe live audio streams with high accuracy and low latency. By streaming your audio data to our secure WebSocket API, you can receive transcripts back within a few hundred milliseconds, and our system continues to revise these transcripts with greater accuracy over time as more context arrives.
In this guide, you'll learn how to establish a WebSocket connection, send audio data, and receive partial and final transcription results. Real-Time Transcription requires 16-bit signed integer PCM-encoded, single-channel audio, matching a predefined sample rate. You can send between 100 and 2000 milliseconds of audio over a WebSocket at a time.
Get started
Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for a free account and get your API key from your dashboard. Please note that this feature is available for paid accounts only. If you're on the free plan, you'll need to upgrade.
The entire source code of this guide can be viewed here.
Step-by-step instructions
- 1
To use the microphone stream you need to install pyaudio. Mac and Linux users also need to install
portaudio
first. Additionally, install thewebsocket-client
package:# (Mac)
brew install portaudio
# (Debian/Ubuntu)
apt install portaudio19-dev
pip install pyaudio
pip install websocket-clientIn your code, first setup the microphone stream and then establish a WebSocket connection with the real-time endpoint by using a WebSocket client and connecting to
wss://api.assemblyai.com/v2/realtime/ws
.Authenticate your request by including your API key in the authorization header of your WebSocket connection, and provide the sample rate of your audio data as a query parameter to the real-time endpoint.
- 2
Update the WebSocket's
message
event to load the incoming data as JSON and extract the text - 3
Update the WebSocket's
message
event to print the transcript, conditionally prepended with a string that signifies if the transcript is partial or final. - 4
Update the WebSocket's
open
event to stream data from the microphone, sent base-64 encoded via JSON - 5
Optional: Add up to 2,500 characters of custom vocabulary to your real-time session by including the
word_boost
parameter as an optional query parameter in the URL.See also Adding Custom Vocabulary
- 6
Update the WebSocket's
error
event to handle WebSocket errors and application-level errors, including bad sample rate, authentication failure, insufficient funds, and more. See also Closing and Status Codes for a list of errors.Additionally, update the WebSocket's
close
event.
Audio Requirements
The raw audio data in the audio_data
field above must comply with a strict encoding format. This is because we don't do any transcoding to your data, we send it directly to the model for transcription to reduce latency. The encoding of your audio must be in:
- WAV PCM16
- A sample rate that matches the value of the
sample_rate
query param you supply - Single-channel
- 100 to 2000 milliseconds of audio per message
Request Types
These are the types of requests that can be sent to the WebSocket API.
Opening a Session
When opening a Session you can pass the following query attributes to the WebSocket URL:
sample_rate
The sample rate of the streamed audio.
Example: wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000
word_boost
See also Adding Custom Vocabulary
token
See also Creating Temporary Authentication Tokens
Sending Audio
When sending audio over the WebSocket connection, you should send a JSON payload with the following parameters:
Field | Example | Description |
---|---|---|
audio_data | "UklGRtjIAABXQVZFZ" | Raw audio data, base64 encoded. This can be the raw data recorded directly from a microphone or read from an audio file. |
Terminating a Session
When you've completed your session, clients should send a JSON message with the following field.
Field | Example | Description |
---|---|---|
terminate_session | true | A boolean value to communicate that you wish to end your real-time session forever. |
After requesting session termination, the server will send the remaining transcript messages, followed by a SessionTerminated message.
Response Types
These are the types of responses that can be received from the WebSocket API.
Session Start
Once your request is authorized and connection established, your client receives a SessionBegins
message with the following JSON data:
Field | Example | Description |
---|---|---|
message_type | "SessionBegins" | Describes the type of the message. |
session_id | "d3e8c537-2f11-494b-b497-e59a434588bd" | Unique identifier for the established session. |
expires_at | "2023-05-24T08:09:10.161850" | Timestamp when this session will expire. |
Transcripts
Our Real-Time Transcription pipeline uses a two-phase transcription strategy, broken into partial and final results.
Partial Transcripts
As you send audio data to the API, the API immediately starts responding with Partial Results. The following keys are returned from the WebSocket API.
Field | Example | Description |
---|---|---|
message_type | "PartialTranscript" | Describes the type of message. |
audio_start | 0 | Start time of audio sample relative to session start, in milliseconds. |
audio_end | 1500 | End time of audio sample relative to session start, in milliseconds. |
confidence | 0.987190506414702 | The confidence score of the entire transcription, between 0 and 1. |
text | "there is a house in new orleans" | The partial transcript for your audio. |
words | [{"start": 0, "end": 440, "confidence": 1.0, "text": "there"}, ...] | An array of objects, with the information for each word in the transcription text. Includes the start /end time (in milliseconds) of the word, the confidence score of the word, and the text (i.e. the word itself). |
created | "2023-05-24T08:09:10.161850" | The timestamp for the partial transcript. |
Final Transcripts
After you've received your partial results, our model continues to analyze incoming audio and, when it detects the end of an "utterance" (usually a pause in speech), it'll finalize the results sent to you so far with higher accuracy, as well as add punctuation and casing to the transcription text.
The following keys are returned from the WebSocket API when Final Results are sent:
Field | Example | Description |
---|---|---|
message_type | "FinalTranscript" | Describes the type of message. |
audio_start | 0 | Start time of audio sample relative to session start, in milliseconds. |
audio_end | 1500 | End time of audio sample relative to session start, in milliseconds. |
confidence | 0.997190506414702 | The confidence score of the entire transcription, between 0 and 1. |
text | "There is a house in New Orleans" | The partial transcript for your audio. |
words | [{"start": 0, "end": 440, "confidence": 1.0, "text": "There"}, ...] | An array of objects, with the information for each word in the transcription text. Includes the start /end time (in milliseconds) of the word, the confidence score of the word, and the text (i.e. the word itself). |
created | "2023-05-24T08:09:10.161850" | The timestamp for the final transcript. |
punctuated | true | Whether the text has been punctuated and cased. |
text_formatted | true | Whether the text has been formatted (e.g. Dollar -> $) |
Session Terminated
After requesting session termination, the server will send the remaining transcript messages, followed by a SessionTerminated
message.
Your client receives a SessionTerminated
message with the following JSON data:
Field | Example | Description |
---|---|---|
message_type | "SessionTerminated" | Describes the type of the message. |
Closing and Status Codes
The WebSocket specification provides standard errors.
Our API provides application-level WebSocket errors for well-known scenarios:
Error Condition | Status Code | Message |
---|---|---|
bad sample rate | 4000 | "Sample rate must be a positive integer" |
auth failed | 4001 | "Not Authorized" |
insufficient funds | 4002 | "Insufficient Funds" |
free tier user | 4002 | "This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account" |
attempt to connect to nonexistent session id | 4004 | "Session not found" |
session expired | 4008 | "Session Expired" |
attempt to connect to closed session | 4010 | "Session previously closed" |
rate limited | 4029 | "Client sent audio too fast" |
unique session violation | 4030 | "Session is handled by another WebSocket" |
session times out | 4031 | "Session idle for too long" |
audio too short | 4032 | "Audio duration is too short" |
audio too long | 4033 | "Audio duration is too long" |
bad json | 4100 | "Endpoint received invalid JSON" |
bad schema | 4101 | "Endpoint received a message with an invalid schema" |
too many streams | 4102 | "This account has exceeded the number of allowed streams" |
reconnected | 4103 | "This session has been reconnected. This WebSocket is no longer valid." |
reconnect attempts exhausted | 1013 | "Temporary server condition forced blocking client's request" |
Quotas and Limits
The following limits are imposed to ensure performance and service quality.
- Idle Sessions - Sessions that don't receive audio within 1 minute will be terminated.
- Session Limit - 32 sessions at a time for paid users. Please contact us if you need to increase this limit. Free-tier users must upgrade their account to use real-time streaming.
- Session Uniqueness - Only one WebSocket per session.
- Audio Sampling Rate Limit - Customers must send data in near real-time. If a client sends data faster than 1 second of audio per second for longer than 1 minute, we'll terminate the session.
Adding Custom Vocabulary
Developers can also add up to 2500 characters of custom vocabulary to their real-time session by adding the optional query parameter word_boost
in the URL. The parameter should map to a JSON encoded list of strings as shown in this Python example:
import json
from urllib.parse import urlencode
sample_rate = 16000
word_boost = ["foo", "bar"]
params = {"sample_rate": sample_rate, "word_boost": json.dumps(word_boost)}
url = f"wss://api.assemblyai.com/v2/realtime/ws?{urlencode(params)}"
Creating Temporary Authentication Tokens
In some cases, a developer needs to authenticate on the client-side and doesn't want to expose their AssemblyAI API key. You can do this by sending a POST
request to https://api.assemblyai.com/v2/realtime/token
with the parameter expires_in: {TTL in seconds}
. Below is a quick example in curl.
The expires_in
parameter must be greater than or equal to 60 seconds.
curl --request POST \
--url https://api.assemblyai.com/v2/realtime/token \
--header 'authorization: YOUR_API_TOKEN' \
--header 'Content-Type: application/json' \
--data '{"expires_in": 60}'
In response you'll receive the following JSON output:
{
"token": "b2e3c6c71d450589b2f4f0bb1ac4efd2d5e55b1f926e552e02fc0cc070eaedbd"
}
A developer can now use this temporary token in the browser to authenticate a new WebSocket session with the following endpoint wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000&token={New Temp Token}
. For example:
let socket
const token = 'b2e3c6c71d450589b2f4f0bb1ac4efd2d5e55b1f926e552e02fc0cc070eaedbd'
socket = new WebSocket(
`wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000&token=${token}`
)
Conclusion
Real-Time Transcription is a powerful feature with even more powerful possibilities for integration. On the AssemblyAI blog, you can learn about using Real-Time Transcription to:
- Automatically Transcribe Zoom Calls in Real Time
- Transcribe Twilio Phone Calls
- Connect to the Real-Time Transcription API using a PyAudio stream
You can also find an example of using Express.js for Real-Time Transcription on GitHub.