Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Guardrails ai Guardrails CLI Validate

From Leeroopedia
Knowledge Sources
Domains CLI, Validation
Last Updated 2026-02-14 00:00 GMT

Overview

The validate CLI command validates LLM output against a Rail specification file and writes the validated result to a JSON output file.

Description

This module provides the guardrails validate CLI command, built using the Typer framework. It consists of two components:

  • validate_llm_output function: A utility that loads a Guard from a Rail specification file, parses the given LLM output through it, and returns the validated output.
  • validate command: A Typer CLI command registered under the guardrails command group. It accepts a Rail spec path, an LLM output string, and an optional output file path. The command prints the result to stdout and writes it as JSON to the specified output file (default: .rail_output).

The command is decorated with @trace for telemetry tracking.

Usage

Use the validate CLI command to quickly test whether a given LLM output string conforms to a Rail specification. This is useful for debugging, testing validators, and verifying Rail specs outside of a full application pipeline.

Code Reference

Source Location

  • Repository: Guardrails
  • File: guardrails/cli/validate.py
  • Lines: 1-41

Signature

def validate_llm_output(rail: str, llm_output: str) -> Union[str, Dict, List, None]:
    """Validate guardrails.yml file."""

@guardrails.command()
@trace(name="guardrails-cli/validate")
def validate(
    rail: str = typer.Argument(..., help="Path to the rail spec."),
    llm_output: str = typer.Argument(..., help="String of llm output."),
    out: str = typer.Option(default=".rail_output", help="Path to the compiled output directory."),
):
    """Validate the output of an LLM against a `rail` spec."""

Import

from guardrails.cli.validate import validate, validate_llm_output

I/O Contract

validate_llm_output

Parameter Type Description
rail str Path to the Rail specification file.
llm_output str The raw LLM output string to validate.
Return Type Description
Union[str, Dict, List, None] The validated output from the Guard, or None if validation fails entirely.

validate CLI Command

Argument/Option Type Default Description
rail (argument) str required Path to the Rail specification file. Must exist and be a file.
llm_output (argument) str required The LLM output string to validate.
--out (option) str .rail_output Path to write the JSON-formatted validation result.
Side Effect Description
Prints to stdout The validated result is printed to the console.
Writes JSON file The result is written as JSON to the --out path.

Usage Examples

# CLI usage (from terminal):
# guardrails validate my_spec.rail '{"name": "Alice", "age": 30}'
# guardrails validate my_spec.rail '{"name": "Alice"}' --out result.json

# Programmatic usage:
from guardrails.cli.validate import validate_llm_output

result = validate_llm_output(
    rail="path/to/spec.rail",
    llm_output='{"name": "Alice", "age": 30}'
)
print(result)
# {"name": "Alice", "age": 30} (if valid)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment