Skip to main content
Realtime upgrades GET /v1/realtime?model=... and uses session / conversation / response events. It differs from Responses WebSocket; changing only the URL is insufficient. This server-side example targets the GA protocol without legacy Beta headers or event names.

Run text or audio output

Install dependencies and set the address, token, and a Realtime-capable model using connection and runtime. By default the script sends a fixed text greeting. Text mode prints text; audio mode saves audio. Choose a supported voice using API_VOICE.
Each execution creates a separate session. An existing output file causes an immediate failure instead of overwriting; choose a new filename for another run. Output is a 24 kHz, mono, 16-bit little-endian PCM WAV file for a WAV-capable player. The example does not play sound directly.

Send recorded speech

Set API_AUDIO_PCM_PATH to upload a file instead of the fixed text. It must contain headerless PCM16LE, 24 kHz, mono, not WAV, MP3, AAC, or Base64 text. Export that format using your recording/conversion tool. The example accepts 0.1–30 seconds and checks length, even byte count, and common container headers. Raw PCM alone cannot prove its real sample rate or speech quality.
Upload uses small Base64 chunks. The example disables VAD, appends and commits audio, waits for committed, then creates a response. It does not combine automatic VAD generation with duplicate manual creation. Text mode can produce a text answer to spoken input, but does not guarantee a verbatim transcript.

Complete script

examples/realtime.mjs imports websocket-client.mjs from the same directory.

Session and audio validation

The script waits for session.created, sends session.update, and checks the effective modality, PCM format, sample rate, voice, and VAD settings in session.updated. It uses GA response.output_text.* / response.output_audio.* events; legacy response.text.* / response.audio.* events are incompatible. See the Realtime conversation guide for configuration. The receiver permits one assistant output item with one content part and checks response_id / item_id / output_index / content_index. Text must match deltas, text.done, and final output. Audio bytes come from deltas, not the final response.done object. Publication requires a content done event, response.done with completed status, and valid nonempty output. See Realtime server events. Invalid Base64, odd total PCM length, missing done, failed / cancelled / incomplete states, refusals, tools, disconnects, and timeout fail the example. The 10 MiB output cap is an example bound. Individual audio chunks may split samples, but the joined length must be even. Fixtures check bytes and WAV headers, not speech intelligibility or real model availability.

Multi-turn sessions, tools, and interruption

Production sessions can add conversation items and create subsequent responses, but applications must manage playback, event association, and context. This script generates one turn per execution. It does not capture microphones, implement automatic VAD conversations, execute tools, perform echo cancellation, or provide browser WebRTC. Interruption requires distinguishing server generation cancellation from stopping local playback. Generated but unplayed audio may require conversation.item.truncate to synchronize heard history; upstream rules determine the fields. Do not transplant WebRTC audio-buffer clearing into WebSocket playback. See Realtime client events. Ordinary gateway forwarding does not automatically convert every legacy protocol into GA. WebRTC, SIP, temporary client tokens, and Live API need separate verification; this site does not promise those capabilities. Realtime API reference · Production integration 阅读中文