Output Guardrails Example
Description: Protect against sensitive data leaks by checking agent outputs before returning them to users
Difficulty: advanced
Category: guardrails
Python Code Example
from __future__ import annotations
import asyncio
import json
from pydantic import BaseModel, Field
from agents import (
Agent,
GuardrailFunctionOutput,
OutputGuardrailTripwireTriggered,
RunContextWrapper,
Runner,
output_guardrail,
)
"""
This example shows how to use output guardrails.
Output guardrails are checks that run on the final output of an agent.
They can be used to do things like:
- Check if the output contains sensitive data
- Check if the output is a valid response to the user's message
In this example, we check if the agent's response contains a phone number.
"""
# The agent's output type
class MessageOutput(BaseModel):
reasoning: str = Field(description="Thoughts on how to respond to the user's message")
response: str = Field(description="The response to the user's message")
user_name: str | None = Field(description="The name of the user who sent the message, if known")
@output_guardrail
async def sensitive_data_check(
context: RunContextWrapper, agent: Agent, output: MessageOutput
) -> GuardrailFunctionOutput:
phone_number_in_response = "650" in output.response
phone_number_in_reasoning = "650" in output.reasoning
return GuardrailFunctionOutput(
output_info={
"phone_number_in_response": phone_number_in_response,
"phone_number_in_reasoning": phone_number_in_reasoning,
},
tripwire_triggered=phone_number_in_response or phone_number_in_reasoning,
)
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant.",
output_type=MessageOutput,
output_guardrails=[sensitive_data_check],
)
async def main():
print("=== Output Guardrails Demo ===\n")
# Test 1: Normal question - should pass
print("Test 1: Normal question")
print("User: What's the capital of California?")
try:
result = await Runner.run(agent, "What's the capital of California?")
print(f"Assistant: {result.final_output.response}")
print("✓ Passed guardrail check\n")
except OutputGuardrailTripwireTriggered as e:
print(f"✗ Guardrail tripped: {e.guardrail_result.output.output_info}\n")
# Test 2: Message with phone number - should trip guardrail
print("Test 2: Message with phone number")
print("User: My phone number is 650-123-4567. Where do you think I live?")
try:
result = await Runner.run(
agent, "My phone number is 650-123-4567. Where do you think I live?"
)
print(f"✗ Guardrail didn't trip - unexpected!\n")
print(f"Output: {json.dumps(result.final_output.model_dump(), indent=2)}")
except OutputGuardrailTripwireTriggered as e:
print(f"✓ Guardrail tripped successfully!")
print(f"Info: {e.guardrail_result.output.output_info}\n")
print("=== Demo Complete ===")
if __name__ == "__main__":
asyncio.run(main())
Expected Output: Output guardrail detects and blocks sensitive phone number data
About This Example
This is an interactive Python tutorial for the OpenAI Agents SDK. The code runs directly in your browser using PyOdide (Python in WebAssembly).
Key Features:
- No installation required - runs entirely in your browser
- Interactive code editor with syntax highlighting
- Real-time execution using PyOdide
- Complete OpenAI Agents SDK functionality
- Connect with GitHub Models for AI capabilities
Related Examples:
- Input Guardrails Example - Learn how to use guardrails to check inputs and prevent unwanted behavior like doing math homework
← Back to all examples
JavaScript is required to run the interactive code editor. Please enable JavaScript in your browser for the full experience.