Controlling HOW the AI formats its response is just as important as controlling WHAT it says. For production systems, structured output is non-negotiable — downstream code needs to parse AI responses reliably.
Key Concepts
- Structured Output: Machine-readable formats like JSON, XML, CSV
- Semi-Structured Output: Human-readable but organized formats like Markdown, tables, lists
- Format Enforcement: Techniques to guarantee the model outputs valid structured data
Common Output Formats
| Format | Best For | Parsing Difficulty |
|---|---|---|
| JSON | API responses, data pipelines | Easy (native parsing) |
| Markdown | Documentation, reports | Medium |
| CSV | Spreadsheet data, bulk analysis | Easy |
| XML | Legacy systems, SOAP APIs | Medium |
| YAML | Configuration files | Easy |
| Plain text | Human consumption | N/A |
| HTML | Web content | Medium |
Techniques for Reliable JSON Output
- Schema Definition: Provide the exact JSON schema you expect
- Assistant Prefix: Start the response with
{to force JSON mode - Few-Shot Examples: Show 1-2 examples of the desired JSON structure
- Validation Instructions: "Ensure the output is valid JSON. Do not include any text before or after the JSON object."
Example: Forcing JSON Output
"Analyze the following customer review and output a JSON object with this exact schema:
`json
{
"sentiment": "positive" | "negative" | "neutral",
"confidence": 0.0 to 1.0,
"key_topics": ["string"],
"summary": "string (max 50 words)"
}
`
Output ONLY the JSON object. No explanations, no markdown code fences."
Table Generation
"Create a comparison table with these exact columns:
Use checkmarks (✅) and crosses (❌) for boolean features. Include at least 8 rows."
Markdown Reports
"Generate a technical report in this exact Markdown structure: