# JSON Schema

> The JSON Schema is a vocabulary for describing the structure of JSON data, used to define tools and structured outputs for AI models.

## How do I write a JSON Schema for LLM tools and outputs?

The format for describing the shape of JSON data: types, required fields and allowed values. In AI applications, it defines the parameters of tools and the format of structured outputs that the model must follow.

- Topic: APIs & Integrations
- Type: format

## Example

Wrong:

```json
{
  "type": "object",
  "properties": {
    "city": { "type": "string" },
    "unit": { "type": "string" }
  }
}
```

Right:

```json
{
  "type": "object",
  "properties": {
    "city": { "type": "string", "description": "City name, e.g. 'Paris'" },
    "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
  },
  "required": ["city", "unit"],
  "additionalProperties": false
}
```

Descriptions, `enum`, 'required' and 'additionalProperties' make the model fill the right fields with allowed values.

## Common mistakes

- Forgetting 'required', so the model leaves out important fields.

## Key points

- Required fields are listed in 'required', and unknown fields are blocked with 'additionalProperties': false.
- Fields with fixed values use 'enum' instead of free text.
- Each property has a 'description' that explains its meaning and format.
- Data is still validated in code after the model returns it.

## Interview questions

### Do all JSON Schema features work with LLM structured outputs?

No. Strict structured output modes often support only a subset, for example requiring 'additionalProperties': false and every field in 'required'.

### Can I generate JSON Schema from TypeScript types?

Yes, schema libraries like Zod can produce JSON Schema and also validate the model's response at runtime, keeping one source of truth.

### How do I make a field optional in strict structured outputs?

When every field must be listed in 'required', make it nullable with a type like ['string', 'null'] and treat null as missing.

## Related cards

- [Structured Output](https://wonderium.dev/ai/structured-output.md)
- [Tool Calling](https://wonderium.dev/ai/tool-calling.md)
- [MCP Tool](https://wonderium.dev/ai/mcp-tool.md)
- [.json](https://wonderium.dev/engineering/json.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/json-schema
- Last updated: 2026-11-25
