Ilustración editorial para Salidas estructuradas con IA: cómo validar datos antes de guardarlos o actuar
Imagen generada con gpt-image-2.5-sunburst para InferamaSource ↗
01

A processable format does not guarantee correct data

A structured output is a model response organized so that an application can interpret it predictably—for example, a JSON object with defined fields and types. It can be useful for extracting data from documents, classifying requests, or preparing information for another component. Its main advantage is that it reduces ambiguity in the format; by itself, it does not turn generated content into a verified fact.

It helps to separate four questions. Can the response be parsed as JSON? Does it have the agreed fields and types? Are its values consistent with one another and with the source information? Is it permitted to use the response for its intended purpose? A response can pass the first checks and fail any of the later ones. An object containing a date in the expected format does not prove that the date appears in the document; a valid category does not prove that the classification is correct.

This distinction also helps you choose an output mechanism. JSON mode or schema-constrained generation is intended to produce a final response in a particular shape. A tool call, by contrast, provides arguments for the application to decide whether to execute an operation. Well-formed arguments do not automatically authorize that operation: execution remains under the control of the system integrating the model.

This guide focuses on the data contract checked on each run. It does not replace the management of changes to models, SDKs, or tools, which requires checking compatibility across versions. Even when the API has not changed, an ambiguous input, an incomplete document, or a misinterpretation can produce a result that should not be saved as confirmed.

02

Define the contract before writing the prompt

Start with the use that will receive the response, not with a list of fields that seems convenient for the model. If another system must store the result, specify what each field represents, what type it has, and what the application will do with it. A useful contract reduces the risk of different interpretations by the system generating the response and the system consuming it.

For each field, decide whether it is required, optional, or nullable. Do not use an empty string, a made-up value, or zero as a universal substitute for “unknown”: those values can be mistaken for real information. Also define units—for example, whether an amount is expressed in a particular currency—date formats, acceptable bounds, and allowed enumerations. If you have categories, explain what each one means and what to do when none fits.

Business rules usually go beyond types. A schema may allow two amounts to be numbers, while the application may require the total to equal the sum of the line items within a defined tolerance. It may accept a confidence value as a number without establishing an appropriate threshold for confirming a piece of data. Keep these rules explicit rather than assuming that the model will always apply them.

The JSON Schema standard can describe structures and constraints, but a provider's channel may support only a subset of its capabilities. Before relying on a particular keyword, check the current documentation for the model and output mode you have chosen. If a constraint is unavailable in that channel, validate it in the application; do not silently remove it or assume that a prompt instruction will make the model enforce it.

Contract decisions to settle

DecisionDesign questionApplication check
PresenceIs the field required, optional, or nullable?Reject disallowed omissions and distinguish null from an empty value.
Type and unitIs it text, an integer, a decimal, a date, or a quantity with a unit?Check the type and normalize only according to explicit rules.
Allowed valuesAre there permitted categories, ranges, or formats?Validate membership, bounds, and format in code.
RelationshipsWhat conditions must hold across multiple fields?Run business rules after validating the structure.
UseWill the output be displayed, stored, or used to propose an operation?Apply permissions and approvals according to the downstream effect.
03

Choose the channel based on the result you need

A structured final output is appropriate when the application needs organized data as its response. JSON mode can guide the overall shape; schema-constrained generation can impose additional constraints if the provider, model, and channel support them. Neither is a universal guarantee of truth, and neither eliminates the need to validate the response received.

Tool calling is useful for proposing arguments to a function known to the application. The workflow does not end when those arguments arrive: the system inspects them, decides whether they can be executed, and performs the operation if appropriate. Keep that separation visible in the design. A field such as “send_payment” should not be an instruction that runs merely because the model produced it.

Before putting an integration into production, check how the channel represents normal results, incomplete responses, and refusals. These states must not be treated as valid objects just because they appear during a transmission or can be partially converted to text. For streamed responses, wait until you have an identifiable final result before making a business decision.

If a schema constraint is unsupported, choose an explicit alternative: validate that constraint locally, simplify the contract without losing essential controls, or switch to a compatible channel. Record the difference. A contract that looks strict in the prompt but is not enforced by the channel can create a false sense of security.

Decision path for selecting a mechanism

  1. 01Decide whether you need a final response to display or store, or arguments for a function.
  2. 02Check the documentation for the chosen channel to see which output mode and constraints it supports.
  3. 03Verify how complete, incomplete, and refused results are identified in that channel.
  4. 04Implement validations the provider does not cover, and keep action execution under application control.
  5. 05Test the workflow with deliberate errors before allowing it to affect data or external systems.
04

Validate in layers, not with a single check

The first layer is the response state: determine whether you received a processable final response or whether the provider indicated a refusal, interruption, or incomplete result. If the result is truncated or unfinished, do not turn it into a partial acceptance unless your product has explicitly defined and tested that behavior.

The second layer is parsing and structure. Check that the content can be interpreted in the expected format, that the root has the agreed type, and that fields, types, allowed values, and applicable constraints are valid. Do not skip parse-error handling or automatically coerce ambiguous values, such as text to a number, without a documented rule.

The third layer checks meaning and relationships between fields. Verify ranges, consistency, incompatible combinations, and business conditions. The fourth compares the proposal with the original input: an invoice, ticket, or request. When possible, confirm relevant data in the document or in an authorized record. The fifth decides whether the use is permitted: displaying a suggestion does not have the same effect as changing an account or executing an external operation.

Keep the proposed value separate from the confirmed value. If a field needs review, the interface and storage should be able to represent it as pending or unverified. Overwriting the original evidence with a questionable extraction makes it harder to correct the error and reconstruct why a decision was made.

Validation layers and decisions

LayerWhat to checkIf it fails
StateThe response is complete and processable; it is not a refusal or incomplete result.Do not interpret it as an acceptable output; follow the failure policy.
Syntax and schemaParsing, types, fields, and supported constraints.Reject or request a narrowly scoped new generation.
SemanticsRelationships between fields and business rules.Quarantine or send for review.
EvidenceAgreement with the document, ticket, or request.Do not confirm the data; request evidence or review.
AuthorizationPermissions, limits, and approvals required for the destination.Block the action even if the arguments are valid.
05

Three practical walkthroughs

The examples below show how a single design distinguishes the shape of a response from its acceptance. The fields are illustrative: a real implementation should adapt them to its documents, accounting rules, taxonomy, and access controls.

blocks

06

Handle failures in a controlled way

Not every failure calls for the same response. A formatting error may allow a new generation with a narrower instruction. An incomplete response may require another request or a stop. A conflict with the source may need human review. A lack of authorization must block the action; it must not trigger a retry intended to obtain a more convenient answer.

Define in advance when to accept, retry, quarantine, and reject. Limit retries and retain the failed result so you can understand what happened. If the system retries, do not accumulate incompatible responses or simply choose the one that looks most complete. A retry should have a specific purpose—for example, correcting a missing field—and must pass through the same validations again.

Avoid silent corrections that can turn a visible failure into apparently reliable data. Normalizing whitespace or an unambiguous representation may be safe if documented; inventing a missing field, choosing between two contradictory amounts, or adjusting a category to pass a rule is not. Preserve enough information to distinguish the original value from the normalized value.

Response policy for unacceptable results

SituationControlled responseAvoid
Invalid JSON or missing fieldLimited retry or rejection, depending on impact.Automatically filling in a made-up value.
Incomplete or refused responseStop the acceptance path and follow the channel policy.Treating a fragment as the final response.
Conflict with the sourceQuarantine or review with access to the evidence.Choosing the most plausible value without a record.
Action without permissionBlock and request the required approval.Retrying until the model proposes a different action.
07

Test and record the complete workflow

A useful test suite includes ordinary examples and edge cases: missing fields, nulls, out-of-range values, blurry or contradictory documents, categories that do not fit, incomplete responses, and requests that exceed available permissions. Test each stage separately, too. This lets you distinguish a generation-channel failure from a validator error or an overly restrictive business rule.

Measure the proportion of responses usable without intervention, errors by field, detected contradictions, refusals, retries, and cases sent for review. A high schema-conformance rate is not the same as a high semantic-accuracy rate. Keep those metrics separate and compare a sample of accepted results with the original source.

To debug without storing unnecessary data, retain the schema version, the channel and model identifiers where appropriate, the final response state, the result of each validation layer, and the downstream decision. Record the input or a secure reference to it only when data policy allows. Avoid storing sensitive information by default if a reference, technical summary, or error signal is sufficient.

Version the contract and classify its changes. Adding an optional field may be compatible with consumers prepared to ignore it; changing a field's type, changing the meaning of a category, or making an optional field required can break them. Test consumers and migrations before deploying changes, and retain enough trace data to identify which version produced each piece of data.

Checks before saving, displaying as confirmed, or acting

  1. 01Is the response complete and not marked as refused or incomplete?
  2. 02Can it be parsed, and does it meet the current contract, including rules validated by the application?
  3. 03Are the values consistent with one another and supported by the input or an authorized source?
  4. 04Can the destination consume these values without confusing proposals with confirmed data?
  5. 05Is the downstream operation permitted and does it have the necessary approvals?
  6. 06If any answer is no or unknown, does the system stop it, send it for review, or apply a limited and recorded retry?
08

The final criterion: accept only what has been verified for its intended use

Whether to accept an output depends on its destination. A draft visible to a person may tolerate uncertainty if it is clearly marked; a confirmed accounting record or an external operation requires stricter controls. No single schema resolves these differences: the contract describes the shape, business rules check the meaning, and permissions govern the action.

Before launching the workflow, make sure you can answer what is validated, where it is validated, what evidence supports each value, what happens if a check fails, and who can authorize the next step. If the application cannot distinguish a proposal from verified data and an approved action, it is not yet ready to trust the output.

Open questions

  • The supported JSON Schema subset and available response states depend on the provider, model, version, and access channel; check current documentation before implementing specific constraints.
  • The fields, accounting tolerances, categories, review thresholds, and approval requirements in the examples are illustrative and should be defined according to the domain and team policies.
  • Retention of inputs, responses, and traces must comply with the privacy, security, and retention obligations applicable to the system.
09

Keep exploring

09

Sources consulted

03

Corrections and transparency

If you spot incorrect or outdated information, send us a correction with the page and source we should review.

Submit a correction