On this page

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)
InputUint8Array, ArrayBuffer, Blob or ReadableStreamAsyncIterable<Uint8Array> or ReadableStream
OutputOne resultAn async iterable of transcripts
Use it forRecordings, voice messages, uploaded filesLive 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

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

FieldWhat it holds
oktrue when speech was recognized.
textThe full transcript.
wordsEach word as { text, startMs, endMs, confidence }.
languageThe language of the transcript.
durationMsThe length of the audio, in milliseconds.
reasonWhy 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.

reasonWhat happenedWhat to do
no_speechThe audio has no speech in it, such as silence or hold music.Ask the speaker to try again, or skip the file.
timeoutRecognition didn't finish in time.Retry, or split long audio into shorter parts.
unsupported_audioThe 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.

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

  • partial is the provider's current guess for speech still in progress. A later partial or final replaces it. Use partials for live captions.
  • final is 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.

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

TypeScript
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

Esc

Loading…

↑↓ navigate openCtrl J preview