ABZ AgentABZ AgentDocs← Site
Docs/Core Concepts/Guardrails

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.

Python
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.

Python
result = agent.run("What is 348 * 27?")

Example Output

Raised Exception
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.

Python
result = agent.run("Is your product better than BrandX?")

Example Output

Raised Exception
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:

Python
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.
GuardrailRunsException
InputBefore the model is calledInputGuardrailTripwireTriggered
OutputBefore run() returnsOutputGuardrailTripwireTriggered

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.

Python
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],
)
Mix and match
String guardrails and decorator guardrails can be combined freely in the same list — use plain sentences for simple policies and decorators when you need custom logic.
Python
agent = Agent(
    ...
    input_guardrails=[
        "Block mathematical questions.",
        block_offensive_language,
    ],
)

Next Step#

Continue to Handoffs.

← Previous
Structured Output
Next →
Handoffs