Intermediate

How to Get Structured Output from AI (2026)

Getting AI to return clean, structured output is the difference between a result you can use immediately and one you have to reformat by hand. These techniques give you JSON, tables, or any format you need, every time.

TLDR

Show the model an exact example of the format you want: an empty JSON schema, a table with headers, or a single completed example. Be explicit about every field, including what to return when data is missing.

How to do it

1

Show an exact example of the structure

Do not describe the format: show it. Paste an example of the exact JSON schema, table structure, or list format you want filled in. The model mirrors a shown example far more reliably than it follows a text description.

2

Specify how to handle missing data

If a field might not have a value, tell the model exactly what to return: null, an empty string, "N/A," or omit the field entirely. Unspecified behavior produces inconsistent handling across records.

3

Test with one record before the full batch

For large structured outputs, ask the model to produce one complete example first and confirm it matches before asking for the full set. Catching format errors on record one saves you from reformatting fifty.

4

Prohibit extra commentary

By default, AI models often add explanatory text before or after structured output. Add "return only the JSON, no explanation or surrounding text" to get clean output ready for parsing.

5

Use JSON mode for production applications

If you are building something programmatic, use the JSON mode available in the ChatGPT and Claude APIs. It enforces valid JSON at the model level, eliminating parsing failures from malformed responses.

Example prompt

Product review extraction: schema shown, field types specified, null handling defined, commentary prohibited

Extract the following product information from the review text below and return it as a JSON array. Use exactly this schema for each product: { "product_name": string, "rating": number (1-5), "main_complaint": string or null, "main_praise": string or null, "would_buy_again": boolean } Return only the JSON array. No explanation or surrounding text. [Paste review text here]

When to use it

Feeding output into code or a database

When AI output will be parsed by code, inconsistent formatting breaks everything. Structured output prompts make AI output reliable enough to use programmatically without manual cleanup.

Extracting data from unstructured text

Converting emails, documents, or web content into structured records is one of the most valuable AI use cases and requires precise format control to work reliably.

Building consistent reports

When you need the same structure every time, such as weekly reports, competitive analyses, or meeting summaries, structured prompts ensure the format is consistent across every run.

Common mistakes

01

Describing the format instead of showing it

"Return a JSON object with a name field, a date field, and a list of items" is harder to follow than showing the actual empty schema. Always show, do not describe.

02

Not specifying edge cases

What should happen when a field is empty, multiple values exist, or data is ambiguous? If you do not specify, the model makes its own inconsistent choices.

03

Asking for structure and analysis in the same prompt

Asking for JSON and a written explanation in one prompt often produces mixed-format output. Separate them: structure first, then a follow-up prompt for analysis if needed.

Frequently asked questions

How do I get reliable JSON from AI?+

Show the exact schema as an example, specify field types and null handling, and say "return only JSON with no explanation." For production use, enable JSON mode in the ChatGPT or Claude API.

Can AI extract structured data from PDFs or images?+

Yes, with multimodal models. Paste the document content or image and provide your JSON schema. Accuracy depends on the clarity of the source document.

What if the AI keeps adding explanation text around the JSON?+

Add this line to your prompt: "Your response must begin with { or [ and contain nothing else." This is more specific than asking it to omit explanation and typically eliminates the problem.

Bottom line

Show the exact format you want rather than describing it. Specify field types, null handling, and prohibit extra commentary. For production applications, use JSON mode in the API.

Related concepts

Put it into practice

Prompt packages that apply this technique directly.

More from Learn

Back to Learn