Your first Guard
Lesson 1's rules were two if statements in a list. Guardrails calls a single rule a validator, and a set of rules wrapped around one value a Guard.
Installing it
pip install guardrails-ai guardrails-ai-valid-lengthTwo packages, and the second one matters. Until version 0.11 validators came from a private registry, installed with guardrails hub install and a token you had to sign up for. In 0.11 they became ordinary PyPI packages named guardrails-ai-<name>, installed with pip and imported from the guardrails_ai namespace. The 0.11.0 migration guide is the page that says so, and lesson 6 is the whole story.
One rule, one value
from guardrails import Guard
from guardrails_ai.valid_length import ValidLength
desk = Guard().use(ValidLength(min=1, max=40))
outcome = desk.validate("Order 8821 ships today.")
print(outcome.validation_passed)
print(outcome.validated_output)Guard() on its own holds no rules. .use() attaches a validator instance to it and hands the Guard back, so the two lines are usually written as one. validate() runs the rules against a value you already have.
ValidLength takes min and max in characters. It is one of ten validators that need no model, and lesson 6 lists the rest.
What comes back
print(repr(outcome.raw_llm_output))
print(outcome.error)
print(outcome.validation_summaries)validate() returns a ValidationOutcome, and it has the same shape whether a model was involved or not. raw_llm_output is the value that went in, which reads oddly here because no model produced it. Lesson 14 calls the same Guard with a model behind it and the name stops being odd.
validation_summaries is empty because nothing failed. It only ever holds the failures, which is why it is the first thing to print when something goes wrong.
guardrails-ai 0.11.0. Where the published docs and the installed library disagree, this course follows the library and says so on the page. There are several of those, and the next lesson is one.- Change
max=40tomax=10and run it. Read the traceback carefully, then read lesson 3. - Print
outcome.call_id. It is a different number each run, which is why no lesson in this course prints it. - Attach the validator with
Guard().use(validators=[ValidLength(min=1, max=40)])instead. Same Guard, different spelling, and lesson 7 explains when you need it.
You understood something today that you didn't yesterday.