How do Validators work?
Each validator is a method that encodes some criteria, and checks if a given value meets that criteria.- If the value passes the criteria defined, the validator returns
PassResult. In most cases this means returning that value unchanged. In very few advanced cases, there may be a a value override (the specific validator will document this). - If the value does not pass the criteria, a
FailResultis returned. In this case, the validator applies the user-configuredon_failpolicies (see On-Fail Policies).
Runtime Metadata
Occasionally, validators need additional metadata that is only available during runtime. Metadata could be data generated during the execution of a validator (important if you’re writing your own validators), or could just be a container for runtime arguments. As an example, theExtractedSummarySentencesMatch validator accepts a filepaths property in the metadata dictionary to specify what source files to compare the summary against to ensure similarity. Unlike arguments which are specified at validator initialization, metadata is specified when calling guard.validate or guard.__call__ (this is the guard() function).
Provenance_LLM and DetectPII validators require metadata.
Custom Validators
Custom validators let you extend the ability of Guardrails with your own validation logic. Documentation for them can be found here.Installing Validators
Validators can be combined together into Input and Output Guards that intercept the inputs and outputs of LLMs. Guardrails-AI validators are published to public PyPI asguardrails-ai-<name> and can be installed with pip.
Using CLI
You can install a validator with pip. For example, the Toxic Language validator can be installed with:In Code Installs
The in-code
guardrails.install(...) SDK is deprecated. Install a validator from PyPI with pip (or uv), then import it: