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

# Errors and retries

> Handle errors and retries in the Python SDK.

## Errors

If a call fails, the SDK raises one of these errors:

| Error | When the SDK raises it |
| - | - |
| `APIError` | Infisical returned an error response. The error's `status_code` holds the [HTTP status code](https://infisical.com/docs/api-reference/overview/errors.md#common-status-codes), and its `response` holds Infisical's description of the error. |
| `InfisicalError` | Infisical's response wasn't valid JSON |
| `requests.exceptions.ConnectionError`, `requests.exceptions.Timeout`, and other network errors | The request still failed after the SDK retried it |
| `ValueError` | A method got arguments it can't use, such as neither `project_id` nor `project_slug`, and the SDK sent nothing to Infisical |

`APIError` is a subclass of `InfisicalError`, and you can import both from `infisical_sdk`.

This example creates a secret if it doesn't exist yet:

```python theme={"dark"}
from infisical_sdk import APIError

try:
    client.secrets.get_secret_by_name(
        secret_name="SESSION_KEY",
        project_id="<project-id>",
        environment_slug="dev",
        secret_path="/",
    )
except APIError as err:
    if err.status_code != 404:
        raise

    client.secrets.create_secret_by_name(
        secret_name="SESSION_KEY",
        project_id="<project-id>",
        environment_slug="dev",
        secret_path="/",
        secret_value="<new-session-key>",
    )
```

## Retries

If a request fails with a network error, such as a refused connection or a timeout, the SDK retries the request up to four times. It waits about one second before the first retry and doubles the wait before each retry after that. Most requests that create or delete something in Agent Vault are retried only if the connection never opened, so a retry can't make the change twice. Requests from `delegations.set` and `delegations.add_subjects` are retried after any network error.

The SDK doesn't retry a request that Infisical [rate-limits](https://infisical.com/docs/api-reference/overview/rate-limits.md). It raises an `APIError` whose `status_code` is `429`.

The SDK sets no timeout on requests, and you can't change the number of retries.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.