# Guardrails AI

> Validate LLM inputs and outputs with composable guards in Python

- Category: Evaluation & Production Ops / Guardrails & Security
- Language: Python
- Repository: https://github.com/guardrails-ai/guardrails
- Website: https://www.guardrailsai.com
- Canonical page: https://ai-tldr.dev/tools/guardrails-ai/

## In short

Guardrails AI is a Python framework for building reliable LLM applications. It runs Input and Output Guards around your model calls to detect, measure, and reduce specific risks, and it can also force model output into a defined structure. It sits in the Guardrails & Security category of the Evaluation & Production Ops tools and is open-source, written in Python.

## Overview

Guardrails AI is a Python framework for building reliable LLM applications. It runs Input and Output Guards around your model calls to detect, measure, and reduce specific risks, and it can also force model output into a defined structure.

It is aimed at developers who already call an LLM in Python and want a checkpoint before a response reaches a user. You pull pre-built validators from the Guardrails Hub, combine them into a Guard, and let that Guard validate text or generate structured data.

As a guardrail framework, it sits between your application code and the model. Validators cover things like regex matching, competitor mentions, and toxic language, and each Guard decides what to do when a check fails.

## What it does

- Pull pre-built validators (regex match, competitor check, toxic language, and more) from the Guardrails Hub
- Combine multiple validators into a single Input or Output Guard
- Configurable failure handling per validator via OnFailAction (for example, raise an exception)
- Generate structured data from LLMs using a Pydantic BaseModel with Guard.for_pydantic
- Works with proprietary and open-source LLMs, using function calling or prompt-based schema injection
- Run Guardrails as a standalone Flask service with a REST API via guardrails start

## Getting started

Install the package, configure the Hub CLI, then install a validator and build a Guard.

1. Install and configure

   Install the package from PyPI and run the Hub configuration command.

```bash
pip install guardrails-ai
guardrails configure
```

2. Install a validator from the Hub

   Pull a validator, such as regex match, from the Guardrails Hub.

```bash
guardrails hub install hub://guardrails/regex_match
```

3. Create a Guard and validate

   Build a Guard from the installed validator and run validation. Passing text returns normally; failing text raises an exception.

```python
from guardrails import Guard, OnFailAction
from guardrails.hub 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)
```

## When to use it

- Block or flag toxic language in model responses before they reach end users
- Enforce that an LLM does not mention named competitors in generated text
- Validate that model output matches an expected format, such as a phone number pattern
- Generate structured JSON data from an LLM that conforms to a Pydantic schema
