Ask a language model for a small JSON object and the first failure may be obvious: a code fence, a trailing comma, or a missing brace. Modern structured-output features can largely eliminate that class of error. But a response can be valid JSON, match a schema, and still be wrong in the way that matters: the extracted invoice total is inaccurate, the selected account is unauthorized, or a required fact has been quietly guessed.
That distinction is becoming important as AI responses move from chat windows into forms, databases, and software workflows. Structured generation is a useful interface contract, not a truth test. Teams get reliable systems by combining constrained generation with application-side checks, explicit failure handling, and review proportional to the consequences.
Three Different Kinds of Validity
It helps to separate three checks that are often bundled together. First, syntactic validity asks whether the output can be parsed as JSON at all. Second, schema validity asks whether it has the expected shape: the required fields, data types, and allowed values. Third, semantic validity asks whether those values are correct and acceptable in context.
For example, a shipping extractor might return {"postal_code":"02139","country":"US"}. This can parse and satisfy a schema requiring two strings. It may still be wrong if the source image actually says 02138, if the address belongs to a different order, or if the application should not ship to that location. A schema describes a form of data; it does not establish that the data is true, belongs to the right person, or is safe to act on.
JSON mode and schema-constrained output do different jobs
A JSON-only mode generally aims to produce parseable JSON. A schema-constrained mode goes further by restricting the response to a declared structure. OpenAI’s current guide distinguishes its basic JSON mode from Structured Outputs: JSON mode does not guarantee conformance to a particular schema, while Structured Outputs can enforce a supported subset of JSON Schema. Google’s Gemini documentation likewise describes structured output as supporting a subset of JSON Schema, with some constraints not enforced by its decoding engine.
In constrained decoding, the system narrows the tokens the model may emit as it generates. If the next token would break the permitted structure, it can be masked out. The result can make a missing key, wrong primitive type, or out-of-enum value much less likely, or impossible under the supported constraint and successful completion. It does not add evidence to the model’s answer. If the schema allows a string for invoice_total, the generator can still put the wrong amount in that string while remaining perfectly compliant.
For a deeper look at the data layer behind grounded answers, see our guide to retrieval-augmented generation as a search system. Retrieving relevant material and formatting the response are separate controls; neither alone proves that a claim follows from its source.
A schema is not automatically portable between providers
JSON Schema is a published specification, currently at Draft 2020-12, but an API feature that accepts “JSON Schema” may implement only a subset. Providers can differ in supported keywords, nesting limits, required-field conventions, and what happens when a schema is too complex. OpenAI documents unsupported keywords that cause strict-schema requests to fail. Gemini documents constraints that are ignored during constrained decoding and should instead be checked by the client.
This matters during model migration. A schema accepted by one endpoint may be rejected or interpreted differently by another, even if both use the same broad format name. Treat provider support as a runtime capability to test, not as an assumption encoded in a shared type definition. Keep the canonical schema in your application, validate against it after generation, and maintain small compatibility tests for each provider and model version you actually deploy.
The JSONSchemaBench study examines constrained-decoding systems across efficiency, schema coverage, and output quality. Its framing is useful for engineers: “does it produce JSON?” is only one axis. Schema coverage and end-task behavior matter too. A benchmark cannot guarantee performance for your own prompt, model, schema, or data, so representative tests remain necessary.
Use layered validation after generation
A robust pipeline can be thought of as a series of gates, each answering a different question:
- Parse: Is there a complete response that the JSON parser can read?
- Validate the schema: Are required fields present, types correct, and values within the constraints your validator enforces?
- Check business rules: Does the amount reconcile with line items? Is the date plausible? Does this identifier belong to the current record?
- Check evidence and permissions: Can each consequential field be traced to source material, and is the requested action authorized?
- Choose an action: Accept, ask for clarification, send to human review, or safely decline to act.
Keep these checks in ordinary application code wherever possible. For example, a schema may require a non-empty description, but the server should independently verify length if a provider does not enforce that keyword. A financial workflow can parse a currency value, then compare it against the source document and policy limits. A tool call should pass through the same authorization checks as any other request; a well-formed command is not permission.
In high-impact situations, do not turn “schema-valid” into an automatic approval rule. Design an explicit abstention path: a null or “unknown” value when the source is missing, a request for more information when fields conflict, or human review when confidence and stakes do not justify automation. Do not make the model invent a plausible value just to fill a required field.
Refusals, truncation, and partial responses are normal states
Even structured-output APIs have control-flow outcomes that are not ordinary data. A model may refuse a request, a response may stop because it reached a token limit, or a provider may signal an incomplete result. The OpenAI guide explicitly calls out refusal and incomplete-output handling; its examples inspect finish reasons rather than assuming every response contains a completed object.
Applications should represent these outcomes separately from a successful business object. Do not pass a refusal string or partial payload into the same parser-and-action path as a completed record. Log the status, preserve enough context to debug, and use bounded retries only when retrying is safe. Repeating an incomplete read-only extraction is different from retrying a payment or a tool action that may already have executed.
Evaluate the whole workflow, not just JSON parsing
Build an evaluation set from realistic examples, including malformed source documents, ambiguous values, missing fields, contradictory evidence, long inputs, and cases where the correct answer is “unknown.” Track separate rates for parse failures, schema failures, semantic errors, unsupported-schema requests, refusals, incomplete outputs, and incorrect downstream actions. This makes it possible to see whether a provider change improved formatting while harming extraction quality, or reduced model errors while increasing latency.
Include adversarial and boundary cases, but do not mistake a passing test suite for a proof of correctness. Test the actual model, API mode, schema, SDK, and production transformations together. If schemas are generated from application types, test the generated schema too; decorators and client libraries may add constraints that a provider later strips or rejects.
Structured generation pairs naturally with the permissions discipline discussed in separate identities and authorization for AI agents. Formatting helps software understand a proposed action. Identity and authorization determine whether the system may perform it.
What to watch next
Structured-output features are expanding across hosted APIs and local inference stacks, but schemas remain contracts interpreted by specific implementations. Watch for clearer provider compatibility documentation, better incomplete/refusal signaling, and evaluation tools that test schema coverage alongside semantic task quality. For application builders, the practical direction is already clear: keep schemas small and explicit, validate at the boundary, test the whole transaction, and preserve a safe path when the model cannot provide a trustworthy answer.
A model that returns the right shape has made software integration easier. The remaining question – whether that shape contains the right, authorized, evidence-backed values – still belongs to the system around it.










