On this page

Errors

What VoiceError carries, what each error code means, and which outcomes come back as results instead of exceptions.

The SDK throws only when the provider or the session fails. A recognition that finds no speech is not a failure: it resolves with ok: false and a reason. That keeps expected outcomes out of your catch blocks.

VoiceError

Errors from the provider or the session are VoiceError instances. VoiceError extends Error. These are the fields to read:

FieldWhat it holds
codeOne of the codes below. Branch on this, not on message.
providerThe provider that failed, such as "deepgram". In a pipeline, it tells you which side failed.
causeThe underlying error from the provider, its SDK or the network, when there is one.
messageA description for people. It can change between versions, so don't match on it.

Error codes

CodeWhen it happensRetry?
unsupportedYou asked for something the provider can't do: an option at createSession, or a method such as pause().No. Remove the option or switch providers.
authThe provider rejected the credentials, or the adapter found none.No. Fix the key.
rate_limitedThe provider refused the request because of a rate or concurrency limit.Yes, after a backoff.
timeoutThe provider did not respond in time, for example while connecting.Yes.
networkThe connection to the provider failed or dropped.Yes. Open a new session if the old one closed.
provider_errorThe provider returned an error the SDK has no specific code for. Details are in cause.Sometimes. Check cause first.
closedYou called a method on a session that is already closed.No. Open a new session.
invalid_audioThe audio is empty or can't be decoded.No. Fix the audio.

Results, not exceptions

session.transcribe resolves with ok: false when the problem is the audio, not the session. Check ok before you read text.

reasonMeaningWhat to do
no_speechThe audio had no speech in it, such as silence or background noise.Treat it as an empty turn, or ask the caller to repeat.
timeoutThe recognition did not finish in time. The session is still open.Try again, or send shorter audio.
unsupported_audioThe audio decodes, but the provider does not accept its format.Convert it, for example to the session's encoding and sampleRate.

A timeout reason and a thrown timeout are different. The reason means this one recognition ran out of time. The thrown error means the provider stopped responding, and the session may not be usable.

Handle errors

Use instanceof to tell a VoiceError from a bug in your own code, then branch on code:

TypeScript
import { createSession, VoiceError } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";

try {
  await using session = await createSession(deepgram());
  const result = await session.transcribe(await Bun.file("voicemail.wav").bytes());
  console.log(result.ok ? result.text : `no transcript: ${result.reason}`);
} catch (err) {
  if (!(err instanceof VoiceError)) throw err;

  switch (err.code) {
    case "auth":
      console.error(`Check the API key for ${err.provider}.`);
      break;
    case "unsupported":
      console.error("This provider can't run with these options:", err.message);
      break;
    default:
      console.error(err.code, err.provider, err.cause);
  }
}

createSession opens the connection, so auth and unsupported usually surface there, before any audio is sent.

Retry what can succeed

Retry rate_limited, timeout and network with a growing delay between attempts. Retrying the other codes repeats the same failure. A provider_error can be worth one retry once you have checked cause.

TypeScript
import { VoiceError } from "voiceai-sdk";

const retryable = new Set(["rate_limited", "timeout", "network"]);

export async function withRetry<T>(fn: () => Promise<T>, attempts = 3): Promise<T> {
  for (let i = 1; ; i++) {
    try {
      return await fn();
    } catch (err) {
      const retry = err instanceof VoiceError && retryable.has(err.code) && i < attempts;
      if (!retry) throw err;
      await new Promise((resolve) => setTimeout(resolve, 500 * 2 ** i));
    }
  }
}

Wrap a call with it: await withRetry(() => session.transcribe(audio)).

Errors outside a call

Some failures aren't tied to a call you are awaiting, such as a connection that drops while a transport is connected. The session emits those as an error event. Listen for close too, so you know when the session has ended.

TypeScript
session.on("error", (err) => console.error(err.code, err.provider));
session.on("close", () => console.log("session closed"));

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview