SuperAPI AI Directory

guardrails

Validation framework for LLM outputs.

Open website

About this tool

Guardrails is a Python framework designed to help build reliable AI applications, primarily performing two key functions: first, running Input/Output Guards to detect, quantify, and mitigate specific types of risks in applications; second, helping generate structured data from large language models (LLMs).

Key Capabilities

Guardrails provides pre-built validators through the Guardrails Hub, which are measures for specific types of risks. Multiple validators can be combined into input and output guards to intercept LLM inputs and outputs. The Guardrails Hub is a collection of validators; visit its website to view the complete list and documentation.

For structured data generation, Guardrails supports two approaches: for LLMs that support function calling, use function calling syntax to generate structured data; for LLMs that do not support function calling, add the schema of the expected output to the prompt so that the LLM generates structured data.

Usage

Creating Input and Output Guards

  1. Install and configure the Guardrails Hub CLI:pip install guardrails-ai guardrails configure
  2. Install guards from the Guardrails Hub:pip install guardrails-ai-regex-match
  3. Create a Guard from the installed guards:
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}

Running Multiple Guards in a Single Guard

First install the necessary guards:pip install guardrails-ai-competitor-check guardrails-ai-toxic-language

Then create the Guard:

from guardrails import Guard, OnFailAction
from guardrails_ai.competitor_check import CompetitorCheck
from guardrails_ai.toxic_language import ToxicLanguage

guard = Guard().use(
    CompetitorCheck(["Apple", "Microsoft", "Google"], on_fail=OnFailAction.EXCEPTION),
    ToxicLanguage(threshold=0.5, validation_method="sentence", on_fail=OnFailAction.EXCEPTION)
)

guard.validate("""An apple a day keeps a doctor away. This is good advice for keeping your health.""")  # Both the guardrails pass

try:
    guard.validate("""Shut the hell up! Apple just released a new iPhone.""")  # Both the guardrails fail
except Exception as e:
    print(e)

Output: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!

Generating Structured Data from LLMs Using Guardrails

Create a Pydantic BaseModel representing the desired output structure, then create a Guard from that class. The Guard can be used to call the LLM so that the output is formatted as that class. This is implemented under the hood through function calling or prompt optimization.

Guardrails Server

Guardrails can be set up as a standalone service served by Flask, started via guardrails start , allowing interaction through a REST API.

Installation:pip install "guardrails-ai" Configuration:guardrails configure Create configuration:guardrails create --validators=hub://guardrails/two_words --guard-name=two-word-guard Start the development server:guardrails start --config=./config.py

For production deployment, it is recommended to use Docker with Gunicorn as the WSGI server to improve performance and scalability.

Use Cases

Guardrails is suitable for AI application development that needs to ensure the reliability, safety, and structure of LLM outputs. It can be used to detect and mitigate risks (such as competitor mentions, toxic language, etc.), as well as to generate structured data from LLMs that conforms to a specific schema.

Limitations

Guardrails can be used with both proprietary and open-source LLMs. Currently, Python and JavaScript are supported, with support for other languages under development. Users can create their own validators and contribute them to the Guardrails Hub.

News and Updates

  • [July 6, 2026] Guardrails validators are being migrated to standard PyPI packages, installable directly via pip, and Guardrails is discontinuing its hosted remote inference. Planned deadline: August 25, 2026.
  • [February 12, 2025] The Guardrails Index was launched—the first benchmark comparing the performance and latency of 24 guards across 6 of the most common categories.