Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1jehuang Jcode Structured Output Validation

From Leeroopedia
Knowledge Sources
Domains LLM_Applications, Data_Validation
Last Updated 2026-10-18 00:00 GMT

Overview

Technique for constraining LLM output to a user-defined JSON Schema by appending the schema to the prompt, validating the response with a compiled validator, and sending correction prompts on validation failure.

Description

Structured output validation is a pattern for getting typed data from a language model. Instead of relying on the model to produce well-formed JSON by chance, the schema is injected into the prompt as instructions, the model's response is parsed and validated against a JSON Schema compiled with a validator (e.g., AJV), and if validation fails, a correction prompt describing the errors is sent back to the model for a retry.

The problem solved is unreliable structured generation: LLMs produce text, not typed data. When an application needs a specific JSON shape (e.g., a config object, a classification result), the model may omit required fields, produce wrong types, or add unexpected properties. Without validation and correction, the application must either accept malformed data or fail entirely.

The correction loop is bounded: after a configurable number of retries (default 2), the system throws a structured error containing all attempts and their validation errors, so the caller can inspect what went wrong.

Usage

Use this principle when you need an LLM to produce a specific JSON structure reliably. The trigger is: "the application requires typed data from a model turn, not free-text." It is appropriate for: classification, config generation, data extraction, and tool argument generation. It is not needed when free-text responses are acceptable.

Theoretical Basis

The validation-correction cycle has three phases:

  1. Prompt construction: The schema is serialized into a text instruction appended to the user prompt, telling the model to produce JSON matching the schema.
  2. Validation: The model's response text is parsed as JSON and validated against the compiled schema. The validator produces structured error messages.
  3. Correction: If validation fails, a correction prompt is built containing the schema, the previous attempt's text, and the specific validation errors. This is sent as the next turn.

Pseudo-code logic:

# Abstract algorithm (NOT real implementation)
def run_structured(model, prompt, schema, max_retries=2):
    compiled = compile_schema(schema)
    full_prompt = build_prompt(prompt, schema)

    for attempt in range(1, max_retries + 2):
        response = model.run(full_prompt)
        result = validate(response, compiled)

        if result.is_valid:
            return result.data

        full_prompt = build_correction_prompt(
            schema, previous_text=response, errors=result.errors
        )

    raise StructuredOutputError(all_attempts)

Related Pages

Implemented By

Uses Heuristics

Page Connections

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