Key facts
- Structured outputs means the model has to fill a JSON Schema you supply, instead of free-form prose you then try to parse. OpenAI’s Structured Outputs guide describes this as constraining completed, non-refusal output to that schema.
- On the OpenAI API, the current documented path is the Responses API with a schema (in Python,
client.responses.parseand a Pydantic model; in HTTP, a JSON Schema withstrict: true). Your app still has to handle refusals, incomplete responses and API errors. - Strict schemas have rules: the root should be an object, required fields should be listed, and objects typically need
additionalPropertiesset tofalse. Optional values are usually a union withnull, not a missing key. - Local runtimes can do a lighter version of the same idea. Ollama accepts a
formatfield ("json"or a schema object). It is useful, but it is not the same guarantee as a provider-side constrained decoder.
If you have ever watched a script choke on a trailing comma, a missing quote, or a chatty preamble before the JSON, structured outputs are the fix. This post covers how to declare a schema, the limits that trip people up, and how to do a simpler version with a local model.

Start with a schema, not a stern prompt
A prompt that says “reply only with JSON” is a wish. A schema is a contract. You describe the object you want — field names, types, enums, required keys — and the API keeps the model’s completed output inside that shape.
OpenAI’s documented Python helper looks like this (the official example uses a current flagship model; swap in the model you actually call, such as gpt-6-luna or gpt-6.1-sol):
from openai import OpenAI
from pydantic import BaseModel
client = OpenAI()
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
response = client.responses.parse(
model="gpt-6-luna",
input=[
{"role": "system", "content": "Extract the event information."},
{
"role": "user",
"content": "Alice and Bob are going to a science fair on Friday.",
},
],
text_format=CalendarEvent,
)
event = response.output_parsed
The same idea in raw JSON Schema is an object with properties, a required list, and "additionalProperties": false. Libraries such as Pydantic and Zod generate that schema for you.
Two things the docs are explicit about:
- Refusals are not schema failures. Safety refusals are detectable as their own output type. Do not assume
output_parsedis always present. - The schema does not enforce your business rules. “Date must be in the future” still belongs in your own validation.
Schema design that actually works
A few patterns prevent most broken integrations:
- Keep the object flat when you can. Deeply nested schemas are harder to get right and harder to debug.
- Use enums for a fixed set of labels (
"open","pending","closed"), rather than hoping the model uses your exact wording. - Mark optional fields as a union with null, for example
{"type": ["string", "null"]}, if the API requires every property to appear inrequired. - Describe each field in the schema, not only in the prompt. The description is what the model uses to decide what belongs in that field.
- Don’t ask the schema to write an essay. If you need a long explanation plus a structured result, put the prose in one string field and the machine-readable bits in the others — or make two calls.
Token cost is real. A large schema is part of the prompt. Reuse the same schema across turns and take advantage of prompt caching when your provider supports it.
Local models: format is helpful, not magic
If you run models on your own machine, you can still ask for JSON. With Ollama’s HTTP API, set "format": "json", or pass a schema object in format, alongside your prompt. Other OpenAI-compatible local servers have their own flags; check that server’s docs rather than assuming the OpenAI Responses API is present.
What you should not expect is the same reliability as a frontier API with constrained decoding. Smaller local models drift, wrap JSON in markdown fences, or drop a required field. Always parse the response, and retry or repair on failure. For install and API setup, see our guides to Ollama settings and using a local model as an OpenAI-compatible API.
A practical local pattern:
curl http://localhost:11434/api/chat -d '{
"model": "gemma4:e2b",
"messages": [
{"role": "user", "content": "Extract the event: Alice and Bob go to a science fair on Friday."}
],
"format": {
"type": "object",
"properties": {
"name": {"type": "string"},
"date": {"type": "string"},
"participants": {"type": "array", "items": {"type": "string"}}
},
"required": ["name", "date", "participants"]
},
"stream": false
}'
Change the model tag to whatever you have pulled. Then parse the message.content string as JSON in your code — do not eval it.
Tool calling and structured outputs work together
Tool calling is how a model asks your code to act. Structured outputs are how it returns a final answer your code can ingest. Many apps need both: tools while the model gathers facts, then a schema-constrained object at the end.
Do not overload a single tool argument with your entire application state. Keep tool schemas small, and use a structured final response for the object you store or display.
Bottom line
Write a schema, send it with the request, and validate what comes back — including refusals and incomplete answers. On hosted APIs, use the provider’s structured-output feature rather than begging for JSON in the prompt. On local models, format helps, but your parser is still the last line of defence.
Sources: OpenAI Structured Outputs guide, Ollama API documentation. Facts checked November 19, 2026.
Cover image: OpenAI (official image)
