Why Structured Outputs?
Unstructured text is hard to process programmatically:Techniques
1. Explicit Format Instructions
Tell the agent exactly what structure you want:4. Template-Based Output
Provide a fill-in-the-blank template:Common Formats
Action Results
Analysis Reports
Task Status
Decision Records
Validation
Post-Processing
Retry on Invalid Output
Schema Validation with Zod
Prompt Patterns
The “Only JSON” Pattern
The “Strict Format” Pattern
The “Transform” Pattern
Anti-Patterns
Bad Prompt
Good Prompt
Best Practices
- Always provide complete schema or examples
- Validate output before using
- Implement retry logic for parsing failures
- Use “ONLY JSON” instructions explicitly
- Test with edge cases (empty results, errors)
- Include error format in schema
Related
Slop Constraints
Keep outputs clean and focused
Context Optimization
Better input = better structured output