guardflow
Validators and a retry-until-valid guard for LLM output, in Rust — the core pattern behind Python's Guardrails AI, with no Rust equivalent as of 2026.
Install
Usage
use ;
use ;
let guard = new
.with
.with
.with;
let result = guard.check;
if !result.passed
Regenerate until it passes, feeding the previous failure back into your prompt:
let text = guard_with_retry.await;
Built-in validators
NotEmpty, MaxLength, MinLength, MatchesRegex, OneOf, ValidJson, NoPii (heuristic regex, not ML), JsonSchema (feature json-schema, via the jsonschema crate). Implement Validator for your own checks.
graph-flow integration
With the graphflow feature, GuardedTask<T> wraps any graph_flow::Task and re-runs it until the response passes, or gives up after max_attempts:
use GuardedTask;
let guarded = new.with_max_attempts;
Works with graphflow-stream — wrap a task in GuardedTask before passing it to spawn_task/spawn_graph.
Benchmarks
cargo bench (benches/overhead.rs):
| Scenario | Time |
|---|---|
Guard::check, 1 validator |
~27 ns |
Guard::check, 5 validators (incl. NoPii) |
~1.2 µs |
License
MIT