A few years ago, getting a language model to return reliably parseable JSON meant careful prompting, hopeful regex, and a retry loop for the inevitable cases where the model added a stray sentence before the JSON or wrapped it in markdown fencing you had not asked for. That entire category of bug has largely disappeared for teams that adopted structured output modes, and it is worth understanding why, because it has quietly changed how AI features get architected.
How constrained decoding actually works
Structured output — sometimes called JSON mode, function calling, or constrained decoding depending on the provider — works by restricting the model's token generation at each step to only the tokens that would keep the output valid against a schema you define upfront. Instead of generating free text and hoping it happens to form valid JSON, the model is mechanically prevented from generating a token that would break the schema. This is a genuinely different guarantee than "the model was prompted well and usually gets it right" — it is closer to a compiler-enforced constraint than a style guideline.
Why this eliminates a whole bug category
Before structured output modes matured, a meaningful share of AI feature bugs were not about the model's actual reasoning being wrong — they were about the response being reasoning that was correct but wrapped in a format that broke a downstream parser. An extra explanatory sentence, a markdown code fence the parser did not expect, a missing comma or an extra trailing one. Structured output removes essentially all of this by construction, since the schema constraint applies to every single token generated, not just a final validation check after the fact.
The Pydantic and instructor pattern
For Python teams, the pattern that has become close to a default is defining response schemas as Pydantic models and using a library like instructor to handle the constrained generation and automatic validation against that schema, including automatic retry with error feedback if a response somehow fails validation despite the constraint. This turns "get structured data out of a model" into a pattern that looks and feels like calling any other typed function in your codebase, rather than a special AI-specific parsing dance.
Zod for TypeScript teams
TypeScript-based teams have converged on an equivalent pattern using Zod schemas, which serve double duty — defining the expected shape for both the constrained model output and the compile-time type your application code works with. This means the schema you use to constrain the model's output is the same schema that gives you type safety throughout the rest of your application, closing the gap between "the AI response is technically valid JSON" and "the AI response is the correctly-typed object my application logic expects."
How this changes AI feature architecture
The practical effect is that AI features can now be architected much more like normal typed application logic than like a special AI-integration layer bolted on the side. A function that calls a model and returns a structured result can slot into existing type systems, existing validation pipelines, and existing error handling patterns, rather than requiring bespoke parsing and defensive coding around unpredictable text output. This has lowered the barrier for less AI-specialized engineers to safely build AI features into a product, since the interface to the model now looks like a typed function call rather than a fundamentally different kind of integration.
What is still worth being careful about
Structured output guarantees the *shape* of a response is valid — it does not guarantee the *content* is correct. A model can produce a perfectly schema-valid JSON object that is factually wrong or contains a poor decision. Teams sometimes over-trust structured output as a correctness guarantee rather than what it actually is, a format guarantee, and skip the semantic validation that still needs to happen separately.
Why this matters for how we build client features
This shift has made AI feature development faster and more reliable for our engineering teams, and it is part of why AI features can now be integrated into existing typed codebases with far less custom glue code than a couple of years ago. If you are building AI features into a product and want engineers fluent in this modern pattern, see our AI engineering services or contact us with what you are building.


