Structured Outputs & JSON Mode — Function Calling & Structured Outputs
Structured Outputs & JSON Mode
Type-Safe AI Outputs
When building applications, you need the AI to return data in a predictable format. OpenAI provides two mechanisms:
JSON Mode (Basic)
Setting response_format: { type: "json_object" } guarantees valid JSON output. You must still instruct the model about the schema in your prompt.
Structured Outputs (Strict - Recommended)
Introduced in late 2024 and natively supported across GPT-6 models, Structured Outputs mathematically constrains the model to only produce tokens valid under your JSON Schema using a Context-Free Grammar (CFG) engine (with first-class Pydantic responses.parse helpers in Python and Zod zodTextFormat helpers in TypeScript).
const response = await openai.responses.create({
model: "gpt-6.1-sol",
input: "Extract: John Doe, age 30, works at Acme Corp",
text: {
format: {
type: "json_schema",
name: "user_info",
strict: true,
schema: {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" },
company: { type: "string" }
},
required: ["name", "age", "company"],
additionalProperties: false
}
}
}
});
When to Use Each
| Mode | Guarantee | Best For |
|---|---|---|
| JSON Mode | Valid JSON (any structure) | Flexible, exploratory outputs |
| Structured Outputs | Exact schema match (100%) | Production data pipelines, type-safe integrations |
strict: true in production. JSON Mode is fine for prototyping but cannot guarantee schema compliance.Your parser crashes on free-text model output. Constrain the Responses API with a strict JSON Schema so invalid tokens are mathematically impossible, then extract a field with jq.