Structured Outputs & Schema Control
From the course Advanced Prompt Engineering: Master AI Communication
When building applications that consume AI output programmatically, you need structured, parseable responses — not free-form text.
JSON Output Prompting
The most common structured format. Be explicit about the schema:
"Analyze this customer review and return a JSON object with exactly these keys: { "sentiment": "positive" | "negative" | "neutral", "topics": ["array of discussed topics"], "urgency": 1-5, "summary": "one sentence summary", "action_required": true | false } Return ONLY the JSON, no other text."
Tool Calling / Function Calling
Modern APIs (OpenAI, Anthropic, Google) support native function calling. Instead of asking the model to output JSON and parsing it yourself, you define a function schema and the model returns structured arguments. This is more reliable than raw JSON prompting.
XML for Complex Structures
For responses with nested, mixed-format content, XML tags work well:
"Return your analysis in this format: <analysis>
<summary>Brief overview</summary> <findings> <finding severity='high|medium|low'>Description</finding> </findings> <recommendation>Action items</recommendation> </analysis>"
Markdown Tables
For comparative data, request markdown tables:
"Compare these three products. Return a markdown table with columns: Feature, Product A, Product B, Product C"
Common Pitfalls
- Don't mix instructions with schema — Put the schema definition at the end, after all context
- Use examples — Show one complete example of the expected output format
- Handle edge cases — Tell the model what to output when data is missing ("use null for unknown fields")
- Validate outputs — Always validate the returned structure in your code; models can still produce malformed output
Production Pattern: Parse with Fallback
In production code, always implement a fallback:
- Try to parse the structured output
- If parsing fails, retry with a more explicit prompt
- If retry fails, log the raw output and handle gracefully
Key Takeaways
- •Be explicit about JSON schemas with exact keys, types, and allowed values
- •Function/tool calling is more reliable than raw JSON prompting
- •XML tags work well for complex nested structures
- •Always show an example of the expected output format
- •Validate structured outputs in code and implement retry/fallback logic
Progress is kept in this browser only.