Why structured outputs?
Raw AI responses are unpredictable. Different providers return different formats, and even the same model can vary between requests. Structured outputs solve this by enforcing a JSON schema on every response, giving your application a contract it can rely on.
How it works
- Define a schema: Create a JSON schema in the ModelRiver console under Structured Outputs.
- Attach to a workflow: Link the schema to one or more workflows.
- Automatic validation: ModelRiver validates every AI response against your schema and merges results so required fields are always present.
- Fallback synthesis: If a provider is down or times out, ModelRiver can synthesise a schema-compliant fallback, flagged via
meta.offline_fallback = true.
Creating a structured output
- Navigate to Structured Outputs in your project sidebar
- Click Create Structured Output
- Define your JSON schema with required fields, types, and descriptions
- Add sample data that conforms to the schema (used for test mode)
- Save and attach to workflows
Schema example
JSON
1{2 "type": "object",3 "required": ["summary", "sentiment", "category"],4 "properties": {5 "summary": {6 "type": "string",7 "description": "A concise summary of the input"8 },9 "sentiment": {10 "type": "string",11 "enum": ["positive", "negative", "neutral"],12 "description": "Overall sentiment classification"13 },14 "category": {15 "type": "string",16 "description": "Topic category of the content"17 },18 "confidence": {19 "type": "number",20 "minimum": 0,21 "maximum": 1,22 "description": "Model confidence score"23 }24 }25}Best practices
- Include examples in the schema to guide providers. ModelRiver passes these as context to improve compliance.
- Start simple: Begin with a minimal schema and add fields as your application matures.
- Use enums for categorical fields to constrain values and reduce parsing errors.
- Monitor validation: Check Observability for validation failure rates and adjust schemas accordingly.
- Leverage fallbacks: The
meta.offline_fallbackflag lets your application distinguish between real and synthesised responses.
Offline fallback behaviour
When all configured providers fail, ModelRiver can return a schema-compliant response using your sample data:
- The response is flagged with
meta.offline_fallback = true - Your application can detect this and show appropriate UI (e.g. "Results may be approximate")
- This ensures your application never crashes due to missing AI responses
Next steps
- Type-safe AI Solution: Why and how to use structured data in production.
- Test mode: Use sample data to validate integrations
- Build a workflow: Attach structured outputs to workflows
- API response formats: Understand response structure