Skip to main content
Every call an SDK makes to the HiddenLayer API can fail. The network can drop, credentials can expire, or the API can reject a request. Each SDK turns these failures into specific exceptions so you can decide what to do: fix your input, retry later, or stop and alert someone. This page explains the exception types, what each one means, and how to handle them. It also covers the retries and timeouts the SDKs apply for you, and the errors that the high-level helpers can raise.

Exception hierarchy

The three SDKs share one model: an API error class that carries the HTTP status code and response body, with a subclass for each status an endpoint declares. Failures that never got a response, such as timeouts and refused connections, are reported differently in each language.
The typed errors are importable from hiddenlayer (and hiddenlayer.errors). Their base class, ApiError, and ParsingError are in hiddenlayer.core. Failures without a response come from httpx, the HTTP library the SDK uses.
hiddenlayer.NotImplementedError has the same name as Python’s built-in NotImplementedError. Refer to it as hiddenlayer.NotImplementedError rather than importing the name.
Because the hierarchy is nested, catching a parent class also catches all of its children. For example, catching the API error base class catches NotFoundError and every other typed error.

Status codes and exceptions

The typed error classes have the same names in every SDK: Python imports them from hiddenlayer, TypeScript reads them from the HiddenLayerApi namespace, and Java imports them from com.hiddenlayer.api.errors.
A method raises a typed error only for the statuses its endpoint declares in the API specification. Any other status raises the base class with that status code. For example, an endpoint that does not declare 401 raises the base class, not UnauthorizedError, when your token has expired. When a specific status matters, check the status code on the base class instead of relying on the subclass.

Exception attributes

Use these attributes to log useful details or decide how to react:
An error’s body can include parts of your input, and its headers can include identifiers. Do not log whole error objects in production. Log the status code, a short message, and only the fields you need.

Order your catch clauses from specific to general

Each language checks catch clauses (or instanceof tests) from top to bottom and runs the first one that matches. Because subclasses also match their parent classes, put the most specific exceptions first:
  1. The timeout before other failures without a response: httpx.TimeoutException before httpx.TransportError in Python, and HiddenLayerTimeoutError before HiddenLayerError in TypeScript.
  2. Typed errors, such as UnauthorizedError or NotFoundError, before the API error base class.
  3. The base classes last, as a catch-all: ApiError in Python, HiddenLayerError in TypeScript, and HiddenLayerApiException followed by HiddenLayerException in Java.
If you catch the base class first, the more specific handlers below it never run. The Java compiler reports this as an error; Python and TypeScript do not.

Complete example

This example evaluates a prompt with the Runtime Security interaction-evaluations endpoint (POST /detection/v2/interaction-evaluations) and handles every category of error. It reads credentials from the HIDDENLAYER_CLIENT_ID and HIDDENLAYER_CLIENT_SECRET environment variables. See Authentication and configuration for how to set them.
The handlers log the status code but not the error body, because the body can contain your input. While you debug a rejected request, read the body to see which field failed validation. When the call succeeds, the program prints the action and threat level. For this prompt-injection attempt, a project with the default policy returns:
The actual values depend on your project’s policy. The Python example also prints httpx request lines, because it configures INFO logging. When the call fails, the program logs one clear message and exits with a non-zero status. Each branch rethrows the exception so the caller decides whether to stop, retry later, or fall back. For guidance on what your application should do when HiddenLayer is unreachable, see Security and production guidance.

Automatic retries

The SDKs retry some failed requests automatically before raising an exception. You do not need to write your own retry loop for transient errors. What is retried by default: How retries are spaced:
  • The default is 2 retries, so up to 3 attempts in total. In Java, OkHttp also repeats a request once by itself after an HTTP 408, so a 408 can reach the server up to 6 times.
  • The delay grows exponentially, starting at 1 second and capped at 60 seconds, with random jitter so many clients do not retry at the same moment.
  • If the response includes a Retry-After or X-RateLimit-Reset header, the SDK waits that long instead, up to 60 seconds.
If every attempt fails, the SDK raises the error from the last attempt. Configure or disable retries on the client, or for one request:
The Java builder also takes retryDelays, initialRetryDelayMillis, maxRetryDelayMillis, and retryJitterFactor to change the backoff.
Implementing your own retry wrapper around SDK calls is usually unnecessary and can multiply the number of attempts. If you need more resilience, increase the retry count instead. A custom wrapper makes sense only for logic the SDK does not cover, such as retrying a whole multi-step workflow.

Timeouts

By default, a request times out after 60 seconds. A timed-out request is not retried. When a request times out, Python raises httpx.TimeoutException, TypeScript raises HiddenLayerTimeoutError, and Java raises a HiddenLayerException whose cause is an InterruptedIOException. Set the timeout in seconds on the client, or for one request:
A Java OkHttpClient you pass keeps its own timeouts unless you also call timeout(...) on the builder.
The timeout applies to each HTTP request. It does not limit how long the model scanner and community scanner wait for a scan to finish. When they wait for results, the helpers poll until the scan reaches a final status, with no overall time limit. See Model scanning.

Errors raised by helpers

The high-level helpers, such as the model scanner and the Python red team session helper, run several steps for you. In addition to the API errors above, they can raise these errors:
scan_file starts the scan on the HiddenLayer API before it reads the local file. If the path does not exist, FileNotFoundError is raised after the scan has been started. To avoid leaving an incomplete scan, check that the file exists with os.path.isfile() before calling scan_file. Whether the incomplete scan appears in the Console needs confirmation.
A scan that fails on the server does not raise an exception. The helper returns a report whose status is failed. Always check the report’s status. See Model scanning.

Red team session errors (Python)

The Python red team session helper (client.evaluation_sessions.red_team) defines its own exceptions in hiddenlayer.lib.red_team_exceptions. They do not inherit from ApiError. The TypeScript and Java SDKs have no session helper; their red team calls raise the API errors described above.
wait_for_completion() polls through 408, 429, and 5xx responses until max_wait, and raises any other API error at once.
For complete red team workflows, see AI Attack Simulation.

Beta warnings are not exceptions

Some endpoints are Beta, such as the AI Attack Simulation red team endpoints. The first time a client calls one, the SDK warns you. The warning does not stop your program. It looks like this:
The SDK emits a hiddenlayer.BetaWarning through Python’s warnings module, once per endpoint for each client. A warning cannot be caught with try/except unless you turn it into an error. To hide it once you have accepted the Beta status:
To make tests fail when code calls a Beta endpoint, turn the warning into an exception:
With this filter, every call to a Beta endpoint raises BetaWarning.
To learn what Beta status means for compatibility, see Version compatibility and upgrades.

Response validation errors

Every response is validated against its model. A response that does not match raises hiddenlayer.core.ParsingError, with the response’s status_code, headers, and body, and the validation error as cause. ParsingError is not an ApiError, so catch it separately. Fields the SDK does not know yet do not cause an error: they stay readable as attributes.

Next steps