Developers · OpenAI Agents SDK
Govern an OpenAI Agents SDK tool
Same shape as any other framework, with one detail specific to this SDK: guard on the inside, decorate on the outside. function_tool builds the tool's JSON schema from your function's signature, so the guard has to be invisible to it — guard_tool uses functools.wraps for exactly that reason.
01
How it works
Call guard_tool() first, then pass the RESULT to function_tool() — not the other way around. Reverse that order and the SDK rejects the tool with an additionalProperties/Pydantic-looking error that sends you to your dependency versions, where the problem isn't.
02
What to notice
- Guard on the inside, decorate on the outside — order matters
- failure_error_function turns a refusal into something the model can explain, not a crash
- input_guardrails is a different feature: it inspects what the model SAID; the gate decides an action — an amount, a merchant, a scope — signed by the agent's key against a mandate its owner issued
Example
See it in code
openai_agents_agent.py
from agents import Agent, function_tool
from metamynd_client import MetaMyndClient, guard_tool, GovernanceBlocked
client = MetaMyndClient.from_env()
# Your tool, unchanged and unaware it is governed.
def book_flight(airline: str, amount: float, route: str) -> dict:
"""Book a flight and charge the corporate card."""
...
# Guard on the INSIDE, decorate on the OUTSIDE.
governed_book_flight = guard_tool(
client, "flight-purchase", book_flight,
lambda airline, amount, route: {"amount": amount, "merchant": airline},
)
def refusal_for_the_model(_ctx, error):
# A refusal is an outcome the model should explain, not a crash.
if isinstance(error, GovernanceBlocked):
return f"Refused: {error.verdict.reason_code}. The flight was not booked."
return f"The tool failed: {error}"
agent = Agent(
name="Travel assistant",
instructions="You book flights for the team.",
tools=[function_tool(governed_book_flight,
failure_error_function=refusal_for_the_model)],
)