Speech-to-text
Turn audio into text with one call for a recording, or as a stream of partial and final transcripts while someone speaks.
A session gives you two ways to recognize speech. transcribe takes a finished piece of audio and resolves with one result. listen takes a live stream and yields transcripts while the speaker talks.
Two ways to recognize speech
transcribe(audio) | listen(stream) | |
|---|---|---|
| Input | Uint8Array, ArrayBuffer, Blob or ReadableStream | AsyncIterable<Uint8Array> or ReadableStream |
| Output | One result | An async iterable of transcripts |
| Use it for | Recordings, voice messages, uploaded files | Live calls and microphones |
transcribe accepts a ReadableStream too, but it resolves only once, with the whole transcript. Use listen when you need text before the audio ends.
Transcribe a recording
import { readFile } from "node:fs/promises";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession(deepgram());
const result = await session.transcribe(await readFile("voicemail.wav"));
if (result.ok) console.log(result.text);
else console.log("no transcript:", result.reason);The result has the same shape on every provider:
| Field | What it holds |
|---|---|
ok | true when speech was recognized. |
text | The full transcript. |
words | Each word as { text, startMs, endMs, confidence }. |
language | The language of the transcript. |
durationMs | The length of the audio, in milliseconds. |
reason | Why ok is false: no_speech, timeout or unsupported_audio. Only set when ok is false. |
When nothing is recognized
Audio without speech is not an error. transcribe resolves with ok: false and a reason, so you handle it next to the success case, without a try.
reason | What happened | What to do |
|---|---|---|
no_speech | The audio has no speech in it, such as silence or hold music. | Ask the speaker to try again, or skip the file. |
timeout | Recognition didn't finish in time. | Retry, or split long audio into shorter parts. |
unsupported_audio | The provider can't read this audio. | Convert it to a format the provider accepts. See Audio streaming. |
The SDK throws only when the provider or the session fails, for example on a bad key or a dropped connection. Those errors are VoiceError instances with a code; see Errors.
Stream with listen
listen takes raw audio as it arrives and yields transcripts. Tell the session the audio format with sampleRate and encoding.
import { createReadStream } from "node:fs";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession(deepgram(), { sampleRate: 16000, encoding: "pcm16" });
for await (const t of session.listen(createReadStream("call.pcm"))) {
console.log(t.type, t.startMs, t.text);
}Each transcript is { type, text, startMs, endMs, speaker? }, where type is one of two values:
partialis the provider's current guess for speech still in progress. A later partial or final replaces it. Use partials for live captions.finalis settled text for a finished stretch of speech. It won't change. Store finals, or pass them to a model.
converse calls your handler once per final, and ignores partials. Whether you get partials at all depends on the provider: check streamingStt in Capabilities.
Word timings and speakers
words gives each word's position in the audio, in milliseconds from the start, with a confidence score. Use it to highlight words during playback or to cut a clip. Speaker labels come on listen transcripts, in the optional speaker field.
import { createReadStream } from "node:fs";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession(deepgram(), { sampleRate: 16000, encoding: "pcm16" });
const { diarization } = session.capabilities;
for await (const t of session.listen(createReadStream("meeting.pcm"))) {
if (t.type === "final") console.log(diarization ? `${t.speaker}: ${t.text}` : t.text);
}Both depend on the provider. Check session.capabilities.wordTimings and session.capabilities.diarization before you rely on them.
Language
Set language on the session to tell the provider what to expect. result.language reports the language of the transcript.
import { readFile } from "node:fs/promises";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession(deepgram(), { language: "es" });
const result = await session.transcribe(await readFile("mensaje.wav"));
console.log(result.language, result.text);Without language, what the provider assumes depends on the provider. If it can't recognize the language you ask for, createSession throws VoiceError with code unsupported.
Next steps
- Audio streaming: get live audio into a session from a call or a browser.
- Text-to-speech: the other direction.
- Errors: what still throws, and each
VoiceErrorcode.