Getting an API key
You create the key in Agent Studio at studio.poly.ai:- Go to studio.poly.ai and sign up, or log in if you already have an account.
- Open the API Keys tab in your workspace’s top bar, then select API key.
- Give the key a name. Leave Agents on All agents, and leave at least one Resource Access permission on. The form needs one, and Dialog-RSN-1 accepts the key whichever you pick.
- Select Create, then copy the key. It’s only shown once.
POLY_API_KEY, or in a secret manager rather than in source control.
Connecting
One WebSocket carries the whole conversation. You authenticate once when you connect, and every turn travels over the same socket as JSON text frames. To open it, connect and send your API key in theX-API-KEY header:
connect
Authorization: Bearer YOUR_API_KEY works too, so an existing realtime client, including the official openai SDK, keeps its auth and only needs a new URL and key.
The moment the connection is accepted, before you’ve sent anything, the server sends you a session.created event. You’re already in a session with the default settings: 24 kHz PCM16 audio in, server turn detection, and spoken replies.
Telling it how to behave
Sendsession.update with your instructions, tools, and anything else you want to change. The server replies with session.updated, echoing back the full configuration in effect.
you send: session.update
session.update at any point in the conversation.
The shape of a turn
Adding something to the conversation and asking for a response to it are separate events:you send: conversation.item.create
you send: response.create
response.create for the follow-up. For a single question, send one straight after the other.
Watching the answer arrive
The reply comes back as a stream of events. You need three of them to hold a conversation:response.output_audio_transcript.deltacarries the words of the reply. Append them to see what it says.response.output_audio.deltacarries the speech: base64 PCM16 audio, 24 kHz mono. Play it, or collect it.response.donetells you the turn is over and how it ended.
server sends: the response
dialog-rsn-1: “The capital of France is Paris.”The audio streams at speaking pace, so
response.done arrives once the reply has been spoken. The other events track which output item the server is writing and where its content part begins and ends, which a fuller integration uses.
For a longer conversation, repeat the same steps: add an item, ask for a response, and collect the deltas until response.done. Each new message builds on the conversation so far, so you have no separate history to maintain.
All of it, together
The same sequence as one script, running on the standardwebsockets package. It prints the reply as it streams and saves the speech to reply.wav. You need Python 3.10 or later, pip install 'websockets>=14', and POLY_API_KEY set in your shell.
The version floor matters: connect() took extra_headers before websockets 14 and additional_headers from 14 on, so the script below raises TypeError on an older install rather than failing to authenticate.
python
output
reply.wav in any audio player, or with afplay reply.wav on macOS.
Keep the error branch. Almost every failure on this API answers with an error event and leaves the connection open, so a turn that fails arrives as an event rather than a dropped socket. The few that close the socket, such as an oversized frame or an idle connection, are listed under close codes. Skip the branch and a failed generation prints nothing and exits zero. The same goes for session.update: a refused update answers with error instead of session.updated and applies nothing, which is why the script checks the reply.
Bring your own text-to-speech
The server’s voice is the quickest way to a spoken reply. When you want your own voice, your own TTS vendor, or control over playback, ask for text instead. Setoutput_modalities to ["text"]:
you send: session.update
response.output_text.delta, with no audio events, and you speak it however you like. In the script above, add "output_modalities": ["text"] to the session and read response.output_text.delta in place of response.output_audio_transcript.delta. The streaming text-to-speech guide takes it from there. Ask for one or the other: ["text", "audio"] is refused.
Session setup by use case
The script above sends text in. A voice agent streams the caller’s audio in withinput_audio_buffer.append, and Dialog-RSN-1 decides when the caller has finished, transcribes the turn and answers it in the same response. From there, set up the session for what you’re building:
Common mistakes
Reading text deltas from a spoken session. A new session replies withresponse.output_audio.delta and puts the words on response.output_audio_transcript.delta. It never sends response.output_text.delta, so a client that reads only text deltas sees an empty reply and no error. Read the transcript deltas and play the audio, or set output_modalities: ["text"].
Waiting for a tool result to answer on its own. Adding an item never starts a reply, whether it’s a user message or a tool result. Follow every function_call_output with a response.create:
you send: a tool result, then a new response
Next steps
Overview
What Dialog-RSN-1 does well, from turn-taking to promptable speech understanding.
API reference
Every event, the session object, PolyAI extensions and error codes.
Build a voice agent in Python
Grow this script into tool calling and a live microphone, with plain websockets.
Prompting basics
Write instructions that shape what Dialog-RSN-1 says and when it speaks.

