Using Guardrails with Agents SDK
Mend Guardrails can easily be integrated with OpenAI's Agents SDK using the MendGuardrailAgent class for a seamless drop-in replacement.
Overview
MendGuardrailAgent provides the simplest integration - just replace Agent with MendGuardrailAgent and add your config:
- Drop-in replacement for Agents SDK's
Agentclass - Automatically configures guardrails from your policy configuration
- Returns a regular
Agentinstance that works with all Agents SDK features - Prompt Injection Detection runs at the tool level - checks EACH tool call and output
- Other guardrails run at the agent level for efficiency
- Keep your existing policy configuration - no need to rewrite
- Use Agents SDK's native exception handling for guardrail violations
Quick Start with MendGuardrailAgent
The easiest way to integrate guardrails is using MendGuardrailAgent as a drop-in replacement:
import asyncio
from pathlib import Path
from agents import InputGuardrailTripwireTriggered, OutputGuardrailTripwireTriggered, Runner
from agents.run import RunConfig
from mendguardrails import MendGuardrailAgent
# Create agent with guardrails automatically configured from your config file
agent = MendGuardrailAgent(
config=Path("guardrails_config.json"),
name="Customer support agent",
instructions="You are a customer support agent. You help customers with their questions.",
)
async def main():
while True:
try:
user_input = input("Enter a message: ")
result = await Runner.run(
agent,
user_input,
run_config=RunConfig(tracing_disabled=True),
)
print(f"Assistant: {result.final_output}")
except InputGuardrailTripwireTriggered:
print("🛑 Input guardrail triggered!")
continue
except OutputGuardrailTripwireTriggered:
print("🛑 Output guardrail triggered!")
continue
if __name__ == "__main__":
asyncio.run(main())
That's it! MendGuardrailAgent automatically:
- Parses your policy configuration
- Creates the appropriate guardrail functions
- Wires them to a regular
Agentinstance - Returns the configured agent ready for use with
Runner.run()
Configuration Options
MendGuardrailAgent supports the same configuration formats as other clients:
# File path (recommended)
agent = MendGuardrailAgent(config=Path("guardrails_config.json"), ...)
# Dictionary (for dynamic configuration)
config_dict = {
"version": 1,
"input": {"version": 1, "guardrails": [...]},
"output": {"version": 1, "guardrails": [...]}
}
agent = MendGuardrailAgent(config=config_dict, ...)
# JSON string (with JsonString wrapper)
from mendguardrails import JsonString
agent = MendGuardrailAgent(config=JsonString('{"version": 1, ...}'), ...)