What you’ll build
A voice agent you can talk to from your terminal, built on the standardwebsockets package and about a hundred lines of Python. No SDK, no framework.
It comes together in stages: a socket and a single text turn first, then tool calling, then the concurrency that audio needs, and last the microphone itself, where the server decides when you’ve stopped speaking. The first script runs on its own, and each stage after it adds to that code until the full example at the end puts it all in one file.
The API reference lists the whole protocol in one place.
Before you start
You need four things:- An API key. Sign up or log in at studio.poly.ai and create one in the API Keys tab. The quickstart walks through it. Keep it in
POLY_API_KEYin your shell rather than in source control. - Python 3.10 or later.
pip install 'websockets>=14' sounddevice numpy. The code below passes headers asadditional_headers, whichconnect()takes from websockets 14 on. Older versions raiseTypeError. Installnumpyyourself, becausesounddevicedoesn’t depend on it and the microphone callback needs it to convert samples. On Linux, also install the PortAudio library from your package manager (libportaudio2on Debian and Ubuntu). The macOS and Windows wheels include it.- A microphone, for the last section. The rest runs without one.
Connecting
One connection carries one conversation. You open a WebSocket and keep it open, and every turn after that travels over the same socket as JSON text frames, in both directions. Authentication happens once, when you connect.connect.py
session.created. The session starts on the server’s default settings, and the next section covers changing them.
Configuring the session
Because the session already exists, configuring it is a message rather than a set of connection parameters. Sendsession.update and the server answers with session.updated, echoing back the full configuration now in effect. Updates merge into what’s already there, so setting instructions later won’t wipe your tools.
you send: session.update
output_modalities is ["text"], because this agent prints the reply as text. A new session starts in ["audio"], where the server speaks the reply and sends the words as response.output_audio_transcript.delta instead of the response.output_text.delta events this guide reads. To hear the agent, leave the default and play response.output_audio.delta, as the quickstart does, or stream the text into your own text-to-speech.
audio.input.format is PCM16 at the rate you send. 16000 is Dialog-RSN-1’s native rate and the rate the microphone code below captures at. Always set rate. Without it, the server assumes the standard realtime rate, 24 kHz, and hears 16 kHz audio 1.5 times too fast. session.updated keeps echoing rate: 24000 either way, and reports the rate you declared as poly_input_rate.
turn_detection.create_response is the only field in that object the service reads. It ignores the others, such as silence_duration_ms and threshold. Dialog-RSN-1 already waits through pauses and slow speech, and to make it more or less patient you describe your callers in instructions.
The shape of a turn
With the session configured, you can take a turn. Turns share the same two-part shape: 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.output_text.delta and you get the text as it types out. response.done closes the turn, with the outcome in response.status.
Connection, session and turn together make a first script:
first_turn.py
error branch is worth keeping. Failures on this API arrive as an error event and leave the connection open, so a turn can fail without the socket closing. Without the branch, a failed generation prints an empty string and exits zero.
Adding tool calling
So far the agent only answers questions. Tools let it look things up and act on them. Declare them on the session, then dispatch with a dict rather than a chain ofif/elif, which stays flat as the list grows:
SESSION_TOOLS as session.tools in your session.update and you’re wired up. A call then arrives as response.function_call_arguments.done, carrying the name, the full arguments as a JSON string, and a server-minted call_id your reply has to echo back.
handle_tool_call runs the function, adds the result as a function_call_output item, then sends response.create for the follow-up. Don’t leave out that response.create, because a tool result doesn’t end a turn by itself and nothing else starts the follow-up. Use TOOLS.get(name) to look the function up. A model can call a name you didn’t define, and a KeyError would stop the whole event loop. Tools and knowledge covers what the model says while a tool runs, and the API reference has the full wire shape.
Structuring concurrent tasks
Handling one turn at a time is fine while you’re typing. With audio, the agent needs to push frames up, watch for a turn to start, and handle what comes back at the same time, so it’s easier to restructure around concurrent tasks before adding the microphone.asyncio.wait(..., return_when=asyncio.FIRST_COMPLETED) returns as soon as one task ends, and the finally cancels the others, so a Ctrl-C doesn’t leave tasks behind. Unlike asyncio.gather, asyncio.wait doesn’t re-raise exceptions. Calling .result() on the finished task raises any bug that ended it.
Here the error branch prints and carries on, where the one-shot script raised. The connection survives an error, so a long-running agent can keep going and the next turn can still succeed. The full example adds a stop_after_turn flag for its one-shot --say mode, and that path does raise. With no microphone sending more audio, it would otherwise wait forever for another turn.
Adding real audio input and server VAD
Streaming audio in means feeding frames into the queuesend_audio_task already drains, base64-encoded PCM16, appended continuously. Set blocksize yourself. Every callback becomes its own WebSocket message, and if you leave it unset the host picks the block size, which can be very small. The server accepts any block size, and 512 samples at 16 kHz (32 ms) is a good default:
sounddevice runs that callback on its own thread, so frames reach the event loop through loop.call_soon_threadsafe rather than by touching the queue from that thread.
SAMPLE_RATE = 16000 asks the device for Dialog-RSN-1’s native rate, which most microphones support. If yours doesn’t, sd.InputStream fails with PortAudioError: Invalid sample rate rather than choosing a nearby rate. In that case, capture at the device’s own rate. The full example sends SAMPLE_RATE as audio.input.format.rate, so the server knows the rate and resamples on the way in:
input_audio_buffer.speech_started arrives as soon as the caller starts to make a sound. While a reply is playing, it arrives once Dialog-RSN-1 has confirmed the caller is talking. input_audio_buffer.speech_stopped arrives when Dialog-RSN-1 judges the caller has finished, so a pause mid-sentence doesn’t end the turn. The server then commits the turn and, with create_response: true, answers it. If the sound turns out to be noise, Dialog-RSN-1 drops the turn, so a speech_started can arrive with no speech_stopped after it.
If the caller starts talking while a reply is still streaming, Dialog-RSN-1 cancels that reply once it confirms they’re talking. You get a response.done with status cancelled that you didn’t ask for, so the event handler needs to expect one. The API reference covers turn-taking and barge-in.
Full example
This file puts everything above together. It also adds a--say flag, which sends a single text turn instead of opening the mic, so you can try the tools without a microphone.
Full code on GitHub
This file with its requirements, ready to clone and run.
voice_agent.py
TOOLS and instructions, and the protocol code stays as it is.
Next steps
Overview
What Dialog-RSN-1 does well, with links to each feature.
API reference
Every event, the session object, function calling, and error codes.
Browser (JavaScript)
The same protocol, from a browser tab, with microphone capture in the client.
Your own text-to-speech
Speak the streamed text through your own TTS provider: barge-in, playback reports, and time to first audio.

