Skip to main content

Introduction

Guardrails is a framework that validates and structures data from language models. These validations range simple checks like regex matching to more complex checks like competitor analysis. Guardrails can be used with any language model.

Installation

Download Guardrails (required)

First, install Guardrails for your desired language:

Generate an API key (optional)

An API key from Guardrails Hub is optional — it enables anonymous usage metrics. It is no longer required to install validators, which now install from public PyPI.

Configure the Guardrails CLI (optional)

Configure the Guardrails CLI with the command:
The configuration process will ask whether you want to enable anonymous metrics reporting.

Install a validator from the Guardrails Hub

In order to perform any validation on LLM output with Guardrails, you will need to install an appropriate validator for your use case from the Guardrails Hub. Validators are published to public PyPI as guardrails-ai-<name>. You can find the exact pip install command for a validator on its details page in the Guardrails Hub. For example, the Detect PII validator can be installed via:

Usage

Create a Guard with an installed validator

First, install the validator you want to use from the Guardrails Hub:
Next, you can import this validator from the guardrails_ai namespace and use it to construct a Guard.

Run multiple validators within a Guard

First, install the necessary validators from Guardrails Hub.
Then, create a Guard from the installed validators.

Structured data generation and validation

Now, let’s go through an example where we ask an LLM to generate fake pet names.
  1. Create a Pydantic BaseModel that represents the structure of the output we want.
  1. 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:
(1) Function calling: For LLMs that support function calling, we generate structured data using the function call syntax. (2) Prompt optimization: For LLMs that don’t support function calling, we add the schema of the expected output to the prompt so that the LLM can generate structured data.
This prints:

Advanced installation instructions

Install the Javascript library

The Javascript library works via an I/O bridge to run the underlying Python library. You must have Python 3.10 or greater installed on your system to use the Javascript library.

Install specific version

To install a specific version in Python, run:

Install from GitHub

Installing directly from GitHub is useful when a release has not yet been cut with the changes pushed to a branch that you need. Non-released versions may include breaking changes, and may not yet have full test coverage. We recommend using a released version whenever possible.