Skip to main content

OpenAI Guardrails: Python (Preview)

Add configurable safety and compliance checks to LLM applications. OpenAI Guardrails wraps the OpenAI Python client to validate inputs and outputs, and integrates with the OpenAI Agents SDK.

Configure guardrails · Documentation · Examples

OpenAI Guardrails configuration screenshot

Installation

Requires Python 3.11+. Install openai-guardrails:

pip install openai-guardrails

If your configuration uses Contains PII, also install its spaCy model during build or deployment:

python -m spacy download en_core_web_sm

For uv with spaCy 3.8, install the model wheel directly:

uv pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0-py3-none-any.whl

Contains PII validates and loads this model during client initialization; a missing or unloadable model fails configuration before any request. See the Contains PII guide for configuration options.

Quickstart

  1. Create and export a pipeline configuration with the Guardrails wizard. Save it as guardrails_config.json in your working directory.
  2. Set the OPENAI_API_KEY environment variable to your OpenAI API key.
  3. Use GuardrailsOpenAI in place of OpenAI:
from pathlib import Path

from guardrails import GuardrailsOpenAI, GuardrailTripwireTriggered

client = GuardrailsOpenAI(config=Path("guardrails_config.json"))

try:
    # Chat Completions API
    chat = client.chat.completions.create(
        model="gpt-5",
        messages=[{"role": "user", "content": "Hello world"}],
    )
    print(chat.choices[0].message.content)

    # Responses API
    response = client.responses.create(
        model="gpt-5",
        input="What are the main features of your premium plan?",
    )
    print(response.output_text)
except GuardrailTripwireTriggered:
    print("Message blocked by guardrails.")

For async and Azure clients, see the quickstart guide. Read about tripwire handling and streaming behavior before integrating those flows.

Agents SDK

Use GuardrailAgent with the OpenAI Agents SDK. This example uses the same config file and API key as the quickstart:

import asyncio
from pathlib import Path

from agents import InputGuardrailTripwireTriggered, OutputGuardrailTripwireTriggered, Runner
from agents.run import RunConfig

from guardrails import GuardrailAgent

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


async def main():
    try:
        result = await Runner.run(
            agent, "Hello, can you help me?", run_config=RunConfig(tracing_disabled=True)
        )
        print(result.final_output)
    except (InputGuardrailTripwireTriggered, OutputGuardrailTripwireTriggered):
        print("Message blocked by guardrails.")


if __name__ == "__main__":
    asyncio.run(main())

See the Agents SDK integration guide for configuration and tool guardrails.

Evaluations

Measure guardrail performance on labeled datasets using the same exported configuration. Install the evaluation dependencies first (currently required for basic evaluation as well as benchmarking):

pip install "openai-guardrails[benchmark]"
python -m guardrails.evals.guardrail_evals \
  --config-path guardrails_config.json \
  --dataset-path data.jsonl

Save one JSON object per line in data.jsonl, with labels for the guardrails in your configuration. For a configuration containing Moderation and NSFW Text:

{"id": "sample_1", "data": "Hello world", "expected_triggers": {"Moderation": false, "NSFW Text": false}}

For the programmatic API, model comparisons, benchmark dependencies, and dataset options, see the evaluation guide.

Examples and Local Development

Clone the repository and install it with the example dependencies:

git clone https://github.com/openai/openai-guardrails-python.git
cd openai-guardrails-python
pip install -e ".[examples]"

Set OPENAI_API_KEY as above. Install the spaCy model from the installation section for examples that use Contains PII, including agents_sdk.py.

python examples/basic/hello_world.py
python examples/basic/agents_sdk.py

Explore the examples:

Available Guardrails

Guardrail Checks for
Keyword Filter Configured keywords and phrases
Competitors Mentions of configured competitors
Moderation Content flagged by OpenAI's moderation API
URL Filter URLs against domain allowlists or blocklists
Secret Keys Potential API keys, secrets, and credentials
Contains PII Personally identifiable information
Hallucination Detection Claims against reference material in vector stores
Jailbreak Jailbreak attempts
Prompt Injection Detection Misaligned tool calls and tool outputs
NSFW Text Workplace-inappropriate content
Off Topic Prompts Content outside a configured topic or scope
Custom Prompt Check Violations of custom instructions

Contributing

Please share bug reports and feature requests through GitHub issues. Pull requests are limited to repository collaborators; we do not accept pull requests from non-collaborators. See CONTRIBUTING.md for the contribution policy and development guide. For security vulnerabilities, follow SECURITY.md.

License

MIT.

Disclaimers

Guardrails may use Third-Party Services such as the Presidio open-source framework, which are subject to their own terms and conditions and are not developed or verified by OpenAI.

Developers are responsible for implementing appropriate safeguards to prevent storage or misuse of sensitive or prohibited content (including but not limited to personal data, child sexual abuse material, or other illegal content). OpenAI disclaims liability for any logging or retention of such content by developers. Developers must ensure their systems comply with all applicable data protection and content safety laws, and should avoid persisting any blocked content generated or intercepted by Guardrails. Guardrails calls paid OpenAI APIs, and developers are responsible for associated charges.

Metadata

Release files for openai-guardrails 0.3.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-guardrails 0.3.6
File Size Uploaded Provenance Actions
openai_guardrails-0.3.6.tar.gz 2.7 MB With provenance Details for openai_guardrails-0.3.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openai-guardrails 0.3.6
File Interpreter ABI Platform Actions
openai_guardrails-0.3.6-py3-none-any.whl Python 3 none any Details for openai_guardrails-0.3.6-py3-none-any.whl

Total release size: 2.8 MB

Release files / openai_guardrails-0.3.6.tar.gz

Download URL openai_guardrails-0.3.6.tar.gz
Size 2.7 MB
Tags Source
SHA-256 checksum
How to use checksums
a732f09623f8e127a2b880e992fef5ebf90de60c2c605c5026e52095d1e8a153
BLAKE2b-256 checksum
How to use checksums
8f9ac03e7f4592f25475b90b27f97c9bc1e8cdafbdecb8bb185253df342f9bf0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Release files / openai_guardrails-0.3.6-py3-none-any.whl

Download URL openai_guardrails-0.3.6-py3-none-any.whl
Size 179.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de69f9457c4bd8c4f54c00f405e0bff9c88c1b29cc1c0ed2a40651927a903d95
BLAKE2b-256 checksum
How to use checksums
3d133e85ffd671384318601adbc361873290a2c1bb3d2d1ce45bafc06db9e0c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Security for openai-guardrails 0.3.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for openai_guardrails-0.3.6.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page