Creative Genius Creative Genius
Lesson 2 of 4 · 22 min read

Structured Outputs & JSON Mode

Stop parsing markdown. Use the API features that guarantee valid JSON.

OpenAI, Anthropic, and Google all now offer structured outputs — pass a JSON schema and the model is mathematically constrained to produce valid JSON matching it.

Why this changes everything

Before structured outputs, ~3–8% of LLM responses had malformed JSON. Retries cost money and add latency. Today, with strict schema mode, that error rate approaches 0.

Code

import OpenAI from "openai";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";

const Lead = z.object({
  name: z.string(),
  email: z.string().email(),
  company: z.string().nullable(),
  intent: z.enum(["high", "medium", "low"]),
});

const client = new OpenAI();
const completion = await client.chat.completions.parse({
  model: "gpt-4o",
  messages: [
    { role: "system", content: "Extract lead details from the user's message." },
    { role: "user", content: "Hey it's Jane from Acme, jane@acme.io — we want to deploy AI Q1." }
  ],
  response_format: zodResponseFormat(Lead, "lead"),
});

const lead = completion.choices[0].message.parsed; // typed Lead
← System vs User Prompts Few-Shot Examples That Actually Work →