InferenceInput, PreProcessResult, PostProcessResult, CallTargetResult,
AuthResult, SessionPreProcessResult, RawResponse
AudioFormat, FileMimeType, ModalityInput, Data, FormatItems
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.
Version Requirements
The base adapter feature works with every client from v1.4.0. Some features need a
newer
Network Channel client, because the adapter runner ships inside the
client.
| Feature | Minimum client |
| Custom target adapters (all functions, context,
text input) | v1.4.0 |
| Google Vertex AI libraries in the adapter runner | v1.4.4 |
| Audio and document attacks, and audio replies | v1.5.0 |
On a client older than v1.9.0, audio and documents still arrive, in the deprecated
audio_b64, file_b64, and related fields.
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.
For Google Vertex AI targets (client v1.4.4 or later), the runner also bundles
google-auth, google-cloud-aiplatform
(including the vertexai namespace), and
google-genai. Use them to authenticate to Google Cloud and call
a Vertex AI Agent Engine or Gemini target from your adapter.
Other third-party packages are not available. Contact AI Red Teaming to request a
package. Where possible, use context.http and the standard
library.
Runtime Limits
The adapter runtime enforces these limits on every call. A turn fails if your
function raises an exception or exceeds a limit.
| Limit | Value | Notes |
| Overall execution time per call | 110 s | Your functions must finish within this window. |
| HTTP timeout for context.http | 100 s | Set your own timeouts on anything context.http
does not cover. |
| Carried state size (auth and
session, each) | 64 KB | Serialized. Checked separately for auth and
session. Returning more state than this fails
the turn. |
| Reply size | 10 MB | The whole adapter reply, including any
audio_b64. A larger reply fails that
turn. |
| Printed output | 64 Ki characters | Applies separately to standard output and standard error. Output
past the limit is cut off, and a line reading [output
truncated: more than N characters] is added. |
Audio clips and documents can be tens of megabytes, and the sidecar holds what your
script keeps while the target answers. To keep memory use low, pass
.b64 to your target as-is, build the request body once, and
don't print() payloads. For audio replies, ask your target for a
compressed format such as MP3 if it offers one; uncompressed WAV speech passes the
10 MB reply limit after about three minutes.