[ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Structured Outputs & JSON Mode

⚙️ Function Calling & Structured Outputs 15 min 300 BASE XP⌨ HANDS-ON LAB

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, Structured Outputs mathematically constrains the model to only produce tokens valid under your JSON Schema. Uses a Context-Free Grammar (CFG) engine at the token generation level.

const response = await openai.responses.create({
  model: "o3-mini",
  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

ModeGuaranteeBest For
JSON ModeValid JSON (any structure)Flexible, exploratory outputs
Structured OutputsExact schema match (100%)Production data pipelines, type-safe integrations
🎯 Rule of Thumb: Always use Structured Outputs with strict: true in production. JSON Mode is fine for prototyping but cannot guarantee schema compliance.
⌨ HANDS-ON LABForce Schema-Perfect JSON
⭐ +150 XP

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.

1Call /v1/responses with text.format set to a strict json_schema (name it user_info).
2Pipe the response through jq to pull the guaranteed-valid JSON payload out of output_text.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 2 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 3
What is the key advantage of Structured Outputs over JSON Mode?
It uses less tokens
It guarantees the output perfectly matches a specific JSON schema you define
It automatically saves to a database
It generates HTML instead of JSON