> ## Documentation Index
> Fetch the complete documentation index at: https://assemblyai.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Error handling

> Dictation API error responses, status codes, and retry guidance.

Errors use one of two JSON body shapes. Auth failures, rate limiting, and
errors from the transcription step return `status`, `title`, and `detail`
fields:

```json theme={null}
{ "status": 401, "title": "Unauthorized", "detail": "Invalid API key" }
```

Errors Dictation raises itself, such as a malformed request, audio over the
limit, capacity, and upstream failures, return an `error` message and a
machine-readable `error_code`:

```json theme={null}
{ "error": "`config` is not valid JSON: Expecting property name enclosed in double quotes", "error_code": "bad_request" }
```

Read both when surfacing an error. In practice, checking `detail` first and
falling back to `error` covers every case.

## Status codes

| HTTP | Body shape | Cause |
| - | - | - |
| 400 | `error` / `error_code` | A malformed request body (a missing or empty `audio` part, a missing `config` part or one sent after `audio`, or `config` that is not valid JSON), or a Dictation-specific field such as `stt_prompt` failing validation |
| 400 | `status` / `title` / `detail` | A transcription `config` field that fails validation: an unknown field, a value out of range, or a field over its length limit |
| 401 | `status` / `title` / `detail` | Missing `Authorization` header, or an invalid API key |
| 413 | `error` / `error_code` | `audio_too_large` on `/v1/transcribe/live`: audio passed 120 seconds or the size cap, returned mid-upload |
| 413 | `status` / `title` / `detail` | `Audio Too Large` on `/v1/transcribe`: audio longer than 20 minutes |
| 415 | `status` / `title` / `detail` | The `audio` part's format is not accepted: anything but `audio/wav` or `audio/pcm` on `/v1/transcribe/live`, or an unsupported format on `/v1/transcribe` |
| 429 | `status` / `title` / `detail` | [Rate limit](https://assemblyai.com/docs/dictation/rate-limits.md) of 200 requests per minute exceeded |
| 502 | `error` / `error_code` | `upstream_error`: transcription upstream unavailable |
| 503 | `error` / `error_code` | `capacity_exceeded`: server at capacity |
| 504 | `error` / `error_code` | `upstream_timeout`: transcription upstream timed out, the upload went silent for too long, or the request ran past its overall limit |

> [!NOTE]
>
> Both auth failures return `401`. A missing header gives `detail: "Missing
>     Authorization header"`, and a bad key gives `detail: "Invalid API key"`.

## A failed rewrite is not a failed request

The transcript rewrite is best-effort and is never allowed to fail the call. If
the rewrite fails, the response is still `200`, `text` still holds the verbatim
transcript, `llm_response` is `null`, and `llm_error` says what went wrong:

| `llm_error` | Meaning |
| - | - |
| `timeout` | The rewrite ran past its deadline |
| `error` | The model returned nothing usable, or the rewrite failed unexpectedly |
| `truncated` | The output hit `max_tokens` before finishing |
| `prompt_not_found` | Your account has no saved prompt with the `aai_prompt` name |
| `context_exceeded` | The input is too long for the model's context window |
| `unsupported_param` | The model doesn't support a parameter that was sent |
| `rate_limited` | LLM Gateway returned `429` |
| `invalid_request` | LLM Gateway rejected the request with any other `4xx` |
| `upstream_error` | LLM Gateway returned a `5xx` or another non-`2xx` error |

Fall back to `text` when `llm_response` is `null`. Never treat a non-`null`
`llm_error` as a failed request. In both SDKs, `result.final_text` already
does this: it returns the rewrite when there is one and the transcript
otherwise.

## Errors in the SDKs

A failed request raises `DictationError` in Python and throws `DictationError`
in JavaScript. It carries the pieces you need to decide what to do next:

| Python | JavaScript | Meaning |
| - | - | - |
| `status_code` | `status` | The HTTP status from the table above |
| `error_code` | `errorCode` | The machine-readable code, when the response carried one |
| `retry_after` | `retryAfter` | Seconds to wait, from the `Retry-After` header on a `429` |

> [tab] Python SDK
>
> ```python theme={null}
> import time
>
> import assemblyai as aai
>
> aai.settings.api_key = "<YOUR_API_KEY>"
>
> try:
>     result = aai.DictationTranscriber().transcribe_live("clip.wav")
> except aai.DictationError as error:
>     if error.retry_after:
>         time.sleep(error.retry_after)
>     raise RuntimeError(
>         f"Dictation failed ({error.status_code}/{error.error_code}): {error}"
>     )
> ```

> [tab] JavaScript SDK
>
> ```javascript theme={null}
> import { setTimeout as sleep } from "node:timers/promises";
> import { AssemblyAI, DictationError } from "assemblyai";
>
> const client = new AssemblyAI({ apiKey: "<YOUR_API_KEY>" });
>
> try {
>   const result = await client.dictation.transcribeLive("clip.wav");
> } catch (error) {
>   if (!(error instanceof DictationError)) throw error;
>   if (error.retryAfter) await sleep(error.retryAfter * 1000);
>   throw new Error(
>     `Dictation failed (${error.status}/${error.errorCode}): ${error.message}`,
>   );
> }
> ```

`retry_after` is `None` in Python, and `retryAfter` is `undefined` in
JavaScript, when the response carried no `Retry-After` header, so
fall back to your own backoff rather than assuming a value is present.

## Retry guidance

* **429, 502, 503, and 504** are transient. Back off and retry.
* **400, 413, and 415** mean the request itself is wrong. Fix the audio or the
  config before retrying. See [Audio requirements](https://assemblyai.com/docs/dictation/audio-requirements.md)
  for the constraints. The exception is `413 audio_too_large` from
  `/v1/transcribe/live`: send the same recording to `/v1/transcribe` instead.
  See [Dictations up to 20 minutes](https://assemblyai.com/docs/dictation/recordings-over-two-minutes.md).
* **401** is a credential problem. Retrying will not help.

A chunked upload cannot be replayed, so keep the audio in memory if you want to
retry a failed request. Set the HTTP client timeout to 90 seconds; typical short
clips respond in under a second.

## Need help?

If you get stuck, contact our support team at
[support@assemblyai.com](mailto:support@assemblyai.com) or create a
[support ticket](https://www.assemblyai.com/contact/support). Include the
`session_id` from the response, or the failing request's timestamp and endpoint
if no response was returned, to help us look up your request.
