JSON Schema
The schema file should be a valid JSON Schema. It is passed to OpenRouter’s response_format parameter to enforce structured output from the LLM.
Basic Structure
Section titled “Basic Structure”{ "type": "object", "properties": { "field1": { "type": "string" }, "field2": { "type": "integer" } }, "required": ["field1", "field2"], "additionalProperties": false}Supported Types
Section titled “Supported Types”JSON Schema supports several data types:
string- Text valuesinteger- Whole numbersnumber- Decimal numbersboolean- True/false valuesarray- Lists of valuesobject- Nested objects
Examples
Section titled “Examples”Person Extraction Schema
Section titled “Person Extraction Schema”{ "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" }, "company": { "type": "string" } }, "required": ["name", "age", "company"], "additionalProperties": false}Sentiment Analysis Schema
Section titled “Sentiment Analysis Schema”{ "type": "object", "properties": { "sentiment": { "type": "string", "enum": ["positive", "negative", "neutral"] }, "confidence": { "type": "number", "minimum": 0, "maximum": 1 } }, "required": ["sentiment", "confidence"], "additionalProperties": false}Entity Extraction with Arrays
Section titled “Entity Extraction with Arrays”{ "type": "object", "properties": { "people": { "type": "array", "items": { "type": "object", "properties": { "name": { "type": "string" }, "role": { "type": "string" } }, "required": ["name", "role"] } }, "organizations": { "type": "array", "items": { "type": "string" } } }, "required": ["people", "organizations"], "additionalProperties": false}Best Practices
Section titled “Best Practices”- Use
additionalProperties: false- This ensures the LLM only outputs the fields you specify - Mark required fields - Use the
requiredarray to specify which fields must be present - Use enums for constrained values - When you need specific values, use
enum - Add constraints - Use
minimum,maximum,minLength,maxLengthfor validation