- Download a third-party model and want to check it before loading it.
- Want to block risky models in a CI/CD pipeline.
- Need a record of scan results for models stored in Hugging Face, AWS S3, or Azure Blob Storage.
Before you begin
- Install the SDK and configure credentials. See Installation and Authentication and configuration.
- Set
HIDDENLAYER_CLIENT_IDandHIDDENLAYER_CLIENT_SECRETin your environment. Alternatively, setHIDDENLAYER_TOKENto a bearer token. Every example on this page reads credentials from these variables. None of them hard-code secrets. - Your tenant must have AI Supply Chain Security enabled, and your API key needs permission to use it.
Understand the scan report
Every scan method returns aScanReport. A ScanReport is a Pydantic model, which means you read its fields as attributes, such as report.status. Import the type with from hiddenlayer.types.scans import ScanReport if you want type hints.
The fields you use most often are:
file_results[].detections includes severity (critical, high, medium, or low), category, description, risk (MALICIOUS or SUSPICIOUS), rule_id, and mapping fields such as cve, cwe, mitre_atlas, and owasp.
report.status before you trust the summary. A failed scan may have zero detections only because the scan did not complete.Choose a scan method
wait_for_results (default True). When it is True, the method blocks until the scan finishes. See Start a scan without waiting for the alternative.
Scan a local file
Usescan_file to scan one model file, such as a .pkl, .pt, .h5, or .safetensors file. The SDK uploads the file in parts, tells HiddenLayer the upload is complete, and then polls until the scan finishes.
The following script scans a file, prints the results, and exits with a non-zero code if the scan fails or finds a critical or high detection. You can run it as a CI gate: most CI systems fail the job when a step exits with a non-zero code.
done, the highest severity is typically none, and the script exits with code 0.
The model_name and model_version values identify the model in the HiddenLayer Console. model_version defaults to "1".
Scan a folder
Usescan_folder when a model consists of several files, such as a weights file plus configuration and tokenizer files. The SDK walks the folder recursively and uploads every file that passes the filters.
You can filter files with fnmatch patterns (shell-style wildcards such as *.bin):
allow_file_patterns: if set, only files that match at least one pattern are scanned.ignore_file_patterns: files that match any pattern are skipped.
*.txt, *.md, *.lock, .gitattributes, and .git directories.
report.file_results.
Scan a Hugging Face model
Usescan_huggingface_model to scan a model repository on Hugging Face. The SDK downloads the repository with huggingface_hub, then scans the downloaded folder.
Extra prerequisite: pip install huggingface_hub.
For private or gated repositories, provide a Hugging Face access token with hf_token:
- Pass a string to use that token. The example reads it from an
HF_TOKENenvironment variable that you set yourself. - Pass
Trueto read the token saved in your local Hugging Face configuration (for example, after you log in with the Hugging Face CLI).
model_name, the SDK uses repo_id as the model name. If you omit revision, the model version is "1". Other options are local_dir (download location), ignore_file_patterns, and force_download.
Expected result: a report for the downloaded repository with report.inventory.origin set to Hugging Face.
Scan a model in AWS S3
Usescan_s3_model to scan a single file stored in S3. The SDK downloads the file with boto3 and then calls scan_file.
Extra prerequisite: pip install boto3.
By default the SDK creates boto3.client("s3"), which uses the standard AWS credential chain. That chain checks sources such as the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, your AWS config files, and attached IAM roles. Pass your own client with s3_client if you need a specific profile or region.
report.inventory.origin set to S3.
Scan a model in Azure Blob Storage
Usescan_azure_blob_model to scan a single blob. The SDK downloads the blob with the Azure Storage SDK and then calls scan_file.
Extra prerequisite: pip install azure-identity azure-storage-blob.
By default the SDK authenticates with DefaultAzureCredential, which tries several Azure sign-in methods, such as environment variables, managed identity, and the Azure CLI. To use a shared access signature (SAS) token instead, pass it as credential. Read it from an environment variable rather than writing it in your code.
- DefaultAzureCredential
- SAS token
report.inventory.origin set to Azure Blob Storage.
Temporary files for downloaded models
Scan a remote model with the community scanner
Useclient.community_scanner.community_scan when HiddenLayer can reach the model directly, for example through a presigned URL or a public Hugging Face repository. HiddenLayer fetches the model itself, so nothing is downloaded to or uploaded from your machine.
Set model_source to one of the CommunityScanSource constants:
HUGGING_FACE example below follows the SDK’s own usage.Start a scan without waiting
By default, scan methods block until the scan finishes. Passwait_for_results=False to return as soon as the scan starts. This is useful in web services or pipelines where you do not want to hold a process open.
With wait_for_results=False:
- For upload-based methods, the upload itself still finishes before the method returns. Only the wait for the analysis is skipped.
- The returned report is a point-in-time snapshot. Its
statusis oftenpendingorrunning, andfile_resultsmay be incomplete. - Save
report.scan_id, then callscan_result(scan_id=...)later to fetch an updated report. Bothclient.model_scannerandclient.community_scannerprovidescan_result.
hiddenlayer.NotFoundError if the scan still cannot be found.
Wait with a time limit
Whenwait_for_results=True, the SDK polls the scan status with exponential backoff (up to 30 seconds between checks) until the scan is done, failed, or canceled. There is no overall timeout. If a scan stays in pending or running, your program waits indefinitely.
In CI/CD or other unattended environments, start the scan with wait_for_results=False and poll yourself with a deadline. client.scans.results.retrieve_summary(scan_id) is a lightweight call that returns the status and summary without the per-file results.
Common errors and fixes
ImportError: Python package huggingface_hub (or boto3, azure-identity, azure-storage-blob) is not installed.
ImportError: Python package huggingface_hub (or boto3, azure-identity, azure-storage-blob) is not installed.
pip install boto3.AuthenticationError (401)
AuthenticationError (401)
HIDDENLAYER_CLIENT_ID and HIDDENLAYER_CLIENT_SECRET are set in the same shell or process that runs the script. Also check that the environment matches your account’s region (prod-us or prod-eu). See Authentication and configuration.PermissionDeniedError (403)
PermissionDeniedError (403)
RuntimeError: Couldn't download model ...
RuntimeError: Couldn't download model ...
FileNotFoundError or the script cannot find the model
FileNotFoundError or the script cannot find the model
model_path and path are resolved relative to the current working directory. Use an absolute path, or run the script from the expected directory.The scan returns with status failed
The scan returns with status failed
file_error on each entry in report.file_results for scanner error messages, and check report.summary.files_failed_to_scan.The script hangs while waiting for results
The script hangs while waiting for results
wait_for_results=True has no overall timeout. Use the pattern in Wait with a time limit.NotFoundError right after starting a scan
NotFoundError right after starting a scan
wait_for_results=False, the SDK retries a few times while a new scan becomes available. If it still raises NotFoundError, wait briefly and call scan_result(scan_id=...) again. Also confirm you are using the same environment (region) that started the scan.
