[ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Structured Outputs & JSON Mode

⚙️ OpenAI API & Code Integration 20 min 350 BASE XP

Forcing JSON Output

When building applications, you often need the AI to return data in a predictable format, not free-text. OpenAI provides two main ways to achieve this: JSON Mode and Structured Outputs.

JSON Mode

By setting response_format: { type: "json_object" }, you guarantee the model will output valid JSON. You must still instruct the model to use JSON in the system prompt.

Structured Outputs (Strict JSON Schema)

Introduced in late 2024, Structured Outputs guarantees the output will exactly match a JSON Schema you define. The model is mathematically constrained to only produce tokens valid under your schema.

const completion = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Extract info for John Doe, age 30." }],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "user_info",
      strict: true,
      schema: {
        type: "object",
        properties: {
          name: { type: "string" },
          age: { type: "number" }
        },
        required: ["name", "age"],
        additionalProperties: false
      }
    }
  }
});
SYNAPSE VERIFICATION
QUERY 1 // 2
What is the key advantage of 'Structured Outputs' over standard 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
Watch: 139x Rust Speedup
Structured Outputs & JSON Mode | OpenAI API & Code Integration — OpenAI Academy