OpenAI Realtime
Run speech-to-speech sessions on the OpenAI Realtime API with openai(): one connection hears the caller and answers in a voice, with barge-in. No OpenAI package needed.
openai() opens a Realtime API connection per session. The model hears audio and answers with audio directly, so there is no separate speech-to-text and text-to-speech step unless you ask for one.
Install
npm install voiceai-sdkpnpm add voiceai-sdkyarn add voiceai-sdkbun add voiceai-sdkSet OPENAI_API_KEY on your server, and keep it out of browser bundles.
Quickstart
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
await using session = await createSession(openai());
const result = await session.transcribe(await Bun.file("question.wav").bytes());
console.log(result.text);
const reply = await session.speak("Thanks for calling. How can I help?");
console.log(reply.durationMs);This prints what was said in question.wav and the length of the spoken reply. await using closes the Realtime connection when the block ends.
What it supports
| Feature | Support | Notes |
|---|---|---|
Speech-to-text (transcribe) | Yes | Transcripts of what the caller said come from the input transcription model |
Streaming transcripts (listen) | Yes | |
Text-to-speech (speak) | Yes | |
| Streaming speech | Yes | |
| Speech-to-speech | Yes | The model answers in audio without a text step |
| Word timings | No | Transcripts have no per-word times |
| Speaker labels | No | |
| Interruptions | Yes | |
Voice list (listVoices) | Yes | The Realtime API's built-in voices |
pause() and resume() | No |
The capability matrix compares these with every other provider.
Stream transcripts
Iterate session.listen to get partial transcripts while someone speaks and a final one when they stop.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
await using session = await createSession(openai(), { language: "en", sampleRate: 16000 });
for await (const t of session.listen(microphone())) {
if (t.type === "final") console.log(`[${t.startMs}ms] ${t.text}`);
}microphone() stands for any AsyncIterable<Uint8Array> of 16-bit PCM, such as a transport or a file stream.
Speak
Iterate session.speak to send audio as it is produced. Pass an async iterable of text, such as a model's output, and speech starts before the text is finished.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
await using session = await createSession(openai());
for await (const chunk of session.speak("Let me check that for you.")) {
player.write(chunk.data);
}Voices
listVoices returns the voices you can pass as voice. The default is alloy.
import { listVoices } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
const voices = await listVoices(openai());
console.log(voices.filter((v) => v.language.startsWith("en")).map((v) => v.id));Hold a conversation
Without a handler, converse lets the model answer on its own using instructions. With a handler, your text is spoken instead.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
import { twilioMediaStream } from "voiceai-sdk/twilio";
await using session = await createSession(openai({ instructions: "You book table reservations." }), {
encoding: "mulaw",
sampleRate: 8000,
});
session.connect(twilioMediaStream(ws));
await session.converse();Configuration
Pass options to openai(). Options that have an environment variable fall back to it.
| Option | Type | Default | Env var |
|---|---|---|---|
apiKey | string | OPENAI_API_KEY | |
model | string | "gpt-realtime" | |
instructions | string | ||
transcriptionModel | string | "gpt-4o-transcribe" | |
baseUrl | string | wss://api.openai.com/v1/realtime | OPENAI_BASE_URL |
instructions is the system prompt the model answers with. When you pass a handler to converse, your handler's text is spoken instead and instructions is ignored.
Call the API directly
session.native is the Realtime WebSocket. Send any client event the API accepts.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
await using session = await createSession(openai());
session.native.send(JSON.stringify({ type: "session.update", session: { instructions: "Answer in French." } }));Limitations
diarizationfails withunsupported.pause()andresume()fail withunsupported. Close the session and open a new one with the same key.sampleRatemust be 24000 forpcm16. Telephony audio (mulaw, 8000) is converted by the SDK.
How it works
- One WebSocket per session. The SDK sends
input_audio_buffer.appendevents and readsresponse.output_audio.deltaevents back as speech chunks. - Barge-in uses the API's server-side voice activity detection. On speech start the SDK truncates the current response and emits
interrupt. - With a
conversehandler, the SDK disables automatic responses and creates each response from your handler's text.
Next steps
- Lifecycle and cleanup
- Give your agent a voice
- Compare providers: the same features on every other provider