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

# Error Handling

> Example of error handling with SDK exceptions.

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    def error_handling_example(client: Cartesia) -> None:
        """Example of error handling with SDK exceptions."""
        from cartesia import APIError, NotFoundError, RateLimitError, BadRequestError, AuthenticationError

        try:
            client.tts.generate(
                model_id="sonic-latest",
                transcript="",  # empty transcript will cause a 400 bad request response
                voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
                output_format={"container": "wav", "encoding": "pcm_s16le", "sample_rate": 44100},
                language="en",
            )
        except BadRequestError as e:
            print(f"Bad request (expected): {e.message}")
        except AuthenticationError as e:
            print(f"Auth failed: {e.message}")
        except NotFoundError as e:
            print(f"Not found: {e.message}")
        except RateLimitError as e:
            print(f"Rate limited: {e.message}")
        except APIError as e:
            print(f"API error: {e.message}")
    ```

    From [cartesia-python/examples/examples.py:825](https://github.com/cartesia-ai/cartesia-python/blob/v3.2.0/examples/examples.py#L825)
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    async function errorHandling(client: Cartesia): Promise<void> {
      try {
        await client.tts.generate({
          model_id: 'sonic-latest',
          transcript: 'Hello, world!',
          voice: { mode: 'id', id: '6ccbfb76-1fc6-48f7-b71d-91ac6298247b' },
          output_format: null as any, // bad request
          language: 'en',
        });
      } catch (e) {
        if (e instanceof BadRequestError) {
          console.log(`Bad request: ${e.message}`);
        } else if (e instanceof AuthenticationError) {
          console.log(`Auth failed: ${e.message}`);
        } else if (e instanceof NotFoundError) {
          console.log(`Not found: ${e.message}`);
        } else if (e instanceof RateLimitError) {
          console.log(`Rate limited: ${e.message}`);
        } else if (e instanceof APIError) {
          console.log(`API error: ${e.message}`);
        } else {
          throw e;
        }
      }
    }
    ```

    From [cartesia-js/examples/node\_examples.ts:835](https://github.com/cartesia-ai/cartesia-js/blob/v3.2.0/examples/node_examples.ts#L835)
  </Tab>
</Tabs>

## Run this example

<Tabs>
  <Tab title="Python">
    ```sh theme={null}
    git clone --branch v3.2.0 https://github.com/cartesia-ai/cartesia-python
    cd cartesia-python
    uv sync
    CARTESIA_API_KEY=YOUR_KEY uv run examples/examples.py error_handling_example
    ```
  </Tab>

  <Tab title="TypeScript">
    ```sh theme={null}
    git clone --branch v3.2.0 https://github.com/cartesia-ai/cartesia-js
    cd cartesia-js
    pnpm i
    CARTESIA_API_KEY=YOUR_KEY pnpm tsn examples/node_examples.ts errorHandling
    ```
  </Tab>
</Tabs>
