Reliable structured output starts before the model generates a response. This guide provides a practical checklist for designing JSON and JSON Schema prompts, validating results, handling missing or invalid fields, and recovering from formatting failures in extraction, classification, API, and AI agent workflows.
Overview
Structured output prompts ask an AI model to return information in a machine-readable format rather than conversational prose. JSON is a common choice because applications can parse it, validate it, store it, and pass it to another step in an automated workflow. Typical uses include extracting fields from documents, classifying support requests, generating content metadata, routing tasks, and producing tool-call arguments for AI agents.
A prompt alone cannot guarantee valid data. Reliability depends on several connected controls: a clear task definition, an explicit output contract, suitable model or API settings, validation in application code, and a recovery path when the response is incomplete or malformed. Treat the model response as untrusted input, even when the prompt appears simple.
The most dependable workflow separates three questions:
- What should the model decide? Define the task, source material, and boundaries.
- What should the response look like? Define fields, types, allowed values, and missing-value behavior.
- What happens after generation? Parse, validate, log, retry selectively, or send the item to review.
Where a provider supports native structured outputs or JSON Schema enforcement, use that capability as an additional control rather than relying only on instructions. Provider features and syntax change, so verify current implementation details in the documentation for the model and SDK you are using. For broader prompt design principles, see this prompt engineering framework.
Checklist by scenario
1. Data extraction
Extraction prompts convert unstructured material into fields such as names, dates, totals, locations, or document sections. Start by defining the source boundary: tell the model to use only the supplied text or image and not to infer facts that are absent.
System instruction:
You extract fields from the supplied document. Use only information present in the document.
If a field is absent or unclear, return null. Do not guess. Return one JSON object only.
Required output:
{
"document_type": "string or null",
"invoice_number": "string or null",
"issue_date": "YYYY-MM-DD string or null",
"total_amount": "number or null",
"currency": "three-letter code or null",
"confidence_notes": ["string"]
}
- Specify whether dates, amounts, and identifiers should preserve the source format or use a normalized format.
- Use
nullfor a known missing value instead of an empty string unless your application requires otherwise. - Define how to handle conflicting values, such as two totals or multiple dates.
- Keep evidence separate from the normalized value when auditability matters.
2. Classification and routing
Classification is more reliable when the label set is closed and every label has a short definition. Do not ask for a category such as “best fit” without explaining the available choices and the fallback behavior.
Classify the message into exactly one category.
Allowed categories:
- billing: payment, invoice, refund, or charge questions
- technical: product errors or configuration problems
- account: access, profile, or subscription administration
- other: does not clearly fit the categories above
Return only:
{
"category": "billing | technical | account | other",
"urgency": "low | normal | high",
"reason": "one short sentence"
}
Validate the category and urgency against an allowlist. If the application uses the result to assign a queue or trigger an action, test ambiguous examples and near-matches rather than only obvious cases.
3. API and content workflows
For publishing or API workflows, make the contract reflect what the next system actually needs. Avoid asking for extra prose, commentary, or duplicate fields that create more opportunities for inconsistency.
Generate metadata for the supplied article.
Return one JSON object and no Markdown.
{
"title": "string, maximum 70 characters",
"description": "string, maximum 155 characters",
"slug": "lowercase hyphen-separated string",
"tags": ["string"],
"review_required": "boolean"
}
If the article lacks enough information for a field, use null and set
review_required to true.
Check length limits and slug rules in application code. Prompt instructions can guide generation, but validation should enforce requirements before publishing or sending data downstream.
4. AI agents and tool calls
Agent prompts need stricter boundaries because malformed output can cause an incorrect tool call. Define the tools, required arguments, permissions, and conditions under which the model must ask for clarification or decline to act.
- Use a closed list of tool names.
- Define required and optional arguments separately.
- Specify whether an empty result means “no action” or requires a follow-up.
- Validate arguments before execution, including identifiers, ranges, and authorization context.
- Require confirmation for irreversible or externally visible actions when appropriate.
For agents, JSON validity is only the first gate. A response can be valid JSON and still be unsafe, incomplete, or semantically wrong. Combine schema validation with business rules and permission checks.
What to double-check
Schema design
- Are field names stable, descriptive, and consistent with the application?
- Does every field have an explicit type?
- Are required fields truly required?
- Are allowed values represented as an enum or allowlist?
- Is the missing-value policy clear:
null, omission, empty array, or a fallback value? - Are nested objects and arrays necessary, or do they add avoidable complexity?
A small schema is usually easier to validate and maintain than a large response containing speculative fields. Add a field only when a downstream step needs it or a reviewer benefits from it.
Prompt instructions
Put the output contract close to the task instructions and provide one representative example when the format is difficult to express. State “return JSON only” when appropriate, but do not treat that sentence as validation. Also explain what the model should do when the input is empty, contradictory, outside scope, or insufficient.
Keep formatting requirements separate from reasoning requirements. If the model must analyze several items, ask it to return the final fields in the schema rather than requesting hidden or verbose reasoning. This reduces unnecessary tokens and makes the interface easier to test.
Application validation and recovery
Use a JSON parser first, then a schema validator, then domain-specific checks. Log the validation category without storing sensitive source material unnecessarily. A useful recovery sequence is:
- Attempt to parse the response.
- Validate the parsed object against the schema.
- Run business-rule checks, such as date ranges or permitted identifiers.
- Retry only when the failure is likely to be recoverable, using a concise correction prompt.
- Send repeated failures to a review queue or return a controlled error.
A repair prompt should include the validation error and the original response only when necessary. Ask for the corrected object, not an explanation. For a repeatable quality process, use a prompt testing and evaluation framework with representative test cases and regression checks.
Common mistakes
- Relying on formatting instructions alone: “Output valid JSON” does not replace parser and schema validation.
- Mixing JSON with commentary: Introductory text, Markdown fences, and explanations can break a strict parser. Request one object only and strip nothing blindly without understanding the risk.
- Using vague null behavior: Empty strings, omitted fields, and null values may have different meanings in the application.
- Allowing uncontrolled labels: Free-text categories create spelling variations and routing errors. Use enums where the set is known.
- Overloading one prompt: Extraction, interpretation, rewriting, and action selection may be better handled as separate steps in a prompt chain.
- Trusting valid JSON as correct data: Syntax validation cannot detect a wrong date, unsupported category, or fabricated value.
- Retrying without diagnosing: Repeating the same request can increase cost and latency without improving the result. Record whether the failure was syntactic, schema-related, semantic, or operational.
- Ignoring model and tool changes: Structured output support, token behavior, and SDK interfaces can change. Keep provider-specific configuration separate from the general prompt contract.
For production workflows, maintain a small prompt library with versioned schemas, sample inputs, expected outputs, known failure cases, and an owner. This makes changes reviewable and supports prompt evaluation across model or tool updates.
When to revisit
Revisit a structured output prompt before seasonal planning cycles, major content batches, or any workflow change that alters the source documents, labels, tools, or downstream fields. Also review it when you change the model, SDK, schema validator, retrieval pipeline, or agent permissions.
Use this quick pre-release checklist:
- Run valid, incomplete, ambiguous, contradictory, and out-of-scope examples.
- Confirm that every response is parsed and validated before use.
- Measure field accuracy, schema failure rate, retry rate, latency, and review volume.
- Inspect failures by category rather than judging only the overall pass rate.
- Confirm that sensitive inputs are handled according to your application requirements.
- Version the prompt and schema together, then retain a rollback path.
When a workflow changes, update the examples and validation rules before expanding the prompt. A reliable JSON response is not a one-time formatting trick; it is an interface that needs the same testing, monitoring, and maintenance as other parts of an AI development workflow. For related quality controls, compare your process with this prompt testing framework and review your token and retry behavior using the prompt cost control guide.