Structured Outputs
Learn how to use structured outputs with JSON schema validation for reliable data extraction and API integration.
Structured Outputs
Structured outputs allow you to instruct AI models to format their responses according to a specific JSON schema. By providing clear schema instructions in your prompts, models will attempt to return properly formatted JSON that can be parsed programmatically.
Universal Support: All chat models on Tinfoil can be instructed to return structured JSON outputs through carefully crafted prompts, enabling data extraction across different models.
Benefits
- Consistent Formatting: Models are instructed to follow your specified JSON schema
- Type Safety: Request specific data types, required fields, and constraints
- Simplified Parsing: Parse JSON responses directly from chat completions
- API Integration: Perfect for building APIs that need consistent response formats
- Data Extraction: Extract structured information from unstructured text
Basic Example
Here’s how to use structured outputs to extract contact information:
Complex Schema Example
For more complex data structures with nested objects and arrays:
Data Analysis Example
Perfect for analyzing and structuring data from text:
Model Recommendations
Different models excel at structured outputs based on their training data and architecture. Models trained on code, JSON examples, and structured data formats tend to perform better at following precise schemas and generating valid JSON. Training methodology, model size, and fine-tuning for instruction following all impact how well a model can understand formatting requirements, maintain consistency across complex nested structures, and reliably produce parseable outputs.
Model | Best For | Structured Output Quality |
---|---|---|
Qwen 2.5 72B | Complex schemas, data processing, API responses | Excellent |
Mistral Small 3.1 24B | Simple to moderate schemas, fast responses | Very Good |
DeepSeek R1 70B | Analytical data, research outputs | Very Good |
Llama 3.3 70B | Content analysis, conversational data | Good |
Best Practices
- Clear Instructions: Provide explicit JSON schema examples in your prompts
- Use Descriptive Field Names: Choose field names that clearly indicate their purpose
- Handle Nullable Fields: Specify which fields can be null in your schema instructions
- Set Appropriate Types: Request specific types (string, number, boolean) in your prompts
- Temperature Control: Use lower temperature values (0.1-0.3) for more consistent JSON formatting
- Handle Errors: Always wrap JSON parsing in try-catch blocks
- Use Nullish Coalescing: Handle null values gracefully with
??
operator
Error Handling
Use Cases
- API Development: Create consistent API responses
- Data Extraction: Extract structured data from documents
- Form Processing: Parse form submissions and surveys
- Content Analysis: Analyze reviews, feedback, and comments
- Report Generation: Create structured reports from unstructured data
- Database Integration: Generate database-ready records
- Workflow Automation: Structure data for automated processing