Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion examples/tracing/openai/openai_agents_tracing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,95 @@
"response\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 11. Add an input guardrail\n",
"\n",
"Guardrails run *before* the main agent and can short-circuit a request. Here we add a relevance guardrail that blocks questions unrelated to airline customer service. A small classifier agent decides whether the message is on-topic; if not, the guardrail trips its tripwire and the SDK raises `InputGuardrailTripwireTriggered`.\n",
"\n",
"In Openlayer, the guardrail appears as a dedicated `GUARDRAIL` step whose `metadata.triggered` reflects the tripwire result."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"from agents import (\n",
" GuardrailFunctionOutput,\n",
" InputGuardrailTripwireTriggered,\n",
" input_guardrail,\n",
")\n",
"\n",
"\n",
"class RelevanceCheck(BaseModel):\n",
" \"\"\"Output schema for the relevance classifier.\"\"\"\n",
" is_relevant: bool\n",
" reasoning: str\n",
"\n",
"\n",
"relevance_agent = Agent(\n",
" name=\"Relevance guardrail\",\n",
" instructions=(\n",
" \"Decide whether the user's message is related to airline customer \"\n",
" \"service (flights, seats, baggage, check-in, wifi, etc.).\"\n",
" ),\n",
" output_type=RelevanceCheck,\n",
")\n",
"\n",
"\n",
"@input_guardrail\n",
"async def relevance_guardrail(context, agent, user_input): # noqa: ARG001\n",
" \"\"\"Trip the tripwire when the request is off-topic.\"\"\"\n",
" check = await Runner.run(relevance_agent, user_input, context=context.context)\n",
" return GuardrailFunctionOutput(\n",
" output_info=check.final_output,\n",
" tripwire_triggered=not check.final_output.is_relevant,\n",
" )\n",
"\n",
"\n",
"# Attach the guardrail to the triage agent (the entry point of the workflow).\n",
"triage_agent.input_guardrails.append(relevance_guardrail)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Test 4: guardrail trips on an off-topic request\n",
"\n",
"The first request is unrelated to the airline and should be blocked; the second is on-topic and should pass through to the triage agent as usual."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def run_guarded(user_input: str) -> str:\n",
" \"\"\"Run the triage agent with the relevance guardrail enabled.\"\"\"\n",
" with agent_trace(\n",
" \"Customer service (guarded)\", group_id=uuid.uuid4().hex[:16]\n",
" ):\n",
" try:\n",
" result = await Runner.run(\n",
" triage_agent, user_input, context=AirlineAgentContext()\n",
" )\n",
" return f\"✅ Allowed → {result.final_output}\"\n",
" except InputGuardrailTripwireTriggered:\n",
" return \"🛑 Guardrail tripped — off-topic request blocked.\"\n",
"\n",
"\n",
"# Off-topic: should trip the guardrail\n",
"print(await run_guarded(\"Can you give me a recipe for chocolate cake?\")) # noqa: T201\n",
"\n",
"# On-topic: should pass\n",
"print(await run_guarded(\"What are the baggage restrictions?\")) # noqa: T201"
]
}
],
"metadata": {
Expand Down
Loading