- Interactions (v1):
client.interactions.analyzesends a prompt and an optional response in a simple message format and returns a detailed analysis. - Agentic evaluation endpoints (v2):
client.runtime.evaluate_request,evaluate_response, andevaluate_interactionaccept provider-native payloads (the exact JSON you send to or receive from OpenAI or Anthropic) and support agentic content such as tool calls.
Before you begin
- Install the SDK and configure credentials. See Installation and Authentication and configuration.
- Set
HIDDENLAYER_CLIENT_IDandHIDDENLAYER_CLIENT_SECRETin your environment, or setHIDDENLAYER_TOKENto a bearer token. Every example on this page reads credentials from the environment. - Your tenant needs an AI Runtime Security license. The v2 evaluation endpoints require Agentic Runtime Security access. See Get started with Agentic Runtime Security.
- No extra Python packages are needed.
Authentication and evaluation are region-specific. If your account is in the EU, create the client with
environment="prod-eu". The default is prod-us.Choose an endpoint
The v2 endpoints auto-detect these provider formats: OpenAI Chat Completions, OpenAI Responses, and Anthropic Messages. See Evaluation endpoints for details.
Analyze an interaction
Useclient.interactions.analyze to analyze a prompt, a response, or both. It is the simplest way to get a detailed security analysis, and it is not a beta endpoint.
Required and optional arguments:
metadata(required):modelandrequester_idare required.provideris optional.requester_ididentifies the user or service that sent the prompt.input: the prompt, as{"messages": [{"role": "user", "content": "..."}]}.output: the model response, in the same format.hl_project_id: the ID of a Console project. HiddenLayer applies that project’s policy. If you omit it, your tenant’s default project and its policy apply.
analyze_interaction.py
Read the result
result.evaluation.actionisAllow,Alert,Redact, orBlock. TreatBlockas “do not send or show this content”.evaluationis optional in the response type, so check forNonebefore you read it.result.evaluation.threat_levelisNone,Low,Medium,High, orCritical.result.modified_data.inputandresult.modified_data.outputcontain the messages after any redaction. When the action isRedact, forward these instead of the original text.result.analysislists each detector that ran, withname,phase,detected, andfindings.result.metadataincludes theevent_idand theprojectthat was applied.
Guard a request inline
Useevaluate_request to screen a prompt before your application sends it to the model. You pass the exact request payload you are about to send. HiddenLayer returns a payload in the same provider format:
- If the policy allows or redacts, you get back the request (possibly redacted). Forward this returned payload to the model, not your original.
- If the policy blocks, you get back a block message in the provider’s response format. Return it to the user and do not call the model.
200 in every case. A block is signaled only by the hl-runtime-action: BLOCK response header. The plain client.runtime.evaluate_request(...) method returns only the body, so use client.runtime.with_raw_response.evaluate_request(...) to read the header. Call .parse() on the raw response to get the body as a dict.
The following example screens the request, calls the model, and then screens the response with evaluate_response, which works the same way. call_model is a placeholder that returns a fixed OpenAI Chat Completions response. Replace it with your real model call.
guard_inline.py
call_model is never called.
This page does not describe the exact structure of the block message, because it follows the provider’s response format and may change. Treat it as an opaque provider response and pass it back to your caller. If you need to detect a block in later code, keep the header value instead of inspecting the body.
Evaluate an interaction with tool calls
Useevaluate_interaction when you need a structured verdict, when your traffic includes tool calls, or when you evaluate captured traffic after the fact, such as for monitoring or batch analysis. It returns a result object rather than a payload to forward.
Arguments:
metadata(required):model,provider, andrequester_idare required.external_session_idis optional and groups calls into a session.interaction(required): either a provider-native payload, or an explicitly described interaction withmessagesand typed content parts. You can also includetools_availableto describe the tools the agent can call (each withname, and optionallydescriptionandparameters).hl_project_id(optional): apply a specific project’s policy.
evaluate_interaction.py
NONE, the threat level NONE, and the detections list is empty. The exact result depends on your project’s policy.
Read the outcome
outcome.action:NONE(no detections),DETECT(detections reported, payload unchanged),REDACT(payload modified), orBLOCK(a block response is substituted).outcome.threat_level:NONE,LOW,MEDIUM,HIGH, orCRITICAL.outcome.detections: a list of detections, each withrule_nameandrisk_level. The list is empty when no rules fired.outcome.effective_interaction: the payload to forward. If you sent a provider-native payload, you get the same provider shape back as adict.result.evaluated_interaction.messages[].analysis.signals: per-message findings.
Group calls into a session
An agent often makes many model calls to complete one task. To review them together in the Console as one session, tag every call with the same session ID:- For
evaluate_requestandevaluate_response, passhl_runtime_session_id=session_id. - For
evaluate_interaction, setmetadata["external_session_id"]. - To pair a request with its response, pass the same
HL-Roundtrip-Idheader on both, usingextra_headers={"HL-Roundtrip-Id": roundtrip_id}. There is no named argument for it.
Beta warnings
When you call a beta endpoint, the SDK emits ahiddenlayer.BetaWarning through Python’s standard warnings module. The message looks like this:
BetaWarning is a subclass of UserWarning. It does not stop your code. To hide it, add a filter once at startup:
The SDK changelog for version 3.9.0 says the beta label was removed from the v2 runtime endpoints, but version 3.10.0 still emits
BetaWarning for evaluate_request, evaluate_response, and evaluate_interaction. The current status of these endpoints needs confirmation. See Version compatibility and upgrades and the Beta API FAQ.Common errors and fixes
AuthenticationError (401)
AuthenticationError (401)
Check that
HIDDENLAYER_CLIENT_ID and HIDDENLAYER_CLIENT_SECRET (or HIDDENLAYER_TOKEN) are set in the process that runs your code. Credentials are region-specific, so make sure the client’s environment (prod-us or prod-eu) matches your account. See Authentication and configuration.PermissionDeniedError (403) or missing features
PermissionDeniedError (403) or missing features
Your tenant or API key may not have access to Runtime Security or Agentic Runtime Security. Contact your HiddenLayer account representative or Support.
BadRequestError (400) or UnprocessableEntityError (422)
BadRequestError (400) or UnprocessableEntityError (422)
The payload does not match what the endpoint expects. Common causes:
- Sending an explicitly described interaction (typed content parts) to
evaluate_requestorevaluate_response. Those endpoints accept only provider-native payloads. Useevaluate_interactioninstead. - Sending a response payload to
evaluate_request, or a request payload toevaluate_response. - Missing required
metadatakeys, such asrequester_id.
e.body from the exception for details from the API.Blocked prompts still reach the model
Blocked prompts still reach the model
You are probably calling
client.runtime.evaluate_request(...) directly, which returns only the body. Use client.runtime.with_raw_response.evaluate_request(...) and check the hl-runtime-action header, as shown in Guard a request inline.evaluation is None on the Interactions response
evaluation is None on the Interactions response
evaluation is optional in InteractionAnalyzeResponse. Check for None before reading evaluation.action, and fall back to result.analysis if needed.ValueError: Ambiguous URL
ValueError: Ambiguous URL
You passed
environment while the HIDDENLAYER_BASE_URL environment variable is set. Unset the variable, or remove the environment argument.Wrong policy applied
Wrong policy applied
Pass
hl_project_id with the ID of the project whose policy you want. Without it, your tenant’s default project and its policy apply. Check result.metadata.project to see which project was used.
