Skip to main content
This sample app puts your own text-to-speech into a voice agent you can talk to in the browser. Dialog-RSN-1 hears the caller, decides when they’ve finished, and writes the reply as text, and your Python server streams that text into Cartesia. It takes about ten minutes to run and about 450 lines of Python to read. The API’s own voice is also Cartesia, so in production you’d call Cartesia yourself only to control its settings or the playback. The app does it to show the whole pattern in working code, and any streaming text-to-speech that returns word timestamps works the same way.

Full code on GitHub

A FastAPI server, a browser page with a live transcript and event log, and a terminal probe that streams a sample caller through it.

What you’ll build

  1. A Python server that holds one Dialog-RSN-1 connection per browser tab and forwards the caller’s audio to it.
  2. Streaming speech: Cartesia speaks each reply from its first few words, while Dialog-RSN-1 is still writing it.
  3. Barge-in: talk over the agent and it stops as soon as Dialog-RSN-1 confirms it’s you, and the model learns which words you heard.
  4. A browser page with a microphone picker, voice and turn-taking settings, a live transcript with per-turn latency, and a raw event log.

Prerequisites

  1. Python 3.12 and uv.
  2. A Dialog-RSN-1 API key. Sign up or log in at studio.poly.ai and create one in the API Keys tab. The quickstart walks through it.
  3. A Cartesia API key. The free tier works.
  4. Chrome, Edge or Safari, and headphones if you’ll use the microphone.

Run it

Open http://127.0.0.1:3000. Click Start conversation and talk. Or click Play the sample caller: a recorded caller asks about a trip to Lisbon, then talks over the answer, so you see a barge-in without a microphone. To check the whole pipeline from a terminal, leave the server running and stream the same sample through it. The probe counts from audio_end_ms on speech_stopped, where Dialog-RSN-1 marks the end of speech in the caller’s audio. That mark falls a little after the caller’s last word:

Step 1: Open one socket and configure the session

Each browser tab gets its own Call, which opens a Dialog-RSN-1 WebSocket with the key in a header. The browser never sees the key.
config.DIALOG_REALTIME_URL defaults to wss://api.studio.poly.ai/v1/realtime. Set DIALOG_REALTIME_URL in .env to point somewhere else. Then it sends one session.update:
Three fields do the work:
  • output_modalities: ["text"]. This agent speaks through its own Cartesia stream, so it asks Dialog-RSN-1 for text. Sessions start with ["audio"], where the server speaks the reply itself, so set this or the caller hears two voices.
  • rate: 24000. The page records at 24 kHz, and Cartesia returns 24 kHz, so the page never resamples.
  • create_response: true. When Dialog-RSN-1 confirms the caller has finished, it starts the reply itself. You don’t send response.create.
The page waits for session.updated before it sends any audio, so Dialog-RSN-1 reads every frame at the right rate.

Step 2: Forward the caller’s audio

The page sends the microphone as binary PCM16, 100 ms per frame. The server wraps each frame in input_audio_buffer.append and passes it on:
The next event your server acts on is the reply.

Step 3: Stream the reply into speech

Dialog-RSN-1 sends the reply as response.output_text.delta events. Each delta goes straight to Cartesia:
cartesia_tts.py keeps one Cartesia WebSocket open for the whole call. Each response gets its own Cartesia context, named after the response_id. Text goes in with continue: true, so Cartesia keeps the rhythm of one sentence across many small pieces. Two details matter:
  • Send whole words. A delta can end mid-word. push() holds the text after the last space until the word is complete.
  • Lower max_buffer_delay_ms. Cartesia waits up to this long for more text before it speaks. Its default, 3 seconds, favors prosody. The demo sets 250 ms, so Cartesia starts speaking soon after the first words arrive.
Audio comes back as base64 PCM chunks, and the server sends each one to the page as a binary frame. The page’s player worklet plays them as they arrive.

Step 4: Stop talking when the caller does

When the caller starts speaking, Dialog-RSN-1 sends input_audio_buffer.speech_started. It has already checked that the sound is the caller and not noise, so the server acts on it at once:
Stopping takes two steps, because audio lives in two places:
Canceling the Cartesia context stops new audio. Flushing the page’s queue stops audio that’s already been synthesized but not yet played. Skip the second step and the agent keeps talking for seconds after you’ve cut in.

Step 5: Tell Dialog-RSN-1 what the caller heard

Only the page knows what reached the speakers. Its player worklet counts the samples it has played and reports every 250 ms. The server turns those counts into Dialog-RSN-1 playback reports:
spoken_text comes from Cartesia’s word timestamps: every word whose audio had started by played_ms. Dialog-RSN-1 cuts the reply in its history back to spoken_text, so the model knows what the caller missed. In the sample, the agent had written its whole reply, but the caller heard only up to “The Alfama neighborhood”. The page strikes through the words the caller never heard. The reports also keep the barge-in check running for exactly as long as the page plays the reply. Playback reports explains both, and what happens without them.

Tune turn-taking with instructions

Dialog-RSN-1 already waits through pauses and slow speech, and replies quickly once the caller has finished. To shift how patient it is, describe your callers in the instructions. The page’s Turn-taking menu adds one line to them:
Turn-taking has more instructions like these.

Troubleshooting

Where to go next

  • Using your own text-to-speech explains each part of this app for any provider, with an ElevenLabs example in a terminal.
  • Turn-taking covers turn-taking instructions in depth.
  • The API reference lists every event this app uses, including the poly.output_audio.* playback reports.
Last modified on November 24, 2026