OpenAI Guardrails (Python)
Python library that adds configurable safety and compliance checks to OpenAI-based LLM applications and the OpenAI Agents SDK.
At a Glance
About OpenAI Guardrails (Python)
OpenAI Guardrails for Python is an open-source library from OpenAI, currently in preview, that wraps the OpenAI Python client to validate inputs and outputs of LLM calls. Pipelines are configured in a web wizard, exported as a JSON file, and loaded by the client at runtime. It also integrates with the OpenAI Agents SDK.
What It Is
The library is a drop-in replacement layer for the OpenAI client. Developers use GuardrailsOpenAI in place of OpenAI, and requests through Chat Completions or Responses APIs are checked against the configured guardrails. When a check trips, a GuardrailTripwireTriggered exception is raised so the application can block the message. Async and Azure clients are covered in the docs.
Available Checks
Guardrails are grouped into input, output and agentic stages. Documented checks include keyword filtering, competitor mentions, moderation, URL allow/block lists, secret key detection, PII detection and masking, hallucination detection against vector-store reference material, jailbreak detection, prompt injection detection for misaligned tool calls, NSFW text, off-topic prompts, and custom prompt checks. The configuration site lists estimated latency for each check and the type of mechanism it uses, such as regex, moderation API, LLM, or hybrid.
Agents SDK and Evaluations
GuardrailAgent applies the same exported configuration to agents built with the OpenAI Agents SDK, raising input or output tripwire exceptions. An evaluation module measures guardrail performance on labeled JSONL datasets using the same config, with optional benchmark dependencies for model comparisons.
Setup Path
Install with pip install openai-guardrails on Python 3.11+, export a configuration from the wizard, and set OPENAI_API_KEY. The Contains PII check requires a spaCy model to be installed. Guardrails call paid OpenAI APIs, and developers are responsible for those charges and for handling sensitive content appropriately. The code is MIT licensed.
Community Discussions
Be the first to start a conversation about OpenAI Guardrails (Python)
Share your experience with OpenAI Guardrails (Python), ask questions, or help others learn from your insights.
Pricing
Open Source (MIT)
OpenAI Guardrails Python library released under the MIT License. Guardrails call paid OpenAI APIs, which are billed separately.
- MIT License
- Python 3.11+ library installed via pip
- Wraps the OpenAI Python client and integrates with the OpenAI Agents SDK
- Configurable input, output and agentic guardrails
- Calls to paid OpenAI APIs are charged separately; developers are responsible for those charges
Capabilities
Key Features
- Drop-in GuardrailsOpenAI client wrapper
- Web wizard to export guardrail pipeline configuration
- Input, output and agentic guardrail stages
- PII detection and masking
- Moderation checks
- Jailbreak detection
- Prompt injection detection
- Hallucination detection with file search
- NSFW text detection
- URL filter
- Keyword filter and competitor checks
- Secret key detection
- Off-topic and custom prompt checks
- Tripwire exception handling
- Evaluation and benchmarking on labeled datasets
- Async and Azure client support
