How do I make sure the AI always returns valid JSON?
Updated
Attach a structured output to your workflow. It's a JSON schema, and every response is checked against it before it reaches your app. If a model returns empty or invalid output, ModelRiver tries your backup models, so your code only ever receives JSON in the shape you defined, or a clear error.
How do I set one up?#
- Go to Structured Outputs in the console and create a schema, for example
summary(string),sentiment(enum) andpriority(number). - Add sample data. It's what test mode returns.
- Attach the schema to one or more workflows.
Will ModelRiver fill in missing fields?#
No, never. If a field is missing or a value is the wrong type, that attempt fails and the next backup model is tried. If every attempt fails, you get status: "error". ModelRiver never invents data to make a response pass.
Are there schema features I can't use?#
patternandpatternPropertiesaren't accepted.format(likeemailordate) is accepted but not enforced, so validate it in your app if it matters.
What if a model keeps failing the schema?#
- Simplify the schema: fewer nested objects, clearer field names.
- Describe each field in the schema or in the system instructions.
- Use a more capable model as the primary or as a backup.
- Look at the raw response in Request Logs to see what the model actually returned.