Product Architecture

Structured Outputs Beyond Valid JSON

Feb 8, 202610 min readUpdated Jul 26, 2026

Hand-drawn pen-and-ink diagram of the mechanism explained in "Structured Outputs Beyond Valid JSON".
01 / 09

A schema is an application contract

Structured output moves the model from prose into a typed boundary, but schema design determines reliability. Use descriptive domain fields, closed enumerations, explicit nullability, bounded arrays, and units in field names or descriptions. Avoid one free-form details object that recreates the parsing problem inside JSON. Keep the contract as small as the downstream decision needs; every optional field creates another state clients must interpret.

Separate observations from decisions. An extraction schema might contain source values and confidence, while deterministic code computes eligibility. A planning schema can propose actions but cannot mark them authorized. Include evidence references for values that must be grounded. This separation makes semantic validation possible and prevents a plausible object from quietly crossing a business boundary.

Version schemas and generated client types together. Adding an enum value can break an exhaustive consumer even if JSON validation still passes. Prefer additive changes, maintain adapters for active workflow histories, and record schema version with every result. Do not reuse one field with a new meaning; create a migration path. Model prompts, validators, tools, and UI must agree on the contract.

02 / 09

Constrained decoding solves syntax only

Grammar or schema-constrained decoding masks tokens that would make the output invalid, guaranteeing parseable structure when supported. It can prevent missing braces, unknown fields, and invalid enum spelling. It does not ensure a date exists, an identifier belongs to the user, a sum is correct, or cited evidence supports a claim. Treat syntactic validity as the first validation layer, not completion.

Complex schemas can increase latency or create dead ends, especially with deep unions, broad recursive structures, and enormous enumerations. Simplify contracts and move large dynamic choices into validated tool lookups. If the model must select one of ten thousand product IDs, retrieve a small eligible set and constrain against it rather than embedding the catalog in the schema. This improves both decoding and semantic accuracy.

Streaming structured output requires care because partial JSON is not a valid object. Stream high-level progress separately, or use event records whose individual frames are complete and validated. Never trigger side effects from an unfinished field sequence. Buffer the final action proposal, validate it, display a preview when needed, then execute through the normal authorization path.

03 / 09

Validate meaning with deterministic rules

After parsing, validate field formats, ranges, cross-field relationships, source membership, and business invariants. A start date must precede an end date; percentages must total appropriately; a tool identifier must exist in the eligible set. Return machine-readable error paths and expected constraints. This gives a repair step precise information and produces useful telemetry about recurring model failures.

Ground extracted fields by retaining source spans or record IDs. Verify copied text and identifiers against the source rather than trusting model confidence. Recompute arithmetic and normalization in code. For classifications, enforce mutual exclusions and required explanations only where they improve audit. The general rule is to let the model handle ambiguity and language while software handles crisp consistency.

Authorization happens after semantic validation with authenticated context. Never accept a tenant, owner, approval, or role merely because the model emitted it. Resolve resource identifiers server-side and check access. A structurally perfect tool call remains untrusted input. This principle allows teams to adopt new models without turning improvements in format adherence into expanded privilege.

04 / 09

Repair without hiding systemic defects

A targeted repair prompt can receive the invalid object and concise validation errors, then return a corrected object under the same constraint. Limit attempts and avoid changing already valid grounded fields unless necessary. For low-risk formatting failures, deterministic repair may be safer. For semantic contradictions, re-run from original evidence or escalate rather than encouraging the model to invent values that satisfy the validator.

Track first-pass validity separately from final validity. An application that repairs ninety percent of responses may appear reliable while doubling latency and cost. Group errors by schema path, model, prompt, and input slice. Frequent failures usually indicate a confusing field, an overly broad contract, missing context, or a model-route mismatch. Fix the interface instead of normalizing endless repair.

Define failure behavior for consumers. A missing optional enrichment may degrade gracefully, while an invalid payment proposal must stop. Preserve the validation report and original output for secure debugging, but show users a clear recovery path. Structured output is successful when downstream systems can make safe deterministic decisions, not merely when a JSON parser stops throwing.

05 / 09

Research foundation and scope

JSON Schema defines structural constraints, vocabularies, references, and validation output for JSON documents [JSON-SCHEMA]. Constrained decoding may satisfy a syntax or schema grammar, but business validity and authorization remain application responsibilities. These sources establish the mechanism, but they do not remove the need to measure the local implementation. A paper's result belongs to its checkpoint, dataset, hardware, traffic, and experimental protocol; a standard describes a contract or risk practice, not a guarantee that a particular product follows it. The useful engineering move is to convert each cited idea into a hypothesis that can be falsified with representative inputs and observable intermediate state.

A structurally valid invoice can reference a nonexistent customer; a valid tool call can request an unauthorized transfer. Schema version, semantic rules, referential integrity, units, temporal constraints, and effect policy complete the contract. Write these conditions into the design review before selecting a library or provider. Specify the authoritative source of truth, the authenticated principal, the versions that influence behavior, the maximum consequence of an error, and what the system will do when required evidence is missing. This boundary statement makes later optimization honest: teams can compare speed, cost, or convenience only after candidates satisfy the same correctness, authorization, privacy, and recovery contract.

Read [JSON-SCHEMA], [OWASP-LLM], [NIST-GAI] together rather than treating one source as a recipe. JSON Schema Draft 2020-12 Core supplies one part of the foundation; OWASP Top 10 for LLM Applications 2025 supplies a second perspective; Artificial Intelligence Risk Management Framework: Generative Artificial Intelligence Profile helps connect the mechanism to evaluation, governance, or operations. The citations are deliberately primary papers, standards, or official documentation. Product-specific claims still require local evidence, and any dated behavior must be rechecked against the cited version before an article or architecture decision is refreshed.

06 / 09

A production implementation blueprint

Use closed, versioned schemas with explicit nullability and enums, generate into a typed boundary, validate deterministically, resolve references, enforce policy with the authenticated principal, and return machine-readable errors. Begin with one thin vertical path that preserves all decision evidence. Give each run a stable identifier; capture normalized input, authenticated scope, relevant dependency versions, selected policy, timing, result status, and any external receipt. Keep large or sensitive payloads in a separately governed store and pass bounded references through the orchestration layer. This produces a debuggable system without making raw customer content the default telemetry substrate.

Make state transitions explicit and make every external boundary return a typed result. Distinguish validation failure, permission denial, missing evidence, rate limit, timeout, dependency outage, user cancellation, and internal defect. The distinction matters because each permits a different response: clarify, retry, fall back, defer, compensate, or stop. A generic exception handler that asks the model to try again turns uncertainty into repeated cost and can duplicate irreversible effects. Retries require an idempotency strategy and a stored receipt, not optimism.

Roll out the implementation as a versioned policy. Record which cohort receives it, what baseline it replaces, which metrics can stop the rollout, and how to reverse stateful artifacts such as indexes, caches, adapters, or workflow histories. Review privacy and retention alongside performance. If raw content is needed temporarily for diagnosis, state the purpose, access, sampling, encryption, and deletion window. Production readiness means the team can explain, contain, and reverse behavior under load—not merely demonstrate the happy path once.

07 / 09

Failure lab and evaluation plan

Property-test schemas, fuzz boundaries, replay old payloads against new readers, and test valid-but-wrong values. Measure whether a repair pass fixes isolated model mistakes without concealing systematic prompt or contract defects. Keep the arrival pattern, data distribution, authorization, and dependency versions close to production. Use a paired baseline where possible and retain per-case results so averages cannot hide a severe slice. Offline evaluation should localize the failing stage; shadow evaluation should expose integration and traffic effects; a canary should confirm user-facing outcomes with a small blast radius. Each stage needs an exit criterion rather than a vague request to 'look good.'

Include extra fields, truncated streams, duplicate keys, huge arrays, recursive references, incompatible schema revisions, locale-formatted numbers, invalid dates, unauthorized IDs, and output that becomes executable in HTML or SQL. Add adversarial boundary cases and partial failures, not only malformed inputs. Terminate a worker after an external system may have committed, revoke access after a cache was populated, change one dependency version, and delay one branch until its data is stale. Confirm that cancellation releases resources and that a fallback preserves semantics. The test passes only when the external outcome, audit evidence, and user-visible state agree; a fluent final message is not proof of completion.

Evaluation data needs lineage equal to production data. Record why each case exists, who reviewed the expected behavior, which source or policy supports it, and when it must be refreshed. Separate generated cases from production incidents and prevent test cases from leaking into training. When a metric changes, inspect changed examples before accepting an aggregate. A statistically small regression can still block release if it crosses an authorization, privacy, safety, or irreversible-action boundary.

08 / 09

Operating metrics and decision record

Track parse, schema, semantic, authorization, and downstream rejection separately; also record repair success, retries, latency, silent coercions, consumer-version failures, and side effects prevented. Define every metric with a numerator, denominator, unit, time window, exclusions, and responsible owner. Segment by workload and consequence so a dominant easy class cannot hide a rare costly failure. Pair service signals with product outcomes: latency without completion encourages fast useless answers, while answer quality without queue and cost data can produce an uneconomic service. Review percentiles and distributions rather than relying on a single average.

Treat constrained generation as the first validation layer. Release data or execute a tool only after deterministic semantics, authorization, and consumer compatibility pass. Put that choice in a short architecture decision record containing context, alternatives, evidence, assumptions, selected policy, rollout, reversal trigger, and unresolved risk. Link the evaluation snapshot and source versions. This document prevents a benchmark from becoming folklore and tells the next engineer which evidence must be repeated when the model, provider, traffic, data, regulation, or product promise changes.

Monitor leading and lagging signals after release. Leading signals reveal pressure—queue age, cache allocation, disagreement, validation failure, policy denial, or retry growth—before users report harm. Lagging signals show whether the job was actually completed and trusted. Alert on a service objective tied to user consequence, and make the run identifier available to support without exposing private content. Every serious miss should become a reproducible regression case and, when relevant, a new threat or data-quality control.

09 / 09

Primary sources and further reading

  1. JSON Schema Draft 2020-12 CoreJSON Schema project · standard
  2. OWASP Top 10 for LLM Applications 2025OWASP Foundation · standard
  3. Artificial Intelligence Risk Management Framework: Generative Artificial Intelligence ProfileNational Institute of Standards and Technology · standard

Written by

Vishal RajpurohitCo-founder & CTO, ViitorCloud

Co-founder & CTO of ViitorCloud · Founder of LaraCopilot · Organizer of Laracon India · Shipping production software since 2005

More about Vishal