Structured Outputs
Jump to section
Why structured outputs
Most people get free-form text from AI and then manually reformat it. That's a waste of time. When you tell AI exactly what format you want, you get output you can use directly — paste into a spreadsheet, process with a tool, or share with colleagues without editing.
Structured outputs are especially valuable in three situations: you're processing output programmatically (JSON, CSV), you need consistent format for repeated use (templates), or you want output that drops straight into another document (tables, lists).
Markdown tables
The simplest structured format. Just say 'Format the output as a table' and most models will create a markdown table. But for consistent results, be more specific — define the exact columns.
Prompt: 'Compare the 3 most popular task management tools (Asana, Monday.com, Trello). Output as a table with columns: Name, Monthly price (team of 10), Main advantage, Main disadvantage, Ideal for.' — This tells the AI exactly which columns you want and in what order. The result will be a consistent table you can paste directly into a document or presentation.
For tables going into Excel or Google Sheets: ask AI for CSV format. 'Output as CSV with semicolon delimiter. Columns: [columns]. No other text.' CSV imports directly into spreadsheets.
Structured bullet lists
When you need a structured list, define the format of each item. Instead of 'Write a list of tips' try: 'Write 5 tips. Format each tip as: tip name — 1 sentence explanation. Example: [concrete usage example].' This gives you a consistent format for every item.
When you want truly consistent formatting, show AI one completed item as a template. AI will follow that exact template for all remaining items. This combines few-shot (lesson 4) with structured outputs — two of the most powerful tools together.
JSON outputs
JSON is ideal when you need to process data with a program, import into a tool, or simply want perfectly structured output. The key: define the schema upfront and give an example with specific value types.
// Prompt: Analyze this text and return the result as JSON:
{
"sentiment": "positive | negative | neutral",
"confidence": 0.0-1.0,
"key_topics": ["topic1", "topic2"],
"summary": "1 sentence summary",
"action_items": [
{
"task": "description",
"priority": "high | medium | low"
}
]
}When you give AI a specific JSON schema with example value types (string, number, array), the model follows it precisely. If you need guaranteed valid JSON (for programmatic processing), the OpenAI API offers 'JSON mode' and 'Structured Outputs' — the model is forced to return valid JSON.
Schema-driven prompting
The most reliable technique for structured outputs: give AI an example of the complete structure with labels instead of values. AI then fills in real data into your structure. This works because you're telling the model exactly what shape the output should have — the model then predicts tokens to match your template.
Schema-driven prompting is like giving AI a form to fill out instead of a blank sheet of paper. With a blank sheet, it can write anything. With a form, it fills in exactly what you need. This technique is the foundation of most professional AI applications.
XML and tagged context
For complex prompts with multiple inputs, use XML-like tags to distinguish different parts. Claude explicitly recommends this approach. Example: '<document>document text</document> <instructions>your instructions</instructions> <output_format>desired format</output_format>'. Tags help the model unambiguously distinguish what is context, what is instruction, and what is format.
Consistency and common mistakes
When processing multiple items the same way, define the format once and repeat it. AI adding text around the structure? Add: 'Return ONLY [format], no explanation before or after.' AI changing format between items? Add a sample example of the first item. AI returning invalid JSON? Simplify the schema — fewer nested objects, shorter values, fewer optional fields.
If AI persistently adds explanatory text before or after the structured output, try: 'Respond EXCLUSIVELY in the requested format. No introductions, no explanations, no comments. Only [format].' Or add at the end: 'Begin directly with the response:'
Pick any topic (movies, restaurants, books, technology...) and write a prompt that gets a list of 5 items from AI as a valid JSON array. Each item must have at least 5 keys (including a nested object or array). Define the schema directly in your prompt. Example schema for restaurants: [{"name": "...", "cuisine": "...", "price_range": "$/$$/$$$/$$$$", "rating": 0.0-5.0, "best_for": ["date night", "business lunch"], "location": {"city": "...", "neighborhood": "..."}}] Verify that the resulting JSON is valid — copy it into jsonlint.com or another validator.
Hint
If AI adds text before or after the JSON, add the instruction 'Return ONLY the JSON array, no other text. Begin directly with ['. If the format isn't consistent, add one complete example as a template.
Paste any unstructured text into AI (part of an email, meeting notes, product description) and ask for transformation into both a table and JSON. Compare both outputs — which is more useful for your situation? Example prompt: 'This unstructured text contains information about a project: [text]. Extract key information and return it in two formats: 1) Markdown table, 2) JSON object. Both with the same data.'
Hint
This exercise shows the power of structured outputs — from chaotic text you create clean data that you can further process.
Pick any topic and ask AI for output in CSV format that you can import directly into Google Sheets or Excel. Prompt: 'Create a table about [topic] in CSV format with semicolon delimiter. Columns: [define]. 10 rows of data. No other text, only CSV.' Copy the output into a spreadsheet and verify the import works correctly.
Hint
CSV import in Google Sheets: File -> Import -> Paste. In Excel: Data -> From Text/CSV. If import doesn't work, check the delimiter (semicolon vs. comma) and character encoding.
- Define output format directly in your prompt — table, list, JSON, CSV
- Schema-driven prompting: give AI a template/form to fill in — foundation of professional AI apps
- For consistency, show an example of one completed item (few-shot + structure)
- XML-like tags (<document>, <instructions>) help distinguish prompt parts
- The command 'Return ONLY [format]' eliminates extra text around structure
- The simpler the schema, the more reliable the output — don't over-nest
In the next lesson, we dive into Chain-of-Thought: Step by Step — a technique that gives you a clear edge. Unlock the full course and continue now.
2/8 complete — keep going!