Structured JSON Outputs
Force reliable JSON from local models using the format flag and a JSON Schema for exact shapes.
TL;DR
- Pass
format: jsonto force a valid JSON object. - Constrain shape by passing a full
JSON Schemainformat. - Describe the exact keys you expect in the
SYSTEMprompt.
JSON Mode
format jsonForce the response to be a valid JSON object.
curl http://localhost:11434/api/generate -d '{
"model": "llama3.1",
"prompt": "List 2 colors as JSON",
"format": "json",
"stream": false
}'Prompt For KeysName the exact keys you want in the prompt.
"prompt": "Return name and age as JSON"Parse SafelyThe whole response body parses as JSON.
# response text is valid JSON to parseSchema-Constrained Output
format SchemaPass a JSON Schema object in the format field.
"format": { "type": "object", ... }type objectDefine the top-level type and properties.
{ "type": "object",
"properties": { } }requiredList required keys the model must include.
"required": ["name", "age"]Python Example
Pass formatSend a schema in the chat request's format.
ollama.chat(model="llama3.1",
messages=msgs, format=schema)Pydantic SchemaGenerate a schema from a Pydantic model.
schema = Person.model_json_schema()ValidateParse the reply back into your model.
Person.model_validate_json(reply)Determinism
temperature 0Zero temperature removes sampling randomness.
# options: { "temperature": 0 }Fixed seedA set seed repeats output for a prompt.
# options: { "seed": 42 }Schema + temp 0Combine both for repeatable structured output.
# fixed shape + deterministic valuesTips
- Pass a JSON Schema in
formatto pin exact keys and types, which is stricter and safer than plainformat: "json"alone. - Also name the required fields in the prompt, since telling the model what to produce plus a schema yields the most reliable output.
Warnings
- Plain
format: "json"guarantees valid JSON but not your shape; pass aJSON Schemawhen specific keys are required. - Ollama constrains output with a
JSON Schema, not GBNF grammar files; put the schema in theformatfield, not a grammar.
In Practice
Send a JSON Schema in the format field so the model returns exactly the keys your pipeline expects.
- The
formatfield carries a JSON Schema, not just the string json. propertiesfixes the exact keys and their types.requiredforces the model to include every listed key.stream: falsereturns one complete JSON object to parse.
curl http://localhost:11434/api/chat -d '{
"model": "llama3.1",
"messages": [
{"role":"user","content":"Alan Turing, 1912"}
],
"stream": false,
"format": {
"type": "object",
"properties": {
"name": {"type": "string"},
"born": {"type": "integer"}
},
"required": ["name", "born"]
}
}'FAQ
Add "format": "json" to the request. The model then returns a valid JSON object. For a specific shape, pass a JSON Schema in format instead of the plain string.
format: "json" only guarantees the output parses as JSON. A JSON Schema in format constrains the exact keys, types, and required fields, so the result matches a structure your code expects.
Ollama's public API constrains output with a JSON Schema passed in format, not GBNF grammar files. For structured data, define a schema rather than reaching for a raw grammar.
Loose format: "json" does not enforce keys. Pass a JSON Schema, set temperature to 0, and name the required fields in the prompt so a small model cannot drift from the shape.