Skip to main content
This page walks through connecting to Dialog-RSN-1, configuring the session and running one turn, then puts it together as a short Python script that saves the spoken reply to a WAV file. Prerequisites: an API key and a WebSocket client in any language.

Getting an API key

You create the key in Agent Studio at studio.poly.ai:
  1. Go to studio.poly.ai and sign up, or log in if you already have an account.
  2. Open the API Keys tab in your workspace’s top bar, then select API key.
  3. 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.
  4. Select Create, then copy the key. It’s only shown once.
Store it in an environment variable, such as POLY_API_KEY, or in a secret manager rather than in source control.
Match the host to the Studio that issued the key. A key from studio.poly.ai only works on api.studio.poly.ai, which every sample in these docs uses. If your workspace lives on a regional Studio, such as studio.us.poly.ai, connect to that region’s host instead: wss://api.us.poly.ai/v1/realtime. A key on the wrong host gets a bare 401 with {"message":"Unauthorized"}, the same answer as a wrong key. In the samples below, change URL. Most guide repos read it from DIALOG_REALTIME_URL.

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 the X-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.
Keep your key server-side. Treat it like any API credential: call it from a server or trusted backend, not from code that ships to a browser or a device you don’t control. A browser’s native WebSocket can’t set headers at all, so connecting from a page means putting a relay in front, as the browser guide covers.
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

Send session.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
A new session speaks its replies, so you get each reply as audio plus a transcript of what it says, with no text-to-speech of your own. To speak replies with your own voice instead, see bring your own text-to-speech below. You can send another 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
Because they’re separate, you can seed a conversation with prior history without firing a reply at each item. Tool results work the same way: you add the result, then send 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.delta carries the words of the reply. Append them to see what it says.
  • response.output_audio.delta carries the speech: base64 PCM16 audio, 24 kHz mono. Play it, or collect it.
  • response.done tells 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 standard websockets 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
Play 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. Set output_modalities to ["text"]:
you send: session.update
The reply then streams as 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 with input_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 with response.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.
Last modified on November 24, 2026