# Errors and retries

Handle a failed request, retry it safely, and find out why a run stopped.

## Failed requests

When a request fails, the client raises an error that carries the HTTP status and the response body. The body names the field or ID that caused the problem, so read it before you try again.

A `422` body is a `detail` list with one entry for each problem: `loc` says where the field is, `msg` says what is wrong with it, and `type` names the kind of error.

| Status | Python error | What to do |
| --- | --- | --- |
| `400` | `PhiError` | Check the request format. |
| `401` | `PhiAuthError` | Sign in again or check your API key. |
| `402` | `PhiInsufficientCreditsError` | Add credits, then submit again. |
| `403` | `PhiAuthError` | Check your organization and permissions. |
| `404` | `PhiNotFoundError` | Check the ID and which organization owns it. |
| `409` | `PhiConflictError` | Resolve the conflict named in the response. |
| `422` | `PhiValidationError` | Fix the field named in the response. |
| `429` | `PhiRateLimitError` | Wait for the time in `Retry-After`, then try again. |
| `500` | `PhiServerError` | Try again later. |
| `502` | `PhiServerError` | Try again later. |
| `503` | `PhiServerError` | Try again later. |
| `504` | `PhiServerError` | Try again later. |

```python
from phi import PhiClient, PhiInsufficientCreditsError, PhiRateLimitError

with PhiClient() as phi:
    try:
        job = phi.jobs.submit("ESMFOLD", inputs={"sequences": [sequence]})
    except PhiInsufficientCreditsError:
        print("add credits, then submit again")
    except PhiRateLimitError as err:
        print("wait", err.retry_after, "seconds")
```

## Retries

The client retries a read (`GET`, `HEAD`, or `OPTIONS`) on its own after a 429, 500, 502, 503, or 504, up to 3 more times. It waits 0.5 seconds before the first retry and doubles the wait each time. If the response has a `Retry-After` header, it waits that long instead, up to 30 seconds.

The client does not automatically retry a submit. Save an explicit idempotency key and the returned job ID. If the response never arrived, inspect Work or recent jobs before replaying the request. When Phi already has a run for the key, it normally returns that run, with the exceptions below.

The key prevents a duplicate, not a rerun of work that failed. If the job for that key ended as `FAILED`, Phi releases the key, so the next submit with it starts a new job. If that job was deleted, Phi refuses the submit with `409`; restore the run or submit without the key.

```bash
KEY=$(uuidgen)
phi jobs submit ESMFOLD \
  --input sequences=@peptide.fasta \
  --idempotency-key $KEY
```

```python
from uuid import uuid4
from phi import PhiClient

key = str(uuid4())  # Keep this key when retrying the same submission.
with PhiClient() as phi:
    job = phi.jobs.submit("ESMFOLD", inputs={"sequences": ["NLYIQWLKDGGPSSGRPPPS"]}, idempotency_key=key)
```

[Safe automation](https://dynophi.com/docs/guides/safe-automation)

## Failed runs

A run that stops early carries an `error_code` that says why and a `recommended_action` that says what to do. To see both from the command line, run `phi --json jobs status` with the run's ID.

The table lists the codes you can act on. A run can also stop with a code that is not listed here.

| Code | What it means | What to do |
| --- | --- | --- |
| `OOM` | The model ran out of memory. | Try a smaller input. |
| `VALIDATION` | Phi rejected the input. | Fix the input named in the error, then submit again. |
| `QUOTA` | The organization ran out of credits. | Add credits, then submit again. |
| `TIMEOUT` | The run took longer than Phi allows. | Try a smaller request. |
| `CANCELLED` | The run was cancelled. | Submit again if you still need the result. |

---
Source: https://dynophi.com/docs/reference/errors
