Output validation checks generated or returned data against defined conditions before it is accepted or used. In agent workflows, checking tool results against expected schemas helps prevent one bad result from steering later actions. section 3.2.4 section 4.4.1
A guardrail can perform these checks with code or an agent. Simple rules, such as string comparisons or length checks, can mark invalid data by triggering a tripwire; more complex validation logic is also possible. section 4.4.1
A simple guardrail
An input guardrail checks whether a request contains a forbidden research title and triggers a tripwire if it does. The same pattern can guard an agent's output. section 4.4.1
Listing 4.8 07_input_output_guardrails.py
class ResearchOutputModel(BaseModel): #1
"""Output model for the research agent."""
research_plan: str
"""The final research plan as text."""
research_plan_file: str
"""The path to the research plan file."""
@input_guardrail #2
async def research_guardrail( #2
ctx: RunContextWrapper[None],
agent: Agent,
input: str | list[TResponseInputItem]
) -> GuardrailFunctionOutput:
forbidden_research = "The Hitchhiker's Guide to the Galaxy"
if forbidden_research in input:
research_forbidden = True
else:
research_forbidden = False
return GuardrailFunctionOutput(
output_info=f"user asked: {input}",
tripwire_triggered=research_forbidden, #3
)
@output_guardrail #4
async def research_output_guardrail( #4
ctx: RunContextWrapper,
agent: Agent,
output: ResearchOutputModel #1
) -> GuardrailFunctionOutput:
if len(output.research_plan) < 100:
insufficient_research = True
else:
insufficient_research = False
return GuardrailFunctionOutput(
output_info="research plan length: {len(output.research_plan)}",
tripwire_triggered=insufficient_research, #3
)
# code omitted…
agent = Agent(
name="Assistant",
instructions=instructions,
mcp_servers=[research_srv, thinking_srv, fs_srv],
output_type=ResearchOutputModel, #1
input_guardrails=[research_guardrail], #2
output_guardrails=[research_output_guardrail], #4
)
goal = """
Produce a research plan to find the book 'The Hitchhiker's Guide to the Galaxy'
"""
try:
print("Running...", goal)
result = await Runner.run(agent, goal)
print(result.final_output)
except InputGuardrailTripwireTriggered as input_tripped:
print(f"""
Input guardrail tripwire triggered: #1
{input_tripped.guardrail_result.output.output_info}
""")
except OutputGuardrailTripwireTriggered as output_tripped:
print(f"""
Output guardrail tripwire triggered: #2
{output_tripped.guardrail_result.output.output_info}
""")
Inspect an Agents SDK listing that defines input and output guardrails and an output model. section 4.4.1





























