Coderhouse goes global

🌍

Read more

What Are Structured Outputs and Why AI Agents Rely on JSON Schema

AI Engineering

Build and ship real applications with LLMs, agents and automation.

8 weeks · Live online · View course →

Data Analytics

Go from zero to job-ready analyst with SQL, Python and BI tools.

10 weeks · Live online · View course →

Dan Patiño

AI Strategy & Innovation at Coderhouse

Artificial Intelligence

What Are Structured Outputs and Why AI Agents Rely on JSON Schema

Structured outputs are a feature of modern LLM APIs that forces a model’s response to follow a schema you define—usually a JSON Schema—so your code always receives valid, parseable data with the fields and types it expects. Instead of asking a model nicely to “reply in JSON” and hoping, you hand the API a contract, and the response is constrained to match it.

Why it matters now: AI agents and automations pass model output directly into other systems—databases, CRMs, ticketing tools, other agents. One malformed field can break a pipeline. OpenAI, Anthropic, and Google all document schema-constrained output modes for exactly this reason (OpenAI Structured Outputs guide; Claude structured outputs docs; Gemini API structured output docs).

Prompted JSON vs JSON mode vs structured outputs

There are three levels of control, and they are often confused:

  • Prompted JSON: you describe the format in the prompt. The model usually complies, but can add commentary, drop fields, or produce invalid syntax.

  • JSON mode: the API guarantees syntactically valid JSON, but not that it matches your fields and types.

  • Structured outputs (schema-constrained): the API guarantees the response conforms to the schema you supply—required keys present, enums respected, types correct.

Anthropic’s documentation summarizes the problem well: without structured outputs, models can generate malformed JSON or invalid tool inputs that break applications, even with careful prompting.

How it works under the hood

Most implementations use constrained decoding. While the model generates text token by token, the system only allows tokens that keep the output valid according to the schema. If the schema says the next value must be one of "low", "medium", or "high", any other token is blocked. The model still decides what to say; the decoder controls shape.

The schema itself is usually written in JSON Schema, an open standard for describing JSON data. In code, many teams define it with Pydantic (Python) or Zod (TypeScript) and let the SDK convert it.

A simple example

Imagine a support agent that classifies incoming emails. Your schema might require:

  • category: one of billing, technical, account, other

  • priority: one of low, medium, high

  • summary: a string

  • needs_human: a boolean

With structured outputs, every response arrives with those four fields and valid values, so your routing code can be a few lines instead of defensive parsing with retries.

Structured outputs and AI agents

Agents depend on structure in two places. First, tool calls: when an agent decides to call a function, the arguments must match that function’s parameters. Providers now offer strict modes for tool inputs so arguments are validated against the tool schema—read more in our explainer on function calling and why AI agents depend on tool use. Second, final answers: when one agent hands work to another system or agent, a typed result is far easier to validate, store, and audit than free text.

Common use cases

  • Data extraction: pull invoice totals, dates, and vendor names from PDFs or emails into a table.

  • Classification and routing: tag tickets, leads, or reviews with fixed labels.

  • Content pipelines: generate titles, meta descriptions, and FAQs as separate fields for a CMS.

  • Evaluation: have an LLM judge return a score plus a reason in a fixed format.

  • Analytics: convert unstructured feedback into structured data you can aggregate and chart.

Limits to keep in mind

Valid is not the same as correct

A schema guarantees shape, not truth. The model can still fill a perfectly typed field with a wrong value. You still need business validation, spot checks, and guardrails for sensitive actions.

Schema support varies

Providers support different subsets of JSON Schema. Some features—certain recursive definitions, pattern constraints, or very large enums—may be unsupported or limited. Check each provider’s docs before assuming a schema will work everywhere.

Over-constraining hurts quality

Forcing a model to answer instantly in a rigid structure can reduce reasoning quality on hard tasks. A common pattern is to include a short reasoning or notes field before the final answer fields, or to separate a reasoning step from a formatting step.

Refusals and edge cases

Plan for responses where the model declines a request or cannot find the information. Include nullable fields or an explicit status value so “not found” is a valid, structured outcome.

Best practices

  • Keep schemas small and flat when possible; split complex jobs into several calls.

  • Use enums for anything your code branches on.

  • Add clear description fields—models read them as instructions.

  • Version your schemas like an API contract.

  • Log outputs and measure field-level accuracy on a test set.

FAQ

Is structured output the same as function calling?

They are related. Function calling uses a schema for tool arguments; structured outputs apply a schema to the model’s response itself. Many apps use both.

Do I still need to validate the JSON?

For syntax and types, schema-constrained modes remove most of the risk. For business rules—such as a total that must equal the sum of line items—yes, keep validating in code.

Does it work with open-source models?

Yes. Inference engines and libraries for open models support grammar- or schema-constrained decoding, though setup and feature coverage vary.

Who should learn this?

Anyone building AI automations, agents, or data extraction workflows. It is one of the simplest changes that makes LLM features production-ready.

Ready to build reliable AI agents and the data workflows they feed? Explore these Coderhouse courses:

Dan Patiño

I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.

© 2026 Coderhouse. All rights reserved.

© 2026 Coderhouse. All rights reserved.

© 2026 Coderhouse. All rights reserved.