None, never the validator’s reason.
Quick Start
1
Add an input guardrail
2
Get the fixed string back
reply is always a str, so reply.strip() never crashes.3
Read the reason for your logs
How It Works
The agent checks the prompt first; a rejected prompt never reaches the LLM. The same fixed string is returned when an output guardrail exhausts its retries, so callers handle one shape for every block.Before / after PR #6104
Telling a block from a normal reply
The blocked string is identical for input and output blocks, so compare against it rather than parsing it.Call
_guardrail_blocked_message() once at startup only — it also marks any in-flight run as error, so don’t call it per turn.Common Patterns
Pattern A — friendly message in a chat UI
Pattern B — log the reason, keep the reply opaque
Pattern C — async parity
Pattern D — streaming
"[Input blocked", which is no longer emitted.
Best Practices
Never surface the raw reason to end users
Never surface the raw reason to end users
The reason can echo the rejected prompt back and defeat the guardrail. Show your own message instead.
Use last_guardrail_error for ops and telemetry only
Use last_guardrail_error for ops and telemetry only
Log it, alert on it, count it — don’t display it.
Treat the blocked string as opaque
Treat the blocked string as opaque
Compare it to the
BLOCKED value you captured at startup; don’t parse or translate it.Keep input validators fast and pure
Keep input validators fast and pure
A sync
validate_input that does blocking I/O delays every turn. See Per-Tool Guardrails for the async offload story on tool and output guardrails.Learned “always allow” rules survive concurrent writes
Concurrency-safe since PR #6104.PolicyConfig.save_learned_rule now reads, dedupes and appends under a per-path lock using utils.atomic_io.update_json. Multiple processes or threads saving “always allow” decisions no longer drop each other’s rules; dedupe behaviour is preserved (saving the same resource twice is a no-op, and the first action wins).
Related
Guardrails
Input and output validation for agents
Apply Guardrail
Run a guardrail on any text directly
Per-Tool Guardrails
Validate individual tool calls
last_guardrail_error
SDK reference

