Ollama Structured Output
Ollama structured output forces a model to reply with JSON that matches a schema you supply. This makes it safe to parse model output in code, for example when extracting data from text.
JSON Mode
Setting "format": "json" guarantees valid JSON but not a specific shape. Always ask for JSON in the prompt too.
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Return the city and country of the Eiffel Tower as JSON.",
"format": "json",
"stream": false
}'{"response":"{\"city\": \"Paris\", \"country\": \"France\"}", "done":true}
(Sample response.)JSON Schema Format
Pass a full JSON schema in format to control exact field names and types:
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"stream": false,
"messages": [{"role":"user","content":"Ali is 29 and lives in Lahore."}],
"format": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"city": {"type": "string"}
},
"required": ["name", "age", "city"]
},
"options": {"temperature": 0}
}'"content": "{\"name\": \"Ali\", \"age\": 29, \"city\": \"Lahore\"}"
(Sample response.)With Pydantic in Python
from ollama import chat
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
city: str
resp = chat(
model="llama3.2",
messages=[{"role": "user", "content": "Sara, 34, works in Dubai."}],
format=Person.model_json_schema(),
options={"temperature": 0},
)
person = Person.model_validate_json(resp.message.content)
print(person)name='Sara' age=34 city='Dubai'
Tips for Reliable JSON
- Use temperature 0 for extraction tasks.
- Describe the fields in the prompt as well as in the schema.
- Validate the result (Pydantic, Zod) and retry on failure.
- Smaller models follow schemas less accurately on complex nested data.
Frequently asked questions
What is the difference between JSON mode and structured outputs?
JSON mode only guarantees syntactically valid JSON. Structured outputs constrain the reply to a specific JSON schema.
Can I use Zod with Ollama?
Yes. Convert your Zod schema to JSON Schema (for example with zod-to-json-schema or Zod 4's built-in conversion) and pass it as format.
Why does my model still return wrong values?
The schema controls structure, not truth. The model can still misread the input, so validate and test with real examples.