guardrail#
enkrypt.guardrail(name, *, on_violation=DEFAULT_ACTIONS, on_error="block")
Runs a guardrail saved in Enkrypt: a named set of detectors configured in Enkrypt’s dashboard, with separate detectors for input and output.
Allowed actions |
|
Default action |
per detector, from |
Enkrypt endpoint |
the saved guardrail, by name |
One guard per edge#
guardrail() returns a Guardrail with one guard per edge:
Property |
Use in |
Runs the guardrail’s |
|---|---|---|
|
|
input detectors |
|
|
output detectors |
|
|
input detectors: a tool result is untrusted input to the model |
g = enkrypt.guardrail(os.environ["ENKRYPTAI_GUARDRAIL"])
agent = Agent(
FunctionModel(echo),
capabilities=[InputGuardrail(guard=[g.input]), OutputGuardrail(guard=[g.output])],
)
Which detectors run is the saved guardrail’s business; changing them in Enkrypt changes what the agent checks without a code change.
Actions per detector#
One guardrail can flag several detectors at once, so on_violation takes a mapping of detector, or personal data entity type, to action. It is merged over DEFAULT_ACTIONS:
enkrypt.guardrail("brokerage-support", on_violation={"injection_attack": "raise"})
changes only injection attacks; secrets still raise and the rest still block. A single action applies to everything:
enkrypt.guardrail("brokerage-support", on_violation="audit")
When several detectors fire with different actions, the strictest wins.
Personal data in a saved guardrail#
If the saved guardrail detects personal data, it blocks by default. To redact instead, map "pii" to redact:
g = enkrypt.guardrail("brokerage-support", on_violation={"pii": "redact"})
capabilities = [
InputGuardrail(guard=[g.input]),
OutputGuardrail(guard=[g.output, enkrypt.restore]),
]
Redaction works as for pii(), with the same placeholders, and enkrypt.restore puts back what either redacted. Secrets in the same value still raise, because raise is stricter than redact.
Saved guardrail or inline guards#
Saved guardrail |
Inline guards |
|
|---|---|---|
Calls per edge |
one |
one per guard |
Where checks are defined |
Enkrypt’s dashboard |
your code |
Actions |
per detector, one mapping |
per guard |
Order between checks |
none: one response |
the list order |
Use a saved guardrail for fewer calls, which matters under Enkrypt’s rate limits, or when the checks are managed outside the code. Use inline guards for per-edge differences or to put local guards between Enkrypt checks. They mix freely in one list. See Chains and ordering.
Reference#
pydantic_ai_enkrypt.Enkrypt.guardrail(), pydantic_ai_enkrypt.Guardrail, pydantic_ai_enkrypt.DEFAULT_ACTIONS