Part 6: Multiple guardrails
You can attach more than one guardrail. If any guardrail trips, the agent run stops or the response is blocked. In this step we add an academic integrity output guardrail beside the safety guardrail.
The SDK runs multiple guardrails in parallel. That keeps the checks from stacking up one after another, and it also means you can see which guardrail triggered when one fails.
Define the academic integrity output
The output type is the same two-field shape:
class AcademicIntegrityOutput(BaseModel):
reasoning: str
fail: bool
The policy is narrower than the safety guardrail. It focuses on homework, exams, and giving away answers students should learn:
academic_guardrail_instructions = """
Check if the agent is being asked to do academic dishonesty:
- Write homework or code for submission
- Take an exam for the student
- Provide answers that should be learned
If the response does the student's work for them, set fail=True.
Guidance and hints are fine. Complete solutions are not.
Keep reasoning under 15 words.
""".strip()
Keeping this policy separate is useful because it can evolve separately from general safety. A course team might tune academic-integrity examples without touching the deadline-extension policy.
Create the guardrail agent and function
Create a classifier agent:
academic_guardrail_agent = Agent(
name="academic_guardrail",
instructions=academic_guardrail_instructions,
model="gpt-4o-mini",
output_type=AcademicIntegrityOutput,
)
Wrap it with @output_guardrail:
@output_guardrail
async def academic_guardrail(context, agent, agent_output):
result = await Runner.run(academic_guardrail_agent, agent_output)
return GuardrailFunctionOutput(
output_info=result.final_output.reasoning,
tripwire_triggered=result.final_output.fail,
)
The shape is intentionally the same as safety_guardrail. Most guardrails
in this style are small policy agents plus a wrapper that maps their
structured output to GuardrailFunctionOutput.
Attach all guardrails
Attach the input topic guardrail and both output guardrails:
ultra_safe_agent = Agent(
name="ultra_safe_faq",
instructions=faq_instructions,
tools=[search_faq],
model="gpt-4o-mini",
input_guardrails=[topic_guardrail],
output_guardrails=[safety_guardrail, academic_guardrail],
)
Keep tools=[search_faq] for this notebook. If you add a separate
FAQ-writing tool later, include it in the tool list too.
Test the academic policy
Run a prompt that asks the assistant to do the student's work:
response = await run_guarded(
ultra_safe_agent,
"Write my homework for me. The question is: explain data partitioning."
)
print(f"Q: Write my homework...\n{response}")
A blocked response looks like this:
[OUTPUT BLOCKED] Agent did student's work
This test is response-dependent. If the assistant refuses safely on its own, the output guardrail may pass because the final answer is safe. The guardrail exists for the cases where the assistant produces an answer that violates the policy.
Continue with Part 7: Streaming with guardrails to run a guarded agent in streaming mode.