Skip to content

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.

{
"type": "object",
"properties": {
"field1": { "type": "string" },
"field2": { "type": "integer" }
},
"required": ["field1", "field2"],
"additionalProperties": false
}

JSON Schema supports several data types:

  • string - Text values
  • integer - Whole numbers
  • number - Decimal numbers
  • boolean - True/false values
  • array - Lists of values
  • object - Nested objects
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"company": { "type": "string" }
},
"required": ["name", "age", "company"],
"additionalProperties": false
}
{
"type": "object",
"properties": {
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"]
},
"confidence": {
"type": "number",
"minimum": 0,
"maximum": 1
}
},
"required": ["sentiment", "confidence"],
"additionalProperties": false
}
{
"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
}
  1. Use additionalProperties: false - This ensures the LLM only outputs the fields you specify
  2. Mark required fields - Use the required array to specify which fields must be present
  3. Use enums for constrained values - When you need specific values, use enum
  4. Add constraints - Use minimum, maximum, minLength, maxLength for validation