Skip to content

Strings

Guardrails is currently only available in python, but we do want to expand to other languages as demand becomes apparent.

As you've seen in other concepts pages, RAIL xml and pydantic are used to model Guards.

Guardrails additionally supports single-field, string outputs. This is useful for running validations on simple, single-value outputs.

The specification for writing up validators and including failure modes is identical to what we see in pydantic.

Simple Example

In this simple example, we initialize a guard that validates a string output and ensures that it is within 10-20 characters. We first print the final, validated response, then we print the history of the request to see everything the Guard has done to ensure that the name fits parameters.

from guardrails import Guard
from guardrails.validators import ValidLength
import openai
from rich import print

guard = Guard.from_string(
    validators=[ValidLength(min=10, max=20, on_fail='reask')],
    description="Puppy name",
    prompt="Generate a puppy name"
)

raw_llm_output, validated_llm_response = guard(openai.Completion.create)
print(validated_llm_response)
print(guard.state.most_recent_call.tree)
Async event loop found, but guard was invoked synchronously.For validator parallelization, please call `validate_async` instead.
Async event loop found, but guard was invoked synchronously.For validator parallelization, please call `validate_async` instead.

Fifi the Puppy
Logs
├── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮
│   │ ╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮ │
│   │ │ Generate a puppy name                                                                                   │ │
│   │ │                                                                                                         │ │
│   │ │ String Output:                                                                                          │ │
│   │ │                                                                                                         │ │
│   │ │                                                                                                         │ │
│   │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│   │ ╭──────────────────────────────────────────── Message History ────────────────────────────────────────────╮ │
│   │ │ ┏━━━━━━┳━━━━━━━━━┓                                                                                      │ │
│   │ │ ┃ Role  Content ┃                                                                                      │ │
│   │ │ ┡━━━━━━╇━━━━━━━━━┩                                                                                      │ │
│   │ │ └──────┴─────────┘                                                                                      │ │
│   │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│   │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮ │
│   │ │ Fuzzy                                                                                                   │ │
│   │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│   │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮ │
│   │ │ FieldReAsk(                                                                                             │ │
│   │ │     incorrect_value='Fuzzy',                                                                            │ │
│   │ │     fail_results=[                                                                                      │ │
│   │ │         FailResult(                                                                                     │ │
│   │ │             outcome='fail',                                                                             │ │
│   │ │             metadata=None,                                                                              │ │
│   │ │             error_message='Value has length less than 10. Please return a longer output, that is        │ │
│   │ │ shorter than 20 characters.',                                                                           │ │
│   │ │             fix_value='Fuzzyyyyyy'                                                                      │ │
│   │ │         )                                                                                               │ │
│   │ │     ],                                                                                                  │ │
│   │ │     path=None                                                                                           │ │
│   │ │ )                                                                                                       │ │
│   │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│   ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
└── ╭────────────────────────────────────────────────── Step 1 ───────────────────────────────────────────────────╮
    │ ╭──────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────╮ │
    │ │ This was a previous response you generated:                                                             │ │
    │ │                                                                                                         │ │
    │ │ ======                                                                                                  │ │
    │ │ Fuzzy                                                                                                   │ │
    │ │ ======                                                                                                  │ │
    │ │                                                                                                         │ │
    │ │ Generate a new response that corrects your old response such that the following issues are fixed        │ │
    │ │ - Value has length less than 10. Please return a longer output, that is shorter than 20 characters.     │ │
    │ │                                                                                                         │ │
    │ │ Here's a description of what I want you to generate: Puppy name                                         │ │
    │ │                                                                                                         │ │
    │ │ Your generated response should satisfy the following properties:                                        │ │
    │ │ - length: min=10 max=20                                                                                 │ │
    │ │                                                                                                         │ │
    │ │ Don't talk; just go.                                                                                    │ │
    │ │                                                                                                         │ │
    │ │                                                                                                         │ │
    │ │ String Output:                                                                                          │ │
    │ │                                                                                                         │ │
    │ │                                                                                                         │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭───────────────────────────────────────────── Instructions ──────────────────────────────────────────────╮ │
    │ │ You are a helpful assistant.                                                                            │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭──────────────────────────────────────────── Message History ────────────────────────────────────────────╮ │
    │ │ No message history.                                                                                     │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮ │
    │ │ Fifi the Puppy                                                                                          │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │ ╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮ │
    │ │ 'Fifi the Puppy'                                                                                        │ │
    │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯