New Tool Radar

Local AI

How to Get JSON Back from a Local Ollama Model with Structured Outputs

Ask a local Ollama model for JSON with a schema in the format field, so the reply has exactly the fields your code expects.

Schematic diagram with three steps: plain JSON mode with format json, adding a schema object in format, then checking the reply has name and capital as strings and languages as a list.

Asking a model to “reply in JSON” usually works, until it adds a friendly sentence before the JSON, forgets a field, or wraps everything in markdown, and the script reading the reply breaks. Ollama’s structured outputs page says structured outputs let you enforce a JSON schema on model responses, so you can reliably extract structured data or keep every reply consistent. The schema travels in the format field of the request, and Ollama constrains the reply to match it.

JSON mode and a schema are two different promises

Ollama offers two levels. Setting "format": "json" asks for any valid JSON object, with no promise about which fields it contains. Passing a JSON schema object as format asks for JSON with exactly the fields and types in the schema. Use plain JSON mode for a quick test, and a schema whenever code will read the reply.

What you send What Ollama promises When to use it
"format": "json" The reply is a well-formed JSON object A quick test where any shape will do
A JSON schema object in format The reply matches the schema’s fields and types Code that reads named fields from the reply

Try it: ask about Canada and check the three fields

Send the request below with curl. It uses the schema from Ollama’s own structured outputs page: an object with a string name, a string capital, and a list of strings languages, all three required. The page also says it is ideal to pass the JSON schema as a string in the prompt, to ground the model’s response, so the prompt repeats the shape.

curl -X POST http://localhost:11434/api/chat -H "Content-Type: application/json" -d '{
"model": "gpt-oss",
"messages": [{"role": "user", "content": "Tell me about Canada. Reply with JSON matching this schema: {\"name\": string, \"capital\": string, \"languages\": [string]}."}],
"stream": false,
"format": {
"type": "object",
"properties": {
"name": {"type": "string"},
"capital": {"type": "string"},
"languages": {"type": "array", "items": {"type": "string"}}
},
"required": ["name", "capital", "languages"]
}
}'

Check on the reply What you should see
It parses as JSON A JSON parser accepts the reply with no error, and there is no extra text outside it
All three fields are there name, capital and languages are all present
The types match name and capital are strings and languages is a list, as in the docs’ own example answer for Canada

Expected result. We checked Ollama’s structured outputs page and its blog post on structured outputs. We did not send this request or run a model on this machine.

Limits

A schema constrains the shape of the reply, not whether the facts inside it are right: the model can still return a wrong capital in perfectly valid JSON, so check values, not just fields. Ollama’s structured outputs page also says its cloud does not currently support structured outputs, so this is a local-server feature. For more reliable results the same page advises defining schemas with Pydantic in Python or Zod in JavaScript so they can be reused for validation, and lowering the temperature, for example to 0, for more deterministic completions. We did not test any of this ourselves.

Sources

See all