Structured Outputs
Revised
Note: Ollama's cloud currently doesn't1 support structured outputs.
Structured outputs let you enforce a JSON schema on model responses so you can reliably extract structured data, describe images, or keep every reply consistent.
Generate structured JSON2
cURL
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 in one line"}],
"stream": false,
"format": "json"
}'
Python
from ollama import chat
response = chat(
model='gpt-oss',
messages=[{'role': 'user', 'content': 'Tell me about Canada.'}],
format='json'
)
print(response.message.content)
JavaScript
import ollama from 'ollama'
const response = await ollama.chat({
model: 'gpt-oss',
messages: [{ role: 'user', content: 'Tell me about Canada.' }],
format: 'json'
})
console.log(response.message.content)
Generate structured JSON with a schema3
Provide a JSON schema to the format field.
Note: It's4 ideal to also pass the JSON schema as a string in the prompt to ground the model's response.
cURL
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."}],
"stream": false,
"format": {
"type": "object",
"properties": {
"name": {"type": "string"},
"capital": {"type": "string"},
"languages": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["name", "capital", "languages"]
}
}'
Python
Use Pydantic models and pass model_json_schema() to format, then validate the response:
from ollama import chat
from pydantic import BaseModel
class Country(BaseModel):
name: str
capital: str
languages: list[str]
response = chat(
model='gpt-oss',
messages=[{'role': 'user', 'content': 'Tell me about Canada.'}],
format=Country.model_json_schema(),
)
country = Country.model_validate_json(response.message.content)
print(country)
JavaScript
Serialize a Zod schema with z.toJSONSchema() and parse the structured response:
import ollama from 'ollama'
import * as z from 'zod'
const Country = z.object({
name: z.string(),
capital: z.string(),
languages: z.array(z.string()),
})
const response = await ollama.chat({
model: 'gpt-oss',
messages: [{ role: 'user', content: 'Tell me about Canada.' }],
format: z.toJSONSchema(Country),
})
const country = Country.parse(JSON.parse(response.message.content))
console.log(country)
Example: Extract structured data
Define the objects you want returned and let the model populate the fields:
from ollama import chat
from pydantic import BaseModel
class Pet(BaseModel):
name: str
animal: str
age: int
color: str | None
favorite_toy: str | None
class PetList(BaseModel):
pets: list[Pet]
response = chat(
model='gpt-oss',
messages=[{'role': 'user', 'content': 'I have two cats named Luna and Loki...'}],
format=PetList.model_json_schema(),
)
pets = PetList.model_validate_json(response.message.content)
print(pets)
Example: Vision with structured outputs
Vision models accept the same format parameter, enabling deterministic descriptions of images:
from ollama import chat
from pydantic import BaseModel
from typing import Literal, Optional
class Object(BaseModel):
name: str
confidence: float
attributes: str
class ImageDescription(BaseModel):
summary: str
objects: list[Object]
scene: str
colors: list[str]
time_of_day: Literal['Morning', 'Afternoon', 'Evening', 'Night']
setting: Literal['Indoor', 'Outdoor', 'Unknown']
text_content: Optional[str] = None
response = chat(
model='gemma4',
messages=[{
'role': 'user',
'content': 'Describe this photo and list the objects you detect.',
'images': ['path/to/image.jpg'],
}],
format=ImageDescription.model_json_schema(),
options={'temperature': 0},
)
image_description = ImageDescription.model_validate_json(response.message.content)
print(image_description)
Tips for reliable structured outputs
Notes on this page
1 Wording Audit: neutral Uses the contraction "doesn't".
Was:
does not supportContractionsAuditor: The contraction shortens the cloud-support note without changing its meaning.
2 Wording Audit: improved Starts the task heading with a verb.
Was:
## Generating structured JSONproc-imperativeAuditor: “Generate structured JSON” states the section’s task directly and is easier to scan.
3 Wording Audit: improved Starts the task heading with a verb.
Was:
## Generating structured JSON with a schemaproc-imperativeAuditor: “Generate structured JSON with a schema” states the section’s task directly and is easier to scan.
4 Wording Audit: neutral Uses the contraction "It's".
Was:
It is idealPronounContractionsAuditor: The contraction makes the note slightly shorter without changing its meaning.
5 Wording Audit: improved Uses "for example" instead of "e.g.".
Was:
e.g.,LatinAuditor: “For example” replaces an abbreviation with plain language.
6 Wording Audit: improved Says the feature works "with" the API and reads "through" in place of "via".
Was:
work through the OpenAI-compatible API viaLatinAuditor: “Work with the OpenAI-compatible API” reads more plainly while preserving the stated API relationship.
7 Wording Audit: improved Uses "through" instead of "via".
Was:
viaLatinAuditor: “Through” replaces “via” with a plainer word.
8 Wording Audit: improved Adds the period the other tips have, so the list punctuates one way.
list-terminal-punctuationAuditor: The period makes the final tip consistent with the other list items.
Questions for the owner
X22 Cloud limits on OpenAI compatibility omit structured outputs. Cloud and Structured Outputs say the cloud does not support structured outputs, Structured Outputs says they work through
response_format, and OpenAI compatibility's list of cloud limits does not mention it. Should OpenAI compatibility listresponse_formatstructured outputs among the cloud limits?Pages: Cloud, Structured Outputs, OpenAI compatibility
Original
Note: Ollama's cloud currently does not support structured outputs.
Structured outputs let you enforce a JSON schema on model responses so you can reliably extract structured data, describe images, or keep every reply consistent.
Generating structured JSON
cURL
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 in one line"}],
"stream": false,
"format": "json"
}'
Python
from ollama import chat
response = chat(
model='gpt-oss',
messages=[{'role': 'user', 'content': 'Tell me about Canada.'}],
format='json'
)
print(response.message.content)
JavaScript
import ollama from 'ollama'
const response = await ollama.chat({
model: 'gpt-oss',
messages: [{ role: 'user', content: 'Tell me about Canada.' }],
format: 'json'
})
console.log(response.message.content)
Generating structured JSON with a schema
Provide a JSON schema to the format field.
Note: It is ideal to also pass the JSON schema as a string in the prompt to ground the model's response.
cURL
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."}],
"stream": false,
"format": {
"type": "object",
"properties": {
"name": {"type": "string"},
"capital": {"type": "string"},
"languages": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["name", "capital", "languages"]
}
}'
Python
Use Pydantic models and pass model_json_schema() to format, then validate the response:
from ollama import chat
from pydantic import BaseModel
class Country(BaseModel):
name: str
capital: str
languages: list[str]
response = chat(
model='gpt-oss',
messages=[{'role': 'user', 'content': 'Tell me about Canada.'}],
format=Country.model_json_schema(),
)
country = Country.model_validate_json(response.message.content)
print(country)
JavaScript
Serialize a Zod schema with z.toJSONSchema() and parse the structured response:
import ollama from 'ollama'
import * as z from 'zod'
const Country = z.object({
name: z.string(),
capital: z.string(),
languages: z.array(z.string()),
})
const response = await ollama.chat({
model: 'gpt-oss',
messages: [{ role: 'user', content: 'Tell me about Canada.' }],
format: z.toJSONSchema(Country),
})
const country = Country.parse(JSON.parse(response.message.content))
console.log(country)
Example: Extract structured data
Define the objects you want returned and let the model populate the fields:
from ollama import chat
from pydantic import BaseModel
class Pet(BaseModel):
name: str
animal: str
age: int
color: str | None
favorite_toy: str | None
class PetList(BaseModel):
pets: list[Pet]
response = chat(
model='gpt-oss',
messages=[{'role': 'user', 'content': 'I have two cats named Luna and Loki...'}],
format=PetList.model_json_schema(),
)
pets = PetList.model_validate_json(response.message.content)
print(pets)
Example: Vision with structured outputs
Vision models accept the same format parameter, enabling deterministic descriptions of images:
from ollama import chat
from pydantic import BaseModel
from typing import Literal, Optional
class Object(BaseModel):
name: str
confidence: float
attributes: str
class ImageDescription(BaseModel):
summary: str
objects: list[Object]
scene: str
colors: list[str]
time_of_day: Literal['Morning', 'Afternoon', 'Evening', 'Night']
setting: Literal['Indoor', 'Outdoor', 'Unknown']
text_content: Optional[str] = None
response = chat(
model='gemma4',
messages=[{
'role': 'user',
'content': 'Describe this photo and list the objects you detect.',
'images': ['path/to/image.jpg'],
}],
format=ImageDescription.model_json_schema(),
options={'temperature': 0},
)
image_description = ImageDescription.model_validate_json(response.message.content)
print(image_description)
Tips for reliable structured outputs
- Define schemas with Pydantic (Python) or Zod (JavaScript) so they can be reused for validation.
- Lower the temperature (e.g., set it to
0) for more deterministic completions. - Structured outputs work through the OpenAI-compatible API via
response_format
What Changed
Fixed the wording checks (contractions, "for example" and "through" in place of Latin abbreviations and "via") and turned the two headings into verbs. The last tip now ends with a period like the other tips. The cloud statement conflicts with the closing tip about response_format, so that point carries the inventory question.
Content type: task. The page shows how to get JSON, and JSON that matches a schema, out of a model, which is one job done with a request.
Automated check, original: 0 errors, 2 warnings, 2 suggestions. Revised: 0 errors, 0 warnings, 0 suggestions.
Independent audit (GPT-6): improved. The revision makes several small wording and heading improvements while preserving the page’s meaning. I found no material regression; the noted cloud and API statements describe different contexts, so they do not necessarily conflict.
Wording (8)
- 1 Uses the contraction "doesn't". neutral
- 2 Starts the task heading with a verb. improved
- 3 Starts the task heading with a verb. improved
- 4 Uses the contraction "It's". neutral
- 5 Uses "for example" instead of "e.g.". improved
- 6 Says the feature works "with" the API and reads "through" in place of "via". improved
- 7 Uses "through" instead of "via". improved
- 8 Adds the period the other tips have, so the list punctuates one way. improved