New Tool Radar

AI Agents & Automation

How to Add an Input Guardrail to an OpenAI Agents SDK Agent That Blocks Homework Questions

Add an input guardrail to an OpenAI Agents SDK agent so off-topic homework questions are stopped before the main agent spends anything.

Schematic diagram with three steps: Write the checker agent, Add the guardrail, then Test a blocked question.

Guardrails are checks on inputs and outputs, and an input guardrail runs before the main agent spends anything. The SDK documentation uses a homework detector as its example: a small checker agent answers a yes-or-no question, and the guardrail trips when the answer is yes.

Before you start

You need Python with the openai-agents package installed, using pip install openai-agents, and an OpenAI API key. Decide the one topic to block first: a guardrail that blocks everything is a guardrail that blocks the real work too.

Write the checker agent and the guardrail

Create a small checker agent with a structured output type that answers whether the input is a homework question. Attach it to the main agent as an input guardrail. The guardrail runs in parallel to the agent by default; set runInParallel to false if the check must finish before any model or tool call.

Test with one blocked question and one allowed question

Run the agent with a homework question and check the run halts with an InputGuardrailTripwireTriggered error. Then run it with a real question and check it answers normally. If the real question is blocked, the checker instructions are too broad.

Worked example: Guardrail test for a fictional support agent

Maple Support is a fictional support desk. Its agent answers support questions and blocks homework questions. The table shows the test.

Question Blocked? Why
How do I reset my password? No A support question
Solve this maths homework for me Yes A homework question
Where is my order? No A support question

Expected result (UNTESTED): the homework question halts with a guardrail tripwire error, and the two support questions are answered. We checked the OpenAI Agents SDK guides, which describe the homework detector guardrail and the tripwire error, but we did not run the agent on this machine.

Limits

A guardrail is a small agent, so it can be wrong in both directions: it can block a real question and miss a homework question. Test it with ten real questions before you trust it, and keep the checker instructions to one topic.

Sources

See all