# Failures and retries

Source: https://docs.sociaro.com/jobs/failures/

Handle terminal job failures, submit errors and ambiguous responses. See [Jobs API](https://docs.sociaro.com/jobs/index.md) for the job envelope.

## Failed jobs

A failed job includes `error` and omits `outputs`. Example failure after content moderation:

```json
{"id": "job_4be56281b681608b24640e8b82f56cf1",
 "status": "failed",
 "error": {"code": "provider_failed",
           "type": "content_policy_violation",
           "message": "The request failed because the output video may be related to copyright restrictions.",
           "provider_message": "The request failed because the output video may be related to copyright restrictions.",
           "provider_code": "OutputVideoSensitiveContentDetected.PolicyViolation"}}
```

### Reading the error

**Switch on `error.type`, log `error.code`, read `error.message`.** `type` is one of the ten words on the
[Errors](https://docs.sociaro.com/errors/#v2-errors) page, and a failed job's `code` maps to it like this:

| `error.code` | `error.type` |
| --- | --- |
| `provider_rejected` | `invalid_input` |
| `provider_failed`, `timeout`, `expired`, `output_too_large`, `unsupported_media` | `upstream_error` |
| `provider_rejected` or `provider_failed`, when `provider_code` is the generator's own content-moderation code, or `ContentFilter` on a `pruna/` model | `content_policy_violation` |
| `quota_exceeded` | `forbidden` |
| `insufficient_budget` | `insufficient_balance` |
| `internal` | `internal_error` |
| `canceled` | none: a cancel you asked for is not an error, so a canceled job's `error` has `code` and `message` only |

Use `type` for error handling, `code` for diagnostics and `message` for display. A provider moderation code can classify a refusal as `content_policy_violation`. On a failed job, `provider_message` and `provider_code` are optional. If supplied, the provider's message is sanitised and truncated to 400 characters. It can include text from the original request.

A failed job whose `message` is `Upstream error. Try again later or contact your account manager.` can be retried later; if it keeps coming back, contact your account manager, or hello@sociaro.com if your organisation has none.

Read the same error on the job, on an idempotent replay or by looking up the idempotency key. An HTTP error returned at submit has a [different envelope](https://docs.sociaro.com/jobs/failures/#errors-at-submit).

### A busy generator

If the generator still returns `429` on the final submission attempt, the job ends with `provider_failed`. A later retry requires a new `Idempotency-Key`. Other exhausted retry paths may end with `internal`. Check the existing job before starting a replacement.

### What a failed job costs

A failed job normally incurs no charge. One model-specific exception applies to Ideogram images withheld by the generator's safety check: the model bills for the images produced and the job records that amount in `cost_usd`.

### The codes

| Code | Meaning |
| --- | --- |
| `provider_rejected` | The generator rejected the input before rendering. |
| `provider_failed` | Generation failed or the provider remained at capacity after retries. Some model integrations also report an upstream refusal this way. |
| `timeout`, `expired` | Processing timed out or the job exceeded its runtime. |
| `canceled` | The caller canceled the job. |
| `quota_exceeded` | Account storage is full. |
| `insufficient_budget` | The balance can no longer cover the most the job can cost. Top up and submit again. |
| `output_too_large`, `unsupported_media` | The output exceeds the size limit or cannot be stored in the returned format. |
| `internal` | An internal error prevented completion. |
| `byo_storage_error` | Your own bucket refused the result and the provider no longer keeps it, or the deadline passed first. The job stays charged. See [Storage](https://docs.sociaro.com/storage/#charging-with-your-own-bucket). |

### Your bucket refused a result

With your own bucket, a job whose bucket refuses the file or does not answer stays `running` and reports the reason. We deliver the result again for free while the provider keeps it: when you poll the job, and on the first poll after you save corrected storage settings (saving alone does not deliver).

```json
{"status": "running",
 "error": {"type": "invalid_input", "code": "byo_storage_error",
           "message": "Your storage refused the result. Check the storage settings in the console; we will deliver it again while the provider keeps the result (until 2026-11-30 12:00 UTC).",
           "storage_reason": "AccessDenied"}}
```

`storage_reason` is the storage's own error code, `HTTP <status>`, `no answer` or `timeout`. If the provider no longer keeps the result, the job ends `failed` with `byo_storage_error` and stays charged. The job was charged when the render finished. Details on the [Storage](https://docs.sociaro.com/storage/#charging-with-your-own-bucket) page.

## Errors at submit

Every error answer of a `/v2` route has the same `error` object, `{"type", "code", "message", "provider_message"}`,
and the errors of `POST /v2/jobs` also carry `job_created` at the top level; the [Errors](https://docs.sociaro.com/errors/#v2-errors) page has the full
list of types and codes.

```json
{"error": {"type": "invalid_input", "code": "invalid_input",
           "message": "input.duration must be an integer from 4 to 15", "provider_message": null},
 "job_created": false}
```

- `error.type` identifies the category. `error.code` provides the specific condition. Submit errors have `provider_message: null`. [Errors](https://docs.sociaro.com/errors/#v2-errors) lists the types and codes.
- For `429`, top up for `insufficient_balance`. For `rate_limited` or `concurrency_limited`, wait for `Retry-After`.
- `job_created: false` applies to this attempt only. A `409 idempotency_conflict` means the key already belongs to a request with a different body.
- `job_created: null`, a missing response or a non-JSON error can mean that the job was stored. Replay with the same key and body, or use the lookup below.
- A successful submit (`202`, or `200` on replay) returns the job object and omits `job_created`.

## Look up a job by its key

`GET /v2/jobs?idempotency_key=<key>`, with the same API key that submitted. Use it after a submit whose answer you
did not receive. It never returns a list. It never creates a job and never starts a generation or a charge. Like reading the job by id, it reports the job's current state, and if the job has already finished at the generator it may record that result now: its final status, its outputs and the one charge that job gets anyway.

cURL

```bash
curl "https://api.sociaro.com/v2/jobs?idempotency_key=my-request-0001" \
  -H "Authorization: Bearer $SOCIARO_API_KEY"
```

Python

```python
# Requires Python 3 and requests (pip install requests).
import os
import requests

response = requests.get(
    'https://api.sociaro.com/v2/jobs?idempotency_key=my-request-0001',
    headers={
        'Authorization': 'Bearer ' + os.environ['SOCIARO_API_KEY'],
    },
    timeout=120,
)
response.raise_for_status()
print(response.text)
```

JavaScript

```javascript
// Run as an .mjs file with Node.js 22 or later.

const response = await fetch("https://api.sociaro.com/v2/jobs?idempotency_key=my-request-0001", {
  headers: {
    "Authorization": "Bearer " + process.env.SOCIARO_API_KEY,
  },
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
console.log(await response.text());
```

- `200`: the same object `GET /v2/jobs/{id}` returns, with `retry_after` while the job is unfinished.
- `404` `job_not_found`: no job under this key. A key that belongs to another organisation answers exactly like a key
  that does not exist.
- `400`: the parameter is missing (`invalid_input`) or is not a valid key (`invalid_idempotency_key`: 1 to 255
  characters of `A-Z a-z 0-9 . _ : -`). `401` and `503` as elsewhere.
- `503` with `Retry-After` and the message "a request with this Idempotency-Key is still being processed": a submit
  with this key is being stored right now. It is not an answer about the job: ask again after `Retry-After`. The
  lookup never answers `404` while that is going on.

### After a lost answer

After a `502`, `503` or `504` on submit, or `job_created: null`, send the same request again with the same
`Idempotency-Key`. It is safe: it returns the existing job or creates the one job, never two. The lookup is for finding
a job without sending the request again: a `200` means the job exists. A `404` from a lookup you sent at least 180 seconds after
your last attempt with that key ended (an answer arrived, the connection closed, or you stopped waiting and closed it) is final: no job
exists, none of those attempts will create one, and you can release the user's credits. Count from the end of the
attempt, not from its start, and from the attempt that ended last if you sent the same key more than once. What counts
is when you send the lookup, not when its answer arrives: a `404` from a lookup sent earlier means only that no job
existed at that moment, however late you receive it. An attempt may still be in progress, so it is not proof. Send
the lookup again once that time has passed. This holds for requests sent to the public API host, on the condition that a request reaches our server within 90 seconds after your attempt with that key ended; sending the same request again with the same Idempotency-Key is always safe and settles the outcome without relying on timing (it returns the existing job or creates exactly one).

### Releasing the user's credits

`job_created: false` means this attempt created nothing. Release the user's credits on it when no earlier attempt with the same `Idempotency-Key` ended ambiguously (`502`, `503`, `504`, `job_created: null`, or no answer at all). After an ambiguous attempt, keep the credits held and either send the same request again with the same key until the answer is the job (`200` or `202`), or look the key up: the job, or a `404` that the [lookup rule](https://docs.sociaro.com/jobs/failures/#after-a-lost-answer) calls final, settles it. Until then the outcome of the earlier attempt is not known, because it may still create the job: an earlier `404` from the lookup is not proof, and a refusal of a later attempt (a `401`, `403`, `429` or `503`, for example) or its `job_created: false` settles only that attempt. A job that does appear is charged once.

### How long keys are remembered

Idempotency keys are retained for at least 90 days after first submission. After the job record's `retention_until`, lookup can return the terminal status without `outputs`.
