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.- Python
- TypeScript
- Java
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.NotFoundError and every other typed error.
Status codes and exceptions
The typed error classes have the same names in every SDK: Python imports them fromhiddenlayer, TypeScript reads them from the HiddenLayerApi namespace, and Java imports them from com.hiddenlayer.api.errors.
Exception attributes
Use these attributes to log useful details or decide how to react:Order your catch clauses from specific to general
Each language checks catch clauses (orinstanceof 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:
- The timeout before other failures without a response:
httpx.TimeoutExceptionbeforehttpx.TransportErrorin Python, andHiddenLayerTimeoutErrorbeforeHiddenLayerErrorin TypeScript. - Typed errors, such as
UnauthorizedErrororNotFoundError, before the API error base class. - The base classes last, as a catch-all:
ApiErrorin Python,HiddenLayerErrorin TypeScript, andHiddenLayerApiExceptionfollowed byHiddenLayerExceptionin Java.
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.
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-AfterorX-RateLimit-Resetheader, the SDK waits that long instead, up to 60 seconds.
retryDelays, initialRetryDelayMillis, maxRetryDelayMillis, and retryJitterFactor to change the backoff.
Timeouts
By default, a request times out after 60 seconds. A timed-out request is not retried. When a request times out, Python raiseshttpx.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:
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:- Python
- TypeScript
- Java
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.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.
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:- Python
- TypeScript
- Java
The SDK emits a 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
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:BetaWarning.Response validation errors
- Python
- TypeScript
- Java
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
- Troubleshooting: fix specific errors step by step.
- Security and production guidance: configure retries, timeouts, and failure behavior for production.

