1. Design the schema from the downstream use case
A structured output is a model response conforming to a predefined data shape, such as a JSON object with defined fields. The goal is not merely for the response to 'look like JSON'; it is for the receiving system to interpret it consistently. To achieve this, start with the downstream consumer and work backward: identify which fields are needed, what will be done with them, and what should happen when the input does not contain the requested information.
Keep the schema as small as the use case allows. For each field, document the type, whether it is mandatory, allowed values, units, and expected provenance. Also decide how to represent missing data. A null value, an empty string, and an omitted field can have different meanings; do not interchange them without an explicit rule. If a date must feed a database, for example, prefer a normalized format and a defined timezone rather than accepting expressions like 'Friday'.
Separate structure from semantic instructions. The schema can require that `date` be a string, but that alone does not prevent the model from inventing a date. Field descriptions and the prompt can guide extraction, but the application must establish what evidence it accepts and what result to use when it is not found. A safe option is to allow an explicit state like `not_found` or an evidence field, rather than forcing an affirmative answer.
Azure OpenAI documentation describes the use of a JSON schema definition in an inference call and shows examples with Pydantic and Zod. Gemini documentation also presents JSON responses fitted to a schema and examples of using data models. These documented capabilities are specific to the described APIs and configurations; they do not prove that all schemas, models, or providers accept exactly the same restrictions.
Decisions to close before calling the model
| Decision | Practical question | Example rule |
|---|---|---|
| Mandatory fields | What does the consumer need to process the record? | `status` is always mandatory; `delivery_date` can be null. |
| Missing data | How does the system distinguish unavailable data from empty data? | Use `null` and a `not_found` state. |
| Normalization | What format avoids ambiguity in the next system? | ISO 8601 date with timezone when applicable. |
| Traceability | How will a doubtful extraction be reviewed? | Save the source fragment alongside the value. |
| Allowed values | What categories are valid for the pipeline? | Closed enumeration defined by the product. |
2. Choose the output mechanism based on task and provider
There are three common approaches, but they are not equivalent. Pure prompting asks the model to return a specific format via natural language instructions. It may suffice for a prototype or when the output is only for human reading, but the format can degrade: explanations appear outside the JSON, unexpected keys, or values of incorrect types. If the result feeds code, treat it as a less controllable option and always validate the response.
JSON mode requests a JSON response. Azure documentation distinguishes this mode from structured outputs with schema: the former guarantees valid JSON, but does not guarantee strict compliance with the provided schema. Therefore, a response that can be parsed as JSON does not prove it has the keys, types, or restrictions the application needs.
Structured outputs with schema allow providing a definition to guide the shape of the response. Azure documentation shows this mechanism in response format fields, as well as integration examples with Pydantic and Zod. Gemini also documents the configuration of a schema-based JSON response and extraction and classification examples. Specific availability depends on the API, model, and configuration chosen: check the current documentation of the endpoint you will actually use, instead of assuming that two products with similar names have identical behavior.
Function or tool calls serve when the model must propose arguments for an operation available in the application, such as querying an order. They do not equate to executing the function or verifying that the action is safe: the server must validate the arguments and apply its own controls before acting. In the use of structured outputs for function calls described by Azure, the documentation warns that parallel calls are not supported and recommends disabling them in that configuration. Do not extrapolate that limitation to other APIs without confirming it.
As a selection rule: use a schema-based output when the main goal is to produce an object fitted to a definition; use a function call when the model must select an operation and propose its arguments; and reserve pure prompting for lower-risk contexts or tasks where strict local validation compensates for lower coercion. In any case, the response must pass through application controls before reaching the downstream system.
Operational comparison of mechanisms
| Mechanism | What it provides | Risk that still exists | Guiding use |
|---|---|---|---|
| Pure prompting | Textual instructions on the expected format. | May violate the format or include additional text. | Prototypes or non-critical outputs with subsequent validation. |
| JSON mode | Requests a JSON response; in Azure, documentation distinguishes it from strict schema compliance. | Valid JSON may have fields or values that do not fit the application contract. | When JSON syntax is enough and the application completes the checks. |
| Structured output with schema | Guides the response toward a declared structure. | Does not certify that the content is true or that the schema covers all business rules. | Extraction or classification integrated with additional content validation. |
| Function or tool call | Represents an operation and its arguments in a processable way. | Arguments may be erroneous; executing the operation requires server controls. | Scoped actions, with authorization and validation before execution. |
3. Validate the structure before processing the response
The first barrier is to parse the response with a parser of the expected format. For JSON, the parser must reject invalid text, truncated values, and content that cannot be converted into an object. If the provider delivers a response already parsed by an SDK, still check the completion status, refusals, and fields that may indicate an incomplete response; do not assume that a usable result was always received.
The second barrier is to validate against a local schema. JSON Schema allows expressing types, mandatory fields, enumerations, length restrictions, and rules for additional properties, among other conditions. Validation libraries can turn those rules into actionable errors before the output reaches a database or an API. The specific implementation depends on the language and schema version: verify the compatibility of the chosen library with the keywords and dialect used by your contract, and test the combination in continuous integration.
Do not copy without review the schema accepted by the provider as the definitive contract of the application. The provider may accept only a subset of the rules, or transform a data model from a library into a definition compatible with its API. Maintain a source of truth for your contract and, if the provider requires a reduced representation, generate or adapt the call schema without removing critical local validation rules.
Validation must be strict at system boundaries. Reject unexpected properties if they could alter behavior; convert types only when the conversion is unambiguous and documented; and avoid 'fixing' doubtful values automatically. If a field must be an integer, converting `12.5` to `12` is not an innocuous repair: it can change the meaning. Save the error, the request identifier, and the schema version to diagnose failures without silently accepting defective data.
Technical validation sequence
Apply the controls in this order and stop processing at the first failure that prevents interpreting the output safely.
- 01Check that the request completed and did not receive a refusal or an incomplete response.
- 02Extract the content corresponding to the requested format; do not mix explanatory text with the expected object.
- 03Parse the JSON and reject syntax errors, truncations, and values of the wrong type.
- 04Validate the object against the local schema: types, keys, mandatory fields, enumerations, and limits.
- 05Apply business rules not expressed in the schema, such as existing references or allowed ranges.
- 06Record the result, the schema version, and the reason for rejection; do not send the invalid response to the downstream system.
4. Check the content and represent uncertainty
A structurally correct response can be semantically false. To measure that difference, evaluate the fields against the original input and available reference sources, not against the fluency of the model's explanation. In extraction, retain sufficient textual evidence to locate the data in the document. In classification, save the rule or signal that allowed assigning the category. In enrichment, identify which values come from the input and which depend on an authorized external source.
Design an explicit representation for uncertainty. Depending on the case, it can be a null value accompanied by a state, a list of candidates, or a review mark. Avoid asking for a confidence percentage without defining what it means and how it will be calibrated: a figure generated by the model itself is not, by itself, a verified probability. If you use a score, measure it with a labeled set and decide thresholds based on the cost of errors and reviews, not by intuition.
Human escalation must be a normal route of the pipeline, not an improvised exception. Send to review ambiguous cases, those lacking evidence, those violating business rules, and those involving a high-impact decision. The reviewer needs to see the input, the proposed output, the evidence, and the reason for escalation. If a human correction is saved as training data or as a regression example, also record its provenance and the applicable policy.
The available evidence here supports schema-based output capabilities for Azure OpenAI and Gemini, but does not quantify format error rates versus semantic error rates. It also does not allow establishing a universal confidence threshold. Therefore, measure both classes of failure in your own task: a low rate of invalid JSON does not prove a correct extraction, and a good result on a small sample does not guarantee production behavior.
Rules for deciding between acceptance and review
| Observed condition | Safe decision | What to review |
|---|---|---|
| Unparseable JSON or schema violation | Reject and log; do not consume. | Parser or validation error. |
| Data missing in the source | Accept as missing if the contract allows it. | Explicit state and evidence of absence when applicable. |
| Two plausible interpretations | Escalate or return ambiguous state. | Candidates and fragments supporting each option. |
| High-impact data without corroboration | Do not automate the final action. | Authorized source, policy, and human approval. |
| Valid response with weak evidence | Retain for review. | Threshold calibrated with labeled examples. |
5. Test the system and monitor its behavior in production
Build a regression set with representative examples and edge cases before deploying changes to the prompt, schema, model, or SDK. Include clean inputs, incomplete inputs, contradictory data, unexpected characters, long documents, and cases where the correct answer is 'not found'. For each case, retain a verifiable expectation: expected result, fields that must remain absent, and conditions that force escalation.
Measure separately syntactic validity, schema compliance, field accuracy, and escalation rate. A single aggregated metric can hide that the system produces perfect JSON but classifies poorly, or that it gets most fields right and fails precisely on the highest-impact ones. Segment results by input type, model version, prompt template, and schema version.
In production, log metrics that allow detecting degradations: unparseable responses, validation errors, null fields, out-of-range values, disagreements with reference sources, and proportion of human reviews. Define alerts and a recovery path, for example retaining doubtful records or rolling back to a previous version. Do not store unnecessary sensitive data in logs; keep only what is required for debugging, auditing, and compliance with the applicable policy.
Before changing provider or mechanism, repeat tests with the same sample and the same acceptance criteria. Documented functions are not interchangeable in all their details, and a migration may modify which restrictions are accepted or how responses are represented. Treat production configuration as part of the contract: provider, endpoint, model, schema, SDK, prompt, and retry policy must be versioned and auditable.
Deployment and monitoring protocol
- 01Define a labeled set of normal, ambiguous, incomplete, and adversarial cases for the task.
- 02Run each case with the exact combination of provider, model, endpoint, SDK, and schema planned for production.
- 03Calculate separate metrics for syntax, structure, field accuracy, evidence, and human escalation.
- 04Set exit criteria and rejection limits before comparing versions; do not adjust the threshold just to improve an aggregated figure.
- 05Deploy gradually and monitor errors, nulls, extreme values, and changes in the distribution of cases.
- 06Upon degradation, retain affected records, activate review or rollback, and analyze the cause before resuming automation.
Open questions
- The provided evidence does not document here the current capabilities or structured output limitations of OpenAI or Anthropic; those providers are not compared.
- Available sources do not quantify format error rates versus content errors or establish universal confidence thresholds.
- The exact compatibility of each provider with JSON Schema dialects and keywords must be verified for the specific endpoint, model, and SDK.
- The consulted documentation describes capabilities of Azure OpenAI and Gemini, but does not by itself certify semantic correctness, production performance, or suitability for any task.
Keep exploring
Sources consulted
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