Text to speech over a WebSocket
Open one WebSocket, send text as it is produced, and receive each response's audio as it is synthesized.
The speech WebSocket lets you synthesize many utterances over one connection. You send text as conversation items, ask for a response, and the audio streams back as base64 deltas in the format you choose. A voice agent uses it to speak each sentence as soon as the language model produces it.
The events follow the OpenAI Realtime shapes for text input and audio output.
For a single clip from one request, use POST /v1/audio/speech.
Speak a sentence
import asyncio, base64, json, os
import websockets
async def main():
url = "wss://api.sprag.ai/v1/audio/speech?model=chorus"
headers = {"Authorization": f"Bearer {os.environ['SPRAG_API_KEY']}"}
async with websockets.connect(url, additional_headers=headers) as ws:
await ws.send(json.dumps({
"type": "session.update",
"session": {"audio": {"output": {"voice": "marin"}}},
}))
await ws.send(json.dumps({
"type": "conversation.item.create",
"item": {
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "The quarterly numbers are in."}],
},
}))
await ws.send(json.dumps({"type": "response.create"}))
pcm = bytearray()
async for message in ws:
event = json.loads(message)
if event["type"] == "response.output_audio.delta":
pcm += base64.b64decode(event["delta"])
elif event["type"] == "response.done":
break
print(f"{len(pcm)} bytes of 24 kHz PCM16")
asyncio.run(main())The first event is session.created, which states the output format and the
voice the session opens in:
{
"type": "session.created",
"session": {
"object": "realtime.session",
"type": "realtime",
"model": "chorus",
"output_modalities": ["audio"],
"audio": {"output": {"format": {"type": "audio/pcm", "rate": 24000}, "voice": "dominic"}},
"instructions": "",
"expires_at": 1791227832
}
}How it works
Text you send is held until you call response.create. Each
response.create turns the text sent since the previous one into a single
utterance and streams its audio. Responses are spoken one at a time; a
response.create sent while one is speaking waits for it to finish.
The voice, speed, instructions, output format, and timestamp granularities are fixed once the first response is created. To speak in another voice or format, open a new session.
Connect
wss://api.sprag.ai/v1/audio/speech?model=chorusmodel takes a text-to-speech model id from Models.
Authenticate the upgrade with your API key in the bearer header:
Authorization: Bearer $SPRAG_API_KEYA client that cannot set headers on a WebSocket upgrade, such as the browser
WebSocket API, passes the key as a subprotocol pair instead: sprag-api-key
followed by the key.
new WebSocket("wss://api.sprag.ai/v1/audio/speech?model=chorus", [
"sprag-api-key",
spragApiKey,
]);Configure the session
Send session.update before the first response.create. The server answers
each accepted update with session.updated.
{
"type": "session.update",
"session": {
"audio": {"output": {"voice": "marin"}}
}
}| Field | Required | Type | Notes |
|---|---|---|---|
session.audio.output.voice | No | string | A preset id, or the id of a voice your organization created. Defaults to the voice in session.created. |
session.instructions | No | string | Style guidance on how the voice reads. Accepted for voices whose model supports it. |
session.audio.output.format.type | No | string | audio/pcm, audio/wav, or audio/mpeg. Defaults to audio/pcm. |
session.audio.output.format.rate | No | integer | Sample rate in Hz: 8000, 16000, 22050, 24000, 44100, or 48000. Defaults to 24000. |
session.audio.output.timestamp_granularities | No | array | word requests word timestamps on each response. See Request word timestamps. |
A field you leave out keeps its current value, and session.updated states
the format in force. The deltas of one response, joined in order, form one
stream. An audio/wav stream carries its header in the first delta. Each
response starts a new stream.
Voices lists the presets and how to create your own.
Send text
Send text as a conversation.item.create carrying an input_text part, then
response.create to speak it:
{
"type": "conversation.item.create",
"item": {
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "The quarterly numbers are in."}]
}
}{"type": "response.create"}The server confirms each item with conversation.item.added and
conversation.item.done. To speak a sentence as soon as it is ready, send one
item and one response.create per sentence.
Read the audio
| Event | Meaning |
|---|---|
response.created | A response started. |
response.output_audio_transcript.delta | The text the response speaks. |
response.output_audio.delta | A chunk of audio, base64-encoded in the session's output format. Mono. |
response.output_audio_timestamps.delta | The response's word timestamps, when the session requested them. See Request word timestamps. |
response.output_audio_timestamps.done | The response's timestamps are complete. |
response.output_audio.done | The response's audio is complete. |
response.done | The response finished. response.status is completed, cancelled, or failed, and response.usage reports the audio produced. |
Request word timestamps
Word timestamps place each word of a response in its audio. To receive them,
declare the granularity before the first response.create:
{
"type": "session.update",
"session": {
"audio": {"output": {"timestamp_granularities": ["word"]}}
}
}After a response's last response.output_audio.delta, the server sends one
response.output_audio_timestamps.delta carrying the response's words, then
response.output_audio_timestamps.done, then response.output_audio.done:
{
"type": "response.output_audio_timestamps.delta",
"response_id": "resp_...",
"item_id": "item_...",
"output_index": 0,
"content_index": 0,
"granularity": "word",
"words": [
{"word": "The", "start": 0.0, "end": 0.08},
{"word": "quarterly", "start": 0.08, "end": 0.56},
{"word": "numbers", "start": 0.56, "end": 1.04},
{"word": "are", "start": 1.04, "end": 1.2},
{"word": "in.", "start": 1.2, "end": 1.68}
]
}| Field | Type | Notes |
|---|---|---|
granularity | string | word. |
words | array | One entry per word, in spoken order. An empty array when the response holds no speech, and null when the words could not be aligned. |
words[].word | string | The word as you sent it, punctuation included. For text written without spaces, the token the alignment segmented. |
words[].start | number | Seconds from the start of the response's audio to the word's onset. |
words[].end | number | Seconds from the start of the response's audio to the word's end. |
Times count from the first sample of the response's audio, whatever the output
format and rate. A cancelled response carries no timestamp events. If the
session's model does not return word timestamps, the update returns the error
unsupported_parameter; Models lists which do. If the
array holds a value other than word, the update returns the error
invalid_value.
Cancel a response
{"type": "response.cancel"}The active response ends with status cancelled, and responses waiting
behind it are cancelled with it. A cancel with no response active returns the
error response_cancel_not_active.
Constraints
- An utterance holds at most 8,192 characters.
- A session accepts at most 8,192 characters of text per minute.
- A session runs for at most one hour.
session.createdcarries the end asexpires_at. - A session counts toward your organization's cap of concurrent sessions.
- The session takes text only. Audio input events and binary frames return an
error.
Errors
The upgrade fails with an HTTP status and a JSON error body before the socket opens:
| Status | Meaning |
|---|---|
400 | model is missing, or the model does not serve speech. |
401 | The API key is missing or invalid. |
404 | The model does not exist. |
429 | Your organization is at its cap of concurrent sessions. |
During a session, a refused event returns an error event and the session
continues. The code names the reason:
| Code | Meaning |
|---|---|
unknown_voice | The model has no voice with that id. |
unsupported_parameter | The voice's model does not accept the field, such as instructions on a cloned voice or timestamp_granularities on a model that does not return them. |
invalid_value | timestamp_granularities holds a value other than word. |
unsupported_audio_format | The output format's type or rate is not one the session accepts. None of the update applies. |
session_config_locked | The voice, speed, instructions, output format, or timestamp granularities changed after the first response was created. |
input_too_long | The utterance would exceed 8,192 characters. |
rate_limit_exceeded | The session sent more than 8,192 characters in the last minute. |
response_queue_full | Too many responses are waiting behind the one speaking. |
model_not_running, model_at_capacity | No replica could take the session when its first response was created. The socket then closes with code 1013; retry on a new session. |
Next steps
To create a voice of your own, see Voice cloning and Voice design. To transcribe live audio over a WebSocket, see Speech to text in realtime.