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.5

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.5
File Size Uploaded Provenance Actions
openai_guardrails-0.3.5.tar.gz 2.7 MB With provenance Details for openai_guardrails-0.3.5.tar.gz

Built distribution (wheel)

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

Total release size: 2.8 MB

Release files / openai_guardrails-0.3.5.tar.gz

Download URL openai_guardrails-0.3.5.tar.gz
Size 2.7 MB
Tags Source
SHA-256 checksum
How to use checksums
6d1ce6c7f879acde42ccddee01756b89a50b06a898fbced4ff5056d1f841179f
BLAKE2b-256 checksum
How to use checksums
30826f8ade4af52d4b72735d833264e1e5a28d2cb88c0e3ba911557746f7df79
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.5-py3-none-any.whl

Download URL openai_guardrails-0.3.5-py3-none-any.whl
Size 179.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bff481da70d25ab9018c4f7f446bf89650e09c1bae8be76e39aec6907c9ed508
BLAKE2b-256 checksum
How to use checksums
195fd2c45a5deb3ebac202788880945841a9391742c5950334469c9bb0afc46d
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.5

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.5.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