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.

Principle:Guardrails ai Guardrails Structured Guard Execution

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

Overview

An execution principle for running Guards configured for structured JSON output with strategy-specific parameters passed through to the LLM.

Description

Structured Guard Execution extends the basic LLM Validation Wrapping principle to handle structured JSON output. When a Guard is configured with a Pydantic schema (via Guard.for_pydantic), the execution path includes: (1) passing strategy-specific parameters (tools, response_format) to the LLM, (2) parsing the LLM's JSON response, (3) validating each field against its path-specific validators, and (4) applying field-level corrections on re-ask.

The key difference from plain text validation is that the Runner parses JSON output and validates individual fields according to the validator map built from the Pydantic schema's metadata.

Usage

Use this when executing a Guard that was created with Guard.for_pydantic(). Pass the strategy parameters (from json_function_calling_tool() or response_format_json_schema()) as kwargs to the Guard call.

Theoretical Basis

The structured execution extends the validation loop with JSON-aware processing:

# Pseudocode for structured guard execution
for attempt in range(num_reasks + 1):
    llm_output = call_llm(messages, tools=tools, response_format=response_format)
    parsed_json = parse_json(llm_output)
    for field_path, validators in validator_map.items():
        field_value = extract_field(parsed_json, field_path)
        for validator in validators:
            result = validator.validate(field_value)
            if not result.passed:
                apply_on_fail_action(result, field_path)
    if all_passed:
        return validated_dict
    messages = construct_field_level_reask(failed_fields)
return partial_result

Related Pages

Implemented By

Page Connections

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