Clark Farming CompanySoftware Foundry

AI / Agent Reference · Reference

Structured Output Generation

Techniques for constraining Large Language Model output to specific formats — JSON, XML, CSV, or custom schemas — enabling reliable parsing and downstream processing. Critical for production applications where output must be machine-readable. Most providers offer a "JSON mode" that constrains output to valid JSON. OpenAI: response_format: { "type": "json_object" } Anthropic: System prompt: "Always respond in valid JS…

draftneeds-review0 source links3 resolved links
wiki/wiki/ai-ml/structured-output-generation.md

Answer

Techniques for constraining Large Language Model output to specific formats — JSON, XML, CSV, or custom schemas — enabling reliable parsing and downstream processing. Critical for production applications where output must be machine-readable. Most providers offer a "JSON mode" that constrains output to valid JSON. OpenAI: response_format: { "type": "json_object" } Anthropic: System prompt: "Always respond in valid JS…

Auto-generated neutral summary from the source page — needs human review before trusted use.

Evidence & Source Cards

No explicit artifact, library, or external source links found in this sample slice. Evidence state remains needs-review.

Source Excerpt

Techniques for constraining Large Language Model output to specific formats — JSON, XML, CSV, or custom schemas — enabling reliable parsing and downstream processing. Critical for production applications where output must be machine-readable.

Approaches

JSON Mode (Provider-Native)

Most providers offer a "JSON mode" that constrains output to valid JSON.

OpenAI: response_format: { "type": "json_object" }

Anthropic: System prompt: "Always respond in valid JSON"

Gemini: response_mime_type: "application/json"

Limitations: Ensures valid JSON but not valid schema. Model may produce JSON with wrong structure.

JSON Schema (Provider-Native)

Providers increasingly support JSON schema validation at the model level.

OpenAI strict mode: strict: true with JSON schema. Model is trained to produce schema-valid output.

Anthropic: Tool-use format inherently produces structured output.

Advantage: Model-level enforcement, highest reliability.

Regex Constraints

Use regex patterns to constrain output format.

Libraries: guidance, outlines, lm-format-enforcer

from outlines import generate, models

model = models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
generator = generate.regex(model, r"\d{4}-\d{2}-\d{2}")  # Date format
output = generator("What is today's date?")

Advantage: Precise control over output format.

Limitation: Complex schemas become unwieldy regex.

Grammar-Based Constraints

Use formal grammars (EBNF, CFG) to define output structure.

Libraries: guidance, xgrammar

from guidance import models, gen

grammar = """
json_object ::= '{' key_value_pairs '}'
key_value_pairs ::= key_value (',' key_value)*
key_value ::= '"' string '"' ':' value
value ::= '"' string '"' | number | 'true' | 'false' | 'null'
"""

Advantage: Expressive, handles complex nested structures.

Limitation: Requires grammar expertise.

Pydantic Validation (Post-Generation)

Generate free-text, then validate with Pydantic.

from pydantic import BaseModel, Field

class WeatherResponse(BaseModel):
    location: str = Field(description="City and state")
    temperature: float = Field(description="Temperature in Fahrenheit")
    conditions: str = Field(description="Weather conditions")

# Parse LLM output
import json
response = json.loads(llm_output)
validated = WeatherResponse(**response)

Advantage: Leverages existing validation infrastructure.

Limitation: May require retry loops for invalid output.

Library Comparison

LibraryApproachModelsPerformanceMaturity
OutlinesRegex, grammar, JSON schemaTransformers, vLLMFast (token-level)High
GuidanceGrammar, regexLM Studio, transformersMediumHigh
xgrammarXGRAMMAR formatvLLM, transformersFastMedium
InstructorPydantic wrapperAny (retry loop)Slow (retry)High
JsonformerJSON schemaTransformersFastMedium

Best Practices

Prompt Design

Schema Design

Error Handling

Reliability Techniques

Temperature and Top-P

Max Tokens

Validation Pipeline

Large Language Model output → JSON parse → Schema validate → Pydantic model → Success/Fail
                                    ↓ (fail)
                              Retry with error feedback (max 3 retries)
                                    ↓ (fail)
                              Fallback to free-text parsing

Common Use Cases

Data Extraction

{
  "name": "John Smith",
  "email": "john@example.com",
  "phone": "555-123-4567",
  "company": "Acme Corp"
}

Classification

{
  "sentiment": "positive",
  "confidence": 0.92,
  "topics": ["product", "shipping"],
  "urgency": "low"
}

Code Generation

{
  "language": "python",
  "code": "def hello():\n    print('Hello, world!')",
  "explanation": "Simple greeting function"
}

Search Queries

{
  "query": "industrial automation PLC programming",
  "filters": {
    "date_range": "2024-2026",
    "type": "technical_documentation"
  }
}

Related

Relationships

Outbound links

Referenced by

Tags

ai-mlstructured-outputjsonschemavalidationparsing