Guardrails
Validate input and output before it reaches the user.
Guardrails control what an agent is allowed to accept as input and what it's allowed to return as output — without you having to write a moderation pipeline by hand.
The simplest guardrail is a single sentence describing what to block.
agent = Agent(
name="Support Agent",
instructions="Help customers with product questions.",
model="gemini-2.5-flash",
input_guardrails=[
"Block mathematical questions."
],
output_guardrails=[
"Block responses that mention competitor products."
],
)Input Guardrails#
Input guardrails check the user's message before it ever reaches the model. If it matches a blocked policy, the SDK raises InputGuardrailTripwireTriggered instead of generating a response.
result = agent.run("What is 348 * 27?")Example Output
abzagent.InputGuardrailTripwireTriggered: Input blocked — matches policy "Block mathematical questions."Output Guardrails#
Output guardrails check the model's response before run() returns it to you. If the response matches a blocked policy, the SDK raises OutputGuardrailTripwireTriggered instead of handing back the content.
result = agent.run("Is your product better than BrandX?")Example Output
abzagent.OutputGuardrailTripwireTriggered: Response blocked — matches policy "Block responses that mention competitor products."Handle both exceptions the same way you'd handle any other Python exception:
from abzagent import Agent, InputGuardrailTripwireTriggered, OutputGuardrailTripwireTriggered
try:
result = agent.run("What is 348 * 27?")
print(result.content)
except InputGuardrailTripwireTriggered:
print("Sorry, I can't help with that.")
except OutputGuardrailTripwireTriggered:
print("That response was blocked by a content policy.")How It Works#
- Each string guardrail is automatically turned into a classifier — you never write moderation logic yourself.
- The classifier runs on a fast, low-cost model from the same provider as your agent, so no extra API key is required.
- Input guardrails run before the model — they catch disallowed requests early, before you pay for a generation.
- Output guardrails run right before
run()returns — the last checkpoint before content reaches your users. - A tripped guardrail raises an exception instead of silently returning a filtered response, so it's never missed.
| Guardrail | Runs | Exception |
|---|---|---|
| Input | Before the model is called | InputGuardrailTripwireTriggered |
| Output | Before run() returns | OutputGuardrailTripwireTriggered |
Advanced Guardrails#
For logic a single sentence can't express, write a guardrail as a Python function with the @input_guardrail or @output_guardrail decorator instead.
from abzagent import Agent, input_guardrail, GuardrailResult
@input_guardrail
def block_math_questions(input_text: str) -> GuardrailResult:
is_math = any(op in input_text for op in ["+", "-", "*", "/", "="])
return GuardrailResult(
tripwire_triggered=is_math,
reason="This agent doesn't answer math questions.",
)
agent = Agent(
name="Support Agent",
instructions="Help customers with product questions.",
model="gemini-2.5-flash",
input_guardrails=[block_math_questions],
)agent = Agent(
...
input_guardrails=[
"Block mathematical questions.",
block_offensive_language,
],
)Next Step#
Continue to Handoffs.