# OpenAI Guardrails

> Drop-in safety and moderation checks for your OpenAI client

- Category: Evaluation & Production Ops / Guardrails & Security
- Language: Python
- Licence: MIT
- Repository: https://github.com/openai/openai-guardrails-python
- Website: https://guardrails.openai.com
- Canonical page: https://ai-tldr.dev/tools/openai-guardrails/

## In short

OpenAI Guardrails (Python) is a package that adds configurable safety and compliance checks to LLM applications. It works as a drop-in wrapper around OpenAI's Python client, so you can validate and moderate model inputs and outputs without rewriting your existing calls. It sits in the Guardrails & Security category of the Evaluation & Production Ops tools and is open-source, written in Python and licensed MIT.

## Overview

OpenAI Guardrails (Python) is a package that adds configurable safety and compliance checks to LLM applications. It works as a drop-in wrapper around OpenAI's Python client, so you can validate and moderate model inputs and outputs without rewriting your existing calls.

It is aimed at developers building chatbots, agents, and other LLM features who need to catch problems like unsafe content, PII, jailbreak attempts, or off-topic responses. You point it at a config file that defines which checks to run, and a failed check raises a tripwire exception you can handle.

As a guardrail framework, it sits between your application and the model. It also integrates with the OpenAI Agents SDK and ships an evaluation framework so you can measure how your guardrails perform on labeled datasets.

## What it does

- Drop-in GuardrailsOpenAI client that replaces the standard OpenAI client and works with both Chat Completions and the Responses API
- Built-in checks including Moderation, Contains PII, URL Filter, Jailbreak, NSFW Text, Off Topic Prompts, and Hallucination Detection
- Tripwire model: a triggered guardrail raises GuardrailTripwireTriggered so you can catch and handle violations
- GuardrailAgent integration for the OpenAI Agents SDK
- Evaluation framework to benchmark guardrails on JSONL datasets, including model comparison and ROC curves
- Configuration driven by a JSON config file generated via the guided setup at guardrails.openai.com

## Getting started

Install the package, create a guardrail config, then swap in the GuardrailsOpenAI client. Most users generate their config with the guided setup at guardrails.openai.com.

1. Install the package

   Install openai-guardrails from PyPI.

```bash
pip install openai-guardrails
```

2. Use the drop-in client

   Replace the standard OpenAI client with GuardrailsOpenAI and pass your config file. A triggered guardrail raises GuardrailTripwireTriggered.

```python
from pathlib import Path
from guardrails import GuardrailsOpenAI, GuardrailTripwireTriggered

# Use GuardrailsOpenAI instead of OpenAI
client = GuardrailsOpenAI(config=Path("guardrail_config.json"))

try:
    chat = client.chat.completions.create(
        model="gpt-5",
        messages=[{"role": "user", "content": "Hello world"}],
    )
    print(chat.choices[0].message.content)
except GuardrailTripwireTriggered as e:
    print(f"Guardrail triggered: {e}")
```

3. Integrate with the Agents SDK (optional)

   Use GuardrailAgent to attach guardrails to an OpenAI Agents SDK agent.

```python
from pathlib import Path
from guardrails import GuardrailAgent

agent = GuardrailAgent(
    config=Path("guardrails_config.json"),
    name="Customer support agent",
    instructions="You are a customer support agent. You help customers with their questions.",
)
```

## When to use it

- Add moderation and PII detection to a customer-facing chatbot without rewriting existing OpenAI calls
- Block jailbreak attempts and keep an agent's responses within its intended business scope
- Validate model outputs for NSFW or off-topic content before they reach end users
- Benchmark and compare guardrail configurations on labeled datasets before shipping
