The client
The client is the object that holds your configuration (credentials, region, timeouts, retries) and sends requests to the HiddenLayer API. The SDK provides two clients with the same constructor arguments and the same methods:Client and AsyncClient are aliases for the same classes. With the async client, you await every method call.
Create one client and reuse it
Create the client once (for example at application startup) and share it. Reusing a client matters for two reasons:- Connection reuse. The client keeps a pool of HTTP connections open, so later requests are faster.
- Token caching. With client ID and client secret authentication, the client requests an access token once, caches it, and refreshes it shortly before it expires. The cache is protected by a lock, so a single client can be shared safely across threads. A new client per request would fetch a new token every time.
with block (as shown above) or by calling client.close() (await client.close() for the async client). If you do neither, connections close when the client is garbage collected.
Resources and methods
The client groups API operations into resources. You call an operation asclient.<resource>.<method>(). Some resources are nested, such as client.scans.jobs.list().
See the Python API reference for every method signature.
High-level helpers and low-level resources
Most resources map one-to-one to a REST endpoint. These are low-level resources: one method call sends one HTTP request. Three attributes are high-level helpers written by HiddenLayer on top of the low-level resources:client.model_scanneruploads model files in parts, starts the scan, polls until it finishes, and collects all file results into oneScanReport.client.community_scannerasks HiddenLayer to fetch and scan a model from a remote location, then optionally waits for results.client.evaluation_sessions.red_teamstarts a red team workflow and drives it with polling, iteration, or a callback function.
Environments and base URL
An environment is the HiddenLayer region your account lives in. The client turns the environment into a base URL, the address every request starts with.
The client chooses the base URL in this order:
- The
base_urlconstructor argument. - The
environmentconstructor argument. - The
HIDDENLAYER_BASE_URLenvironment variable. prod-us.
environment while HIDDENLAYER_BASE_URL is also set, the constructor raises a ValueError (“Ambiguous URL”). Set only one of them. Use base_url for a self-hosted endpoint, such as a local AI Runtime Security container.
Credentials are region specific, because the token request goes to the same base URL. Use credentials created in the same region you connect to.
Authentication
The client supports two authentication methods:- OAuth2 client credentials (recommended). OAuth2 client credentials is a standard flow where your program exchanges a client ID and client secret for a short-lived access token. Set
HIDDENLAYER_CLIENT_IDandHIDDENLAYER_CLIENT_SECRET. The client fetches, caches, and refreshes the token for you. - Bearer token. Set
HIDDENLAYER_TOKENto an access token you obtained elsewhere. The client sends it as-is and does not refresh it.
Request parameters are TypedDicts
Method arguments that contain nested data, such asmetadata or input for client.interactions.analyze(), are typed as TypedDicts. A TypedDict is a normal Python dict with a declared set of keys and value types. Because of this:
- You pass plain dictionaries. You do not need to import or construct special classes.
- Editors and type checkers (such as Pyright or mypy) can autocomplete keys and flag typos.
Responses are Pydantic models
Methods return Pydantic models. Pydantic is a data validation library. A Pydantic model is a Python object whose attributes are parsed from JSON and have declared types. You read values with attribute access, for examplereport.summary.highest_severity.
Useful helpers on every response model:
hiddenlayer.types, for example from hiddenlayer.types import InteractionAnalyzeResponse or from hiddenlayer.types.scans import ScanReport. Import them for type hints.
A few endpoints return plain Python types instead of models. For example, client.scans.results.sarif() returns a str, and client.runtime.evaluate_request() returns a dict.
Projects and the hl_project_id parameter
In AI Runtime Security, a project groups your application’s traffic and selects the policy used to evaluate it. Runtime methods (interactions.analyze, runtime.evaluate_*, prompt_analyzer.create) and evaluations.red_team.create accept an optional hl_project_id argument. The SDK sends it as the HL-Project-Id HTTP header. If you omit it, the API applies its default behavior.
hiddenlayer_project_id instead. See Runtime security and the policy documentation.
Beta APIs and BetaWarning
Some endpoints are Beta: they are not generally available and can change without notice, including breaking changes. Beta functions and types are excluded from the SDK’s semantic versioning guarantees. See the Beta API guidelines.
When you call a Beta endpoint, the SDK emits a hiddenlayer.BetaWarning (a subclass of Python’s UserWarning) through the standard warnings module. In SDK version 3.10.0 this applies to:
client.runtime.evaluate_interaction,evaluate_request, andevaluate_response- All
client.evaluations.red_teammethods, including calls made byclient.evaluation_sessions.red_team
The 3.9.0 changelog says the Beta label was removed from the runtime v2 endpoints, but version 3.10.0 still emits
BetaWarning for them. This discrepancy needs confirmation.Pagination
Endpoints that return many items use one of three patterns. Each one needs different code, so check the return type in the API reference.Raw and streaming responses
By default, methods return the parsed body only. Two prefixes give you access to the underlying HTTP response:client.<resource>.with_raw_response.<method>(...)returns anAPIResponsewith.status_code,.headers, and.parse()(which returns the normal parsed object). Use it when you need a response header, such as thehl-runtime-actionheader from the agentic evaluation endpoints.client.<resource>.with_streaming_response.<method>(...)must be used in awithblock. It does not read the body until you call a method such as.iter_bytes(),.iter_lines(),.json(), or.parse(). Use it for large responses.
Retries and timeouts
The client retries failed requests automatically so that short outages do not break your program.client.with_options() returns a new client that shares the original’s connection pool, so it is cheap to call.
Retries and timeouts apply to each HTTP request. They do not limit how long
model_scanner or community_scanner wait for a scan when wait_for_results=True. Those helpers poll until the scan finishes, with no overall time limit. See Model scanning.Logging
The SDK uses Python’s standardlogging module. Set the HIDDENLAYER_LOG environment variable to turn it on:
infoenables informational messages from the SDK and its HTTP library (httpx), such as requests sent and retries.debugadds detailed request information, including request bodies.

