# My Favorite OpenAI Agents SDK Feature (And The Most Understated!)

In our [previous tutorial, we built a restaurant customer support chatbot](https://newsletter.adaptiveengineer.com/p/building-a-multi-agent-system-with) using OpenAI's Agents SDK. In this follow-up, we’ll explore **guardrails**—a critical feature that enhances AI chatbot safety and reliability.

### What Are Guardrails in AI Agents?

Guardrails act as a **safety net** for AI agents, ensuring they operate within predefined boundaries and preventing misuse.

They work alongside agents, validating user inputs and outputs to safeguard against errors and inappropriate responses.

There are two types of guardrails:

* **Input Guardrails**: Validate user inputs before processing.
    
* **Output Guardrails**: Ensure the final response is appropriate before delivering it to the user.
    

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8fe4d90-98ff-4bab-845c-3606f61490e9_2665x1120.png align="left")

Let’s see them in action!

## Input Guardrails

* These validate initial user inputs before passing them to expensive models.
    
* They operate in three steps: receiving input, running validation functions, and triggering errors if misuse is detected.
    

Input guardrails are mechanisms put in place to validate, sanitize, and preprocess user inputs before they reach an AI model. These safeguards help in preventing:

* Malicious injections (e.g., prompt injection attacks)
    
* Profanity, hate speech, and harmful language
    
* Unstructured or irrelevant input that reduces model efficiency
    
* Bias amplification
    

By implementing input guardrails, developers can ensure that AI models receive well-structured and appropriate input, leading to better and safer outputs.

### **Why Are Input Guardrails Important?**

1. **Security**: Prevents prompt injections, SQL injections, and adversarial attacks.
    
2. **Quality Assurance**: Filters out irrelevant or poorly structured queries.
    
3. **Bias Mitigation**: Helps remove explicit bias in prompts.
    
4. **User Experience**: Ensures clear and understandable input for meaningful responses.
    
5. **Compliance**: Adheres to ethical AI principles and regulatory requirements.
    

### **How to Implement Input Guardrails**

Create the guardrail agent as below. Use the @input\_guardrail decorator for the guardrail method.

More here in the [documentation](https://openai.github.io/openai-agents-python/guardrails/).

Input guardrails run in 3 steps:

1. First, the guardrail receives the same input passed to the agent.
    
2. Next, the guardrail function runs to produce a `GuardrailFunctionOutput`, which is then wrapped in an `InputGuardrailResult`
    
3. Finally, we check if `.tripwire_triggered` is true. If true, an `InputGuardrailTripwireTriggered` exception is raised, so you can appropriately respond to the user or handle the exception.
    

---

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43849546-663f-4fd3-9c96-a6b5ee456070_1687x1115.png align="left")

Pass the guardrail as an argument to the triage agent.

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065268de-2a29-4489-98cf-6622ce6f4821_1222x307.png align="left")

Handle the `InputGuardrailTripwireTriggered exception.`

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd304c0ec-edd5-4bf6-b425-c66c83cecf1f_1897x1122.png align="left")

Exception raised when the input is “Why is my order delayed? You guys are pathetic“

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f088748-229c-4d11-ab0c-14cc4fc6b076_3140x150.png align="left")

## Output Guardrails

* These validate the final outputs generated by agents before they are delivered to users.
    
* They operate similarly to input guardrails but focus on the output stage to ensure accuracy and safety.
    

Output guardrails run in 3 steps:

1. First, the guardrail receives the same input passed to the agent.
    
2. Next, the guardrail function runs to produce a `GuardrailFunctionOutput`, which is then wrapped in an `OutputGuardrailResult`
    
3. Finally, we check if `.tripwire_triggered` is true. If true, an `OutputGuardrailTripwireTriggered` exception is raised, so you can appropriately respond to the user or handle the exception.
    

In the example below - we want to check for the “card” in the response of the order\_agent and raise an exception accordingly.

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97ddf2a-3011-4d0b-9ded-8c254ee07e0f_1575x855.png align="left")

Add the output guardrail as the argument to the final agent in the workflow.

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4910aa32-8630-4118-a845-1e03d332b916_2905x325.png align="left")

We simulated the response of the order\_agent for order 12346 to contain the word “card” and this is how the exception is caught.

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff197c0bb-2593-4893-b0bc-02c4dfc5fb75_1780x80.png align="left")

## Code

[https://github.com/zahere-dev/openai-agents-sdk-tutorial](https://github.com/zahere-dev/openai-agents-sdk-tutorial)

## **Conclusion:**

* Guardrails are vital components of AI agent systems, ensuring they operate safely and efficiently.
    
* By implementing guardrails, developers can enhance user trust and prevent misuse scenarios effectively.
