Adding guardrails to large language models.
pip, and Guardrails is discontinuing its hosted remote inferencing. See How to Migrate for what to do. Planned cutoff: August 25, 2026.Guardrails is a Python framework that helps build reliable AI applications by performing two key functions:
Guardrails Hub is a collection of pre-built measures of specific types of risks (called 'validators'). Multiple validators can be combined together into Input and Output Guards that intercept the inputs and outputs of LLMs. Visit Guardrails Hub to see the full list of validators and their documentation.
pip install guardrails-aiDownload and configure the Guardrails Hub CLI.
pip install guardrails-ai
guardrails configureInstall a guardrail from Guardrails Hub.
pip install guardrails-ai-regex-matchCreate a Guard from the installed guardrail.
from guardrails import Guard, OnFailAction
from guardrails_ai.regex_match import RegexMatch
guard = Guard().use(
RegexMatch, regex="\(?\d{3}\)?-? *\d{3}-? *-?\d{4}", on_fail=OnFailAction.EXCEPTION
)
guard.validate("123-456-7890") # Guardrail passes
try:
guard.validate("1234-789-0000") # Guardrail fails
except Exception as e:
print(e)Output:
Validation failed for field with errors: Result must match \(?\d{3}\)?-? *\d{3}-? *-?\d{4}Run multiple guardrails within a Guard. First, install the necessary guardrails from Guardrails Hub.
pip install guardrails-ai-competitor-check guardrails-ai-toxic-languageThen, create a Guard from the installed guardrails.
…Output:
```console
Validation failed for field with errors: Found the following competitors: [['Apple']]. Please avoid naming those competitors next time, The following sentences in your response were found to be toxic:
- Shut the hell up!
```Let's go through an example where we ask an LLM to generate fake pet names. To do this, we'll create a Pydantic BaseModel that represents the structure of the output we want.
from pydantic import BaseModel, Field
class Pet(BaseModel):
pet_type: str = Field(description="Species of pet")
name: str = Field(description="a unique pet name")Now, create a Guard from the Pet class. The Guard can be used to call the LLM in a manner so that the output is formatted to the Pet class. Under the hood, this is done by either of two methods:
from guardrails import Guard
import openai
prompt = """
What kind of pet should I get and what should I name it?
${gr.complete_json_suffix_v2}
"""
guard = Guard.for_pydantic(output_class=Pet, prompt=prompt)
raw_output, validated_output, *rest = guard(
llm_api=openai.completions.create,
engine="gpt-3.5-turbo-instruct"
)
print(validated_output)This prints:
{
"pet_type": "dog",
"name": "Buddy
}Guardrails can be set up as a standalone service served by Flask with guardrails start, allowing you to interact with it via a REST API. This approach simplifies development and deployment of Guardrails-powered applications.
pip install "guardrails-ai"guardrails configureguardrails create --validators=hub://guardrails/two_words --guard-name=two-word-guardguardrails start --config=./config.py# with the guardrails client
import guardrails as gr
gr.settings.use_server = True
guard = gr.Guard(name='two-word-guard')
guard.validate('this is more than two words')
# or with the openai sdk
import openai
openai.base_url = "http://localhost:8000/guards/two-word-guard/openai/v1/"
os.environ["OPENAI_API_KEY"] = "youropenaikey"
messages = [
{
"role": "user",
"content": "tell me about an apple with 3 words exactly",
},
]
completion = openai.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
)For production deployments, we recommend using Docker with Gunicorn as the WSGI server for improved performance and scalability.
You can reach out to us on Discord or Twitter.
Yes, Guardrails can be used with proprietary and open-source LLMs. Check out this guide on how to use Guardrails with any LLM.
Yes, you can create your own validators and contribute them to Guardrails Hub. Check out this guide on how to create your own validators.
Guardrails can be used with Python and JavaScript. Check out the docs on how to use Guardrails from JavaScript. We are working on adding support for other languages. If you would like to contribute to Guardrails, please reach out to us on Discord or Twitter.
We welcome contributions to Guardrails!
Get started by checking out Github issues and check out the Contributing Guide. Feel free to open an issue, or reach out if you would like to add to the project!
1.0.0 open questions (incl. do we still need Runner?)
[bug] Guardrails CLI start command broken with guardrails-api 0.4.2
[Bug]: Installation of hub://guardrails/detect_jailbreak fails with StrictDataclassFieldValidationError in huggingface_hub
1.0.0: narrow first-class LLM support to LiteLLM, OpenAI, Anthropic (drop HuggingFace callables)
langchain-core is a core dependency but only needs to be an extra
Bug: coerce_property skips falsy values (0, False, empty string)
FIX action silently mutates output - should there be a 'quarantine' tier between LOG and FIX?
_stream_server_call drops validation_summaries; server-path outcomes are hand-copied field-by-field
Surface before/after values on ValidationSummary so callers can see what a FIX changed
1.0.0: remove RAIL spec and all XML machinery (drops lxml)