Skip to main content
The HiddenLayer platform enhances the developer experience for protecting artificial intelligence (AI) and machine learning (ML) models without needing to write complex code or manage the underlying infrastructure. The HiddenLayer SDK uses Python to provide a simple and efficient way to interact with the HiddenLayer API. This guide will walk you through how to install and use the HiddenLayer Python SDK to retrieve AI Runtime Security and AI Supply Chain Security information. This SDK can be used to interact with the following HiddenLayer services:
  • AI Supply Chain Security (model scanning)
  • AI Runtime Security — Interactions (LLM input/output analysis)
  • AI Runtime Security — Agentic evaluation endpoints
  • AI Attack Simulation (red team evaluations)
Active DevelopmentThis project is under active development. The full API surface is documented in the SDK’s api.md. Some endpoints are marked Beta; see the Beta API Guidelines on the Developer Portal.

Before You Begin

The following are required for using the HiddenLayer Python SDK:
  • Python 3.9+ (this should include pip)
  • HiddenLayer API key and secret; see Create API Key

Install SDK

Install the hiddenlayer-sdk package with pip.
The HiddenLayer Python SDK offers functionality to interact with other services, such as Hugging Face, AWS, and Azure.
  • For the optional async aiohttp HTTP backend:
  • To scan models from Hugging Face, AWS S3, or Azure Blob Storage, install the platform packages separately as needed:

Usage Overview

The main client exposed by the SDK is hiddenlayer.HiddenLayer, which provides access to all HiddenLayer services exposed via API. An async equivalent (AsyncHiddenLayer) is also available with the same interface.
API methods are grouped by resource on the client, for example:
For the full list of resources and methods, see the SDK’s api.md and the Developer Portal.

Authentication

To authenticate to HiddenLayer, generate a client ID and secret from the platform UI. See Create API Key. The SDK supports two authentication methods:
  • OAuth2 client credentials — set HIDDENLAYER_CLIENT_ID and HIDDENLAYER_CLIENT_SECRET, or pass client_id / client_secret directly.
  • Bearer token — set HIDDENLAYER_TOKEN, or pass bearer_token directly.
To target a custom endpoint (for example a locally running Runtime Security container), set base_url when constructing the client, or set the HIDDENLAYER_BASE_URL environment variable.

Async Usage

An async client is available with the same interface as the synchronous client. Import AsyncHiddenLayer and await each API call:
For improved concurrency with the async client, install hiddenlayer-sdk[aiohttp] and pass http_client=DefaultAioHttpClient(). See the SDK README for details.

Data Models

The HiddenLayer Python SDK uses Pydantic to represent data for APIs, which makes the code more readable and type-safe and easier to work with. Request parameters use TypedDicts. Responses are Pydantic models under hiddenlayer.types, with helpers such as model.to_json() and model.to_dict(). Specific data models are organized under hiddenlayer.types. Each resource exposes its own request and response types — see the SDK’s api.md for the full inventory.

Example Usage

The HiddenLayer Python SDK comes with a number of examples demonstrating how to use the library for various common use-cases. These examples and more are located in the examples directory of the GitHub repository (including demo.py for model scanning and red_team/ for Attack Simulation).

Initiate Client

Scanning Models

Scanning a model on disk

You can also scan an entire directory with client.model_scanner.scan_folder(...).

Scanning a Hugging Face model

Requires pip install huggingface_hub.
Alternatively, use the community scanner without downloading the model first:

Scanning from cloud storage

Runtime Security

Analyzing LLM Interactions

Use client.interactions.analyze to send LLM input and output to the Interactions endpoint. For a full walkthrough and an example response, see Getting Started with Interactions.
To target a locally running Runtime Security container instead of the SaaS endpoint, set base_url="http://localhost:8000" (or your container’s URL) when constructing the client.

Agentic evaluation endpoints

Use client.runtime.evaluate_request, client.runtime.evaluate_response, and client.runtime.evaluate_interaction for Agentic Runtime Security use cases. For a full walkthrough, see Get Started with Agentic Runtime Security.

AI Attack Simulation

Use client.evaluation_sessions.red_team (high-level session helpers) or client.evaluations.red_team (low-level API) to drive red team evaluations. For a full walkthrough, see Configuring an Attack Simulation against an Application.

Advanced Configuration

The SDK supports retries, timeouts, pagination helpers, raw/streaming responses, and a custom httpx client. Defaults and examples are in the SDK README. Common client options include:
To check the installed package version at runtime: