Structured Output: Getting Predictable Formats
Session 5.5 · ~5 min read
From Free-Form to Predictable
When AI generates free-form text, every output is different. The structure varies. The section order changes. The formatting is inconsistent. This is fine for one-off writing. It is unacceptable for production, where your next pipeline stage expects a specific format and your publishing system requires a specific structure.
Structured output means specifying the exact format of the AI's response. Instead of "write a product review," you specify "return a JSON object with fields: title (string), summary (100 words max), pros (array of strings), cons (array of strings), rating (integer 1-5), and recommendation (string)." The AI produces data you can parse, not prose you have to interpret.
Structured output turns AI from a creative tool into a production component. When the output format is predictable, you can build automated pipelines around it. Formatting scripts, quality checks, and publishing systems all require knowing what the output looks like before they see it.
Three Levels of Structure
Structure exists on a spectrum. You choose the level based on what your pipeline needs.
| Level | Format | Example Use | Parseable by Code |
|---|---|---|---|
| Loose | Prose with requested headers | Blog posts, articles | Partially (header extraction) |
| Template | Markdown with fixed sections | Reviews, comparisons, reports | Yes (section parsing) |
| Strict | JSON, XML, or YAML | Product data, metadata, structured content | Yes (native parsing) |
Template-Level Structure
For most content production, template-level structure is the sweet spot. You define a markdown template with required sections and the AI fills it in. The template ensures consistency while allowing the AI freedom within each section.
A content template for a product review might look like this:
## Overview
[2-3 sentences summarizing the product]
## What It Does Well
[3-4 specific strengths with examples]
## Where It Falls Short
[2-3 specific weaknesses with examples]
## Who Should Use It
[1-2 paragraphs defining the ideal user]
## Verdict
[1 paragraph, clear recommendation]
Include this template in your prompt with the instruction: "Fill in each section of the template below. Do not add, remove, or reorder sections." The AI produces output that matches your template exactly, making downstream processing predictable.
(fixed structure)"] --> B["AI fills sections"] B --> C["Consistent output"] C --> D["Automated formatting"] D --> E["Publishing"] F["No template
(free-form)"] --> G["AI decides structure"] G --> H["Inconsistent output"] H --> I["Manual reformatting"] I --> E style A fill:#222221,stroke:#6b8f71,color:#ede9e3 style C fill:#222221,stroke:#6b8f71,color:#ede9e3 style F fill:#222221,stroke:#c47a5a,color:#ede9e3 style H fill:#222221,stroke:#c47a5a,color:#ede9e3
Strict JSON Output
For data-heavy content or pipeline components that feed into code, JSON is the right choice. Modern AI APIs support structured output natively. Claude, GPT, and Gemini can all return valid JSON when instructed properly.
The key is providing a schema. Do not just say "return JSON." Specify the exact fields, types, and constraints. Many APIs now support response schemas that enforce the structure at the API level, guaranteeing valid output.
Testing Structural Consistency
A structured prompt is only useful if it produces consistent structure. Test by running the same prompt five times and comparing the outputs. Check: do all five have the same sections? Are the sections in the same order? Do all JSON outputs parse without errors?
If consistency is below 4 out of 5, the prompt needs refinement. Common fixes: more explicit section markers, stricter formatting instructions, or adding "Do not deviate from this structure" as a constraint.
| Consistency Score | Diagnosis | Action |
|---|---|---|
| 5/5 | Prompt is production-ready | Deploy to pipeline |
| 4/5 | Minor drift in one run | Add explicit constraints, re-test |
| 3/5 or below | Structure is not enforced | Rewrite prompt with stricter formatting rules |
Further Reading
- Structured Output with Claude, Anthropic documentation
- Structured Outputs, OpenAI documentation
- Complete Guide to Prompt Engineering with Temperature and Top-p
Assignment
Define a content template for your most common content type. Express it as a markdown structure with section headers and placeholder descriptions. Create a prompt that instructs the AI to fill in the template exactly. Test it five times. How consistent is the structure? Document any deviations and refine the prompt until you achieve 5/5 structural consistency.