Table of Contents

Structured Outputs

Structured outputs refer to techniques and tools that constrain LLM generation to produce well-formed data in a specified format (JSON, XML, SQL, code, etc.) rather than free-form text. This capability is essential for integrating LLMs into software systems where downstream components require predictable, parseable responses.

Why Structured Outputs Matter

LLMs natively produce free-form text, but production applications need:

Without structured output guarantees, applications resort to brittle regex parsing, retry loops, and manual validation, all of which degrade reliability and increase latency.

Approaches to Structured Output

1. Prompting-Based

The simplest approach: instruct the model to output a specific format via the prompt.

2. Function Calling / Tool Use

Model providers offer native function-calling interfaces where the model selects and populates structured function parameters:

Function calling has become the de facto standard for structured agent interactions, serving as the backbone of tool utilization in modern agent frameworks.

3. Constrained Decoding

Intervenes during token generation to mask invalid tokens, guaranteeing schema compliance:

How it works: At each token generation step, a finite-state automaton or pushdown automaton derived from the target schema masks logits for tokens that would violate the schema. This guarantees structural validity without post-processing.

The following example uses OpenAI's native structured output with response_format to guarantee a valid JSON response matching a Pydantic schema:

# [[openai|OpenAI]] Structured Outputs with response_format and Pydantic
from [[openai|openai]] import [[openai|OpenAI]]
from pydantic import BaseModel
 
class MovieReview(BaseModel):
    title: str
    rating: float
    pros: list[str]
    cons: list[str]
    recommended: bool
 
client = [[openai|OpenAI]]()
 
completion = client.beta.chat.completions.parse(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "Extract a structured movie review."},
        {"role": "user", "content": "Dune Part Two was visually stunning with great acting. "
         "The pacing dragged in the middle. 8.5/10, highly recommended."},
    ],
    response_format=MovieReview,
)
 
review = completion.choices[0].message.parsed
print(f"{review.title}: {review.rating}/10 - Recommended: {review.recommended}")
print(f"Pros: {review.pros}")

4. Grammar-Based Generation

Libraries that compile schemas into generation grammars:

5. Post-Processing Transformation

SLOT (Structured LLM Output Transformer) (EMNLP Industry 2025): A model-agnostic approach using a lightweight fine-tuned model to transform unstructured LLM output into schema-compliant structured data.3) A fine-tuned Mistral-7B achieves 99.5% schema accuracy and 94.0% content similarity, and even compact models like Llama-3.2-1B can match larger proprietary models.

Libraries and Frameworks

==== Instructor ==== 4)

LangChain

BAML

Marvin

Magentic

LlamaIndex

Pydantic

Evaluation and Benchmarks

Best Practices

  1. Use native structured output modes when available (OpenAI strict mode, Anthropic tool use) for highest reliability
  2. Define schemas with Pydantic for type safety, validation, and automatic JSON Schema generation
  3. Include descriptions in schema fields to guide model generation with semantic context
  4. Use constrained decoding for open-weight models to guarantee compliance
  5. Implement retry with feedback: On validation failure, pass the error back to the model for correction (the approach Instructor uses)
  6. Keep schemas simple: Deeply nested or highly conditional schemas reduce reliability across all approaches
  7. Test with JSONSchemaBench or similar benchmarks to evaluate reliability before production deployment

See Also

References

2)
github.com/guidance-ai/guidance|Guidance: A Guidance Language for Controlling LLMs. Microsoft, 2023.]]
3)
github.com/dottxt-ai/outlines|Outlines: Structured Text Generation. dottxt, 2023.]]
4)
github.com/jxnl/instructor|Instructor: Structured LLM Outputs. jxnl, 2023.]]