Guards and edges#

A pydantic-ai run moves values across four edges. The harness guardrails run guards on them:

Edge

Value

Guardrail

prompt

the user’s prompt, before the first model request

InputGuardrail(guard=[...])

answer

the model’s final output

OutputGuardrail(guard=[...])

tool arguments

the arguments of a tool call, before the tool runs

ToolGuardrail(guard=...)

tool result

what a tool returned, before the model sees it

ToolGuardrail(result_guard=...)

A guard is a function the harness calls with the run’s context and the value. Every check in this package (enkrypt.toxicity(), enkrypt.pii(...) and the others) returns one, ready to put on any edge:

from pydantic_ai_harness import InputGuardrail, OutputGuardrail, ToolGuardrail

no_advice = enkrypt.code_of_conduct("Never give personalized investment advice.")

capabilities = [
    InputGuardrail(guard=[no_advice]),               # the prompt
    ToolGuardrail(result_guard=enkrypt.injection_attack(), tools=["fetch_page"]),  # tool results
    OutputGuardrail(guard=[no_advice]),              # the answer
]

The same guard works on several edges, as no_advice does here.

How a guard knows its edge#

The harness does not tell a guard which edge it is on, but the action a guard takes depends on it (see Actions). Each guard works it out:

  • a tool call’s arguments and a tool result arrive as the harness’s ToolCallInfo and ToolResultInfo;

  • a prompt and an answer are plain values, told apart by the run’s history: an input guard runs before the model has answered, an output guard after.

What Enkrypt is sent#

Each guard sends one text to Enkrypt:

  • a prompt or answer that is text is sent as it is; a structured answer (a Pydantic model or other data) is sent as JSON;

  • tool arguments are sent as JSON;

  • a tool result is sent as text, or as JSON if it is not text; a ToolReturn is unwrapped to its return value.

Placeholders left by redaction, such as <<PERSON:1>>, are removed from every text first, so they are never mistaken for personal data. See Redaction and restoring.

Each guard makes one call to Enkrypt each time it runs. A guardrail saved in Enkrypt runs several detectors in one call; see guardrail.

Edges and the guards of this package#

Guard

Prompt

Answer

Tool arguments

Tool result

injection_attack, toxicity, code_of_conduct, secrets

yes

yes

yes

yes

pii

redacts

checks; redaction rarely wanted

checks, e.g. to stop exfiltration

redacts

guardrail("name")

.input

.output

.tool_result

enkrypt.restore

last in the list

yes

enkrypt.restore is not a check: it puts back personal data redacted earlier in the run. See Redaction and restoring.