Adapter Contract (SDK Reference)
Focus
Focus
Prisma AIRS

Adapter Contract (SDK Reference)

Table of Contents

Adapter Contract (SDK Reference)

The code contract for a custom target adapter: the functions you implement, what the platform passes in, and what you return.
Where Can I Use This?What Do I Need?
  • Prisma AIRS (AI Red Teaming)

Function Patterns

Implement one of two request patterns. You may add the optional authenticate() and session_pre_process() hooks to either pattern.
  • Pattern A — pre_process and post_process
    You build the request, and the platform makes the HTTP call and passes the response to your post_process() function. Use this pattern for standard HTTP targets, including those that require multiple HTTP calls. Use context.http inside pre_process() for additional per-turn calls. Use session_pre_process() for one-time setup.
  • Pattern B — call_target
    You make the call(s) yourself and return the reply. Use this when the target is not plain request/response HTTP: WebSocket, GraphQL, SDK-based clients, or custom framing and streaming protocols.
If you define both patterns in the same script, call_target takes precedence and pre_process or post_process are not called. Either pattern can add the optional authenticate() and session_pre_process() hooks.
FunctionPatternRequired?Purpose
pre_process(context, inference_input)AYes (Pattern A)Build the HTTP request to send to your target.
post_process(context, raw_response)AYes (Pattern A)Extract the reply text from the target's HTTP response.
call_target(context, inference_input)BYes (Pattern B)Make the call(s) yourself and return the reply.
authenticate(context)EitherNoObtain and cache authentication credentials; refreshed automatically by the platform.
session_pre_process(context)EitherNoOne-time setup on the first turn of every conversation.
The following names are available in your script without importing:
InferenceInput, PreProcessResult, PostProcessResult, CallTargetResult, AuthResult, SessionPreProcessResult, RawResponse raise_rate_limited(), raise_auth_error(), raise_target_error(), raise_content_filtered()
For all other dependencies, use the Python standard library and the bundled HTTP client. See Available Packages.

Conventions

Follow these conventions to avoid common adapter errors.
  • Return the declared result type.
    If your function returns a raw Python string or a raw dictionary directly, instead of wrapping the value in the required result type (for example, PostProcessResult(output="hello")), AI Red Teaming treats the return value as an adapter error.
    # Incorrect — returns a bare string def post_process(context, raw_response): return raw_response.json_body["reply"] # Correct — wrap in the result type def post_process(context, raw_response): return PostProcessResult(output=raw_response.json_body["reply"])
  • Carry state in session_state, not in module-level variables.
    In the adapter runtime, the Python script is not kept running as a persistent process between turns in a conversation. On each turn, that is when each time a message is sent to the target, the runtime reimports and re-executes the script from scratch.
    This means any variable defined at the module level (outside a function) resets to its initial value at the start of every turn. If you assign a value to a module-level variable during one turn, that value is gone by the next turn.
    # Incorrect — module-level state is wiped between turns _SESSION = {} def session_pre_process(context): _SESSION["session_id"] = "abc123" # Correct — return state in session_state; read it from context.session on the next turn def session_pre_process(context): return SessionPreProcessResult(session_state={"session_id": "abc123"})
  • Import only standard library and bundled packages.
    Place all imports at the top of the adapter script.
  • Keep calls bounded.
    Set your own timeouts on anything context.http does not cover. Avoid unbounded loops.

Available Packages

The adapter runner uses Python 3.12 with the full standard library (including json, base64, uuid, time, datetime, hmac, hashlib, and re). Use context.http (backed by httpx) for all HTTP calls rather than importing another client. The websockets package is also bundled for WebSocket targets.
Other third-party packages are not available. Contact AI Red Teaming to request a package. Where possible, use context.http and the standard library.