On this page

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-sdk

Set OPENAI_API_KEY on your server, and keep it out of browser bundles.

Quickstart

TypeScript
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

FeatureSupportNotes
Speech-to-text (transcribe)YesTranscripts of what the caller said come from the input transcription model
Streaming transcripts (listen)Yes
Text-to-speech (speak)Yes
Streaming speechYes
Speech-to-speechYesThe model answers in audio without a text step
Word timingsNoTranscripts have no per-word times
Speaker labelsNo
InterruptionsYes
Voice list (listVoices)YesThe 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.

TypeScript
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.

TypeScript
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.

TypeScript
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.

TypeScript
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.

OptionTypeDefaultEnv var
apiKeystringOPENAI_API_KEY
modelstring"gpt-realtime"
instructionsstring
transcriptionModelstring"gpt-4o-transcribe"
baseUrlstringwss://api.openai.com/v1/realtimeOPENAI_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.

TypeScript
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

  • diarization fails with unsupported.
  • pause() and resume() fail with unsupported. Close the session and open a new one with the same key.
  • sampleRate must be 24000 for pcm16. Telephony audio (mulaw, 8000) is converted by the SDK.

How it works

  • One WebSocket per session. The SDK sends input_audio_buffer.append events and reads response.output_audio.delta events 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 converse handler, the SDK disables automatic responses and creates each response from your handler's text.

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview