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.