ESPO.AI
How It Works

See the complete system overview

Websites

Custom Next.js sites deployed in weeks, not months

CRM

GoHighLevel setup & automation

Ads

Meta + Google campaign management

Video

AI-enhanced video production

AI Agents

Lead qualification & automation

Real Estate

Agents, teams & brokerages

Home Services

HVAC, plumbing, roofing & more

Professional Practices

Law firms, medical & financial

PricingResourcesAbout
Log InBook a Strategy Call
OverviewWebsitesCRMAdsVideoAI Agents
Real EstateHome ServicesProfessional Practices
PricingResourcesAbout
Log InBook a Call
Back to Resources
VideoFebruary 1, 20268 min read

Getting Reliable JSON from LLMs: The 2026 Guide to Structured Outputs

Stop fighting with LLM outputs. Learn 7 proven techniques for guaranteed JSON schema compliance from OpenAI, Anthropic, and Google APIs.

#ai#automation#prompts
In This Post
  • The Bottom Line
  • Why This Matters
  • Provider-Native Structured Outputs
  • 7 Techniques for Constraining Output
  • Tools That Make This Practical

The Bottom Line

Getting reliable, structured data from LLMs has evolved from a frustrating prompt-engineering exercise into a solved problem—if you know the right techniques.

Without constraints, asking an LLM for JSON fails 30-70% of the time on complex schemas. With native structured outputs, you get 100% schema compliance. The difference? Understanding when to use guaranteed constraints versus best-effort approaches.


Why This Matters

Modern applications need LLMs to power APIs, populate databases, and integrate with existing systems. A JSON parsing failure at 2 AM cascades into customer-facing outages.

The core problem: LLMs generate text token-by-token based on probability distributions. They're trained to produce helpful, conversational responses—not machine-parseable data structures.

Two fundamental approaches exist:

ApproachGuaranteeBest For
Guaranteed constraints100% schema complianceProduction systems
Best-effort constraints~70-85% compliancePrototyping, models without native support

Guaranteed constraints modify token generation itself—invalid tokens are mathematically impossible. Best-effort approaches guide through prompting and hope the model complies.


Provider-Native Structured Outputs

All three major AI providers now offer built-in structured output features. Here's how they compare:

Comparison Table

FeatureOpenAIAnthropicGemini
Release dateAug 2024Nov 2025Nov 2025 (enhanced)
Union types (anyOf)NoNoYes
Recursive schemasNoNoYes
Numeric constraintsNoNoYes
Property orderingNoNoYes (Gemini 2.5+)
Streaming supportYesYesYes

OpenAI: Most Mature

OpenAI reports 100% schema compliance vs ~35% with prompting alone. Works through constrained decoding—the API masks out tokens that would violate your schema.

[object Object], openai ,[object Object], OpenAI
,[object Object], pydantic ,[object Object], BaseModel

,[object Object], ,[object Object],(,[object Object],):
    name: ,[object Object],
    date: ,[object Object],
    participants: ,[object Object],[,[object Object],]

client = OpenAI()

completion = client.beta.chat.completions.parse(
    model=,[object Object],,
    messages=[
        {,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],},
        {,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],}
    ],
    response_format=CalendarEvent
)

event = completion.choices[,[object Object],].message.parsed  ,[object Object],

Limitations: No anyOf/oneOf, no recursive schemas, no numeric constraints. All fields must be required.

Anthropic Claude: Newer but Capable

Released November 2025 as public beta. Uses compiled grammar artifacts for enforcement.

[object Object], anthropic ,[object Object], Anthropic

client = Anthropic()

response = client.beta.messages.create(
    model=,[object Object],,
    betas=[,[object Object],],
    max_tokens=,[object Object],,
    messages=[
        {,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],}
    ],
    output_format={
        ,[object Object],: ,[object Object],,
        ,[object Object],: {
            ,[object Object],: ,[object Object],,
            ,[object Object],: {
                ,[object Object],: {,[object Object],: ,[object Object],},
                ,[object Object],: {,[object Object],: ,[object Object],},
                ,[object Object],: {,[object Object],: ,[object Object],}
            },
            ,[object Object],: [,[object Object],, ,[object Object],, ,[object Object],]
        }
    }
)

Note: Requires beta header (anthropic-beta: structured-outputs-2025-11-13).

Google Gemini: Most Flexible

Most advanced JSON Schema support. Unique features: anyOf for union types, $ref for recursive schemas, minimum/maximum for numeric constraints.

[object Object], google ,[object Object], genai
,[object Object], pydantic ,[object Object], BaseModel

,[object Object], ,[object Object],(,[object Object],):
    recipe_name: ,[object Object],
    ingredients: ,[object Object],[,[object Object],]
    instructions: ,[object Object],[,[object Object],]

client = genai.Client()

response = client.models.generate_content(
    model=,[object Object],,
    contents=,[object Object],,
    config={
        ,[object Object],: ,[object Object],,
        ,[object Object],: Recipe.model_json_schema()
    }
)

7 Techniques for Constraining Output

1. JSON Mode vs Structured Outputs

Don't confuse them:

ModeGuarantees
JSON modeValid JSON syntax only
Structured outputsValid JSON AND exact schema compliance
[object Object],
response_format={,[object Object],: ,[object Object],}

,[object Object],
response_format={,[object Object],: ,[object Object],, ,[object Object],: {...}}

2. Schema Design That Works

Well-designed schemas dramatically improve reliability:

[object Object], pydantic ,[object Object], BaseModel, Field
,[object Object], typing ,[object Object], ,[object Object],, ,[object Object],
,[object Object], enum ,[object Object], Enum

,[object Object], ,[object Object],(,[object Object],, Enum):
    positive = ,[object Object],
    negative = ,[object Object],
    neutral = ,[object Object],

,[object Object], ,[object Object],(,[object Object],):
    ,[object Object],

    product_name: ,[object Object], = Field(
        description=,[object Object],
    )
    sentiment: SentimentLevel = Field(
        description=,[object Object],
    )
    rating_inferred: ,[object Object],[,[object Object],] = Field(
        default=,[object Object],,
        description=,[object Object],
    )
    key_points: ,[object Object],[,[object Object],] = Field(
        description=,[object Object],,
        max_length=,[object Object],
    )

Key principles:

  • Use descriptive field names
  • Add description attributes to clarify ambiguous fields
  • Use enums or Literal types to constrain categories
  • Keep nesting shallow—deeply nested schemas have higher failure rates

3. Regex Constraints

For simple patterns like emails, dates, classifications:

[object Object], outlines

model = outlines.models.transformers(,[object Object],)

,[object Object],
classifier = outlines.generate.regex(model, ,[object Object],)
sentiment = classifier(,[object Object],)
,[object Object],

4. Grammar-Based Constraints

For nested structures, recursion, and code generation:

[object Object],
grammar = ,[object Object],

5. Few-Shot Examples

When you can't use constrained decoding:

Extract product information as JSON.

Example 1:
Input: "iPhone 15 Pro - $999, 256GB storage, Space Black"
Output: {"name": "iPhone 15 Pro", "price": 999, "storage": "256GB", "color": "Space Black"}

Example 2:
Input: "Samsung Galaxy S24 Ultra priced at $1199 with 512GB"
Output: {"name": "Samsung Galaxy S24 Ultra", "price": 1199, "storage": "512GB", "color": null}

Now extract:
Input: "OnePlus 12 - 256GB Flowy Emerald edition for $799"
Output:

Best practices: Use 2-5 examples. Cover edge cases. Place the most important example last.

6. Explicit Format Instructions

Clear, specific instructions significantly improve compliance:

system_prompt = ,[object Object],

7. Why Negative Constraints Often Fail

"Don't do X" instructions can make unwanted behavior more likely (the "pink elephant problem"):

[object Object],
prompt = ,[object Object],

,[object Object],
prompt = ,[object Object],

Tools That Make This Practical

ToolBest ForMonthly Downloads
InstructorAPI models, multi-provider3M+
OutlinesLocal models, guaranteed compliance-
LangChainWhen already in LangChain ecosystem-
GuidanceToken-level control, research-

Instructor Example

[object Object], instructor
,[object Object], pydantic ,[object Object], BaseModel

,[object Object], ,[object Object],(,[object Object],):
    name: ,[object Object],
    age: ,[object Object],

,[object Object],
client = instructor.from_provider(,[object Object],)
user = client.chat.completions.create(
    response_model=User,
    max_retries=,[object Object],,
    messages=[{,[object Object],: ,[object Object],, ,[object Object],: ,[object Object],}]
)

Outlines Example

[object Object], outlines
,[object Object], pydantic ,[object Object], BaseModel

,[object Object], ,[object Object],(,[object Object],):
    name: ,[object Object],
    age: ,[object Object],
    armor: ,[object Object],

model = outlines.models.transformers(,[object Object],)
generator = outlines.generate.json(model, Character)

character = generator(,[object Object],)
,[object Object],

When Things Go Wrong

The Truncation Trap

The model hits max_tokens before completing JSON:

[object Object], ,[object Object],(,[object Object],):
    response = client.chat.completions.create(...)

    ,[object Object],
    ,[object Object], response.choices[,[object Object],].finish_reason == ,[object Object],:
        ,[object Object], ValueError(,[object Object],)

    ,[object Object], response.choices[,[object Object],].message.content

Schema Compliance ≠ Content Accuracy

Structured outputs guarantee format, not truth. Always validate content:

[object Object], pydantic ,[object Object], field_validator

,[object Object], ,[object Object],(,[object Object],):
    company_name: ,[object Object],
    founded_year: ,[object Object],

,[object Object],
    ,[object Object], ,[object Object],(,[object Object],):
        ,[object Object], v < ,[object Object], ,[object Object], v > ,[object Object],:
            ,[object Object], ValueError(,[object Object],)
        ,[object Object], v

Graceful Degradation Pattern

[object Object], ,[object Object],(,[object Object],) -> ,[object Object],:
    ,[object Object],
    ,[object Object],:
        ,[object Object], call_with_strict_schema(text).model_dump()
    ,[object Object], ValidationError:
        ,[object Object],

    ,[object Object],
    ,[object Object],:
        ,[object Object], call_with_relaxed_schema(text).model_dump()
    ,[object Object], ValidationError:
        ,[object Object],

    ,[object Object],
    ,[object Object],:
        ,[object Object], json.loads(call_with_json_mode(text))
    ,[object Object], json.JSONDecodeError:
        ,[object Object],

    ,[object Object],
    ,[object Object], {,[object Object],: text, ,[object Object],: ,[object Object],}

Key Takeaways

For API-based applications:

  • Use native structured outputs (OpenAI, Anthropic, or Gemini)
  • Combine with Instructor for automatic retries
  • Always check finish_reason for truncation

For local model deployment:

  • Use Outlines for guaranteed compliance
  • Consider grammar constraints for code generation

For all cases:

  • Design schemas simply with clear field descriptions
  • Implement retry logic with exponential backoff
  • Build graceful degradation paths
  • Test edge cases before production

The tools exist. "The LLM returned invalid JSON" is no longer an acceptable production failure mode.


Building AI-powered systems and want help with structured outputs? Book a strategy call and let's discuss your architecture.

In This Post

  • The Bottom Line
  • Why This Matters
  • Provider-Native Structured Outputs
  • 7 Techniques for Constraining Output
  • Tools That Make This Practical

Share This

Matthew Esposito

Matthew Esposito

Founder of ESPO.AI. I help small businesses build marketing systems they actually own.

Follow on YouTube

Keep Learning

More resources you might find useful

Video
Jan 21, 2026

AI for Real Estate Marketing: What Actually Works in 2025-2026

The specific tools, workflows, and strategies top producers use to close 3x more deals. Beyond generic advice—real pricing, case studies, and implementation details.

aireal-estatemarketing
The Complete Guide to Claude Projects in 2026
Video
Feb 1, 2026

The Complete Guide to Claude Projects in 2026

Master Claude Projects with 200K token context, automatic RAG expansion, cross-conversation memory, and the new Cowork integration.

aiclaudeproductivity
How to Use Personas Effectively in AI Prompts
Video
Feb 1, 2026

How to Use Personas Effectively in AI Prompts

Research shows personas can boost AI reasoning by 10-60%—but only when done correctly. Learn the formats, frameworks, and mistakes to avoid.

aipromptssmall-business
New videos weekly

Want More AI Tips?

Subscribe to get practical AI tutorials, prompt packs, and business automation strategies.

Subscribe on YouTubeBrowse All Resources
ESPO.AI

Your entire marketing system. Deployed in weeks, not months.

Services

  • Websites
  • CRM
  • Ads
  • Video
  • AI Agents

Company

  • How It Works
  • About
  • Pricing
  • Results
  • FAQ
  • Book a Call

Industries

  • Real Estate
  • Professional Practices
  • Home Services

Legal

  • Book a Call
  • Privacy
  • Terms

© 2026 Espo.ai. All rights reserved.