Skip to content

Structured Output

Medium — good to knowAI & ML

ELI5 — The Vibe Check

Structured output means getting the AI to respond in a specific format — like JSON or a filled-in form — instead of just freeform text. It's the difference between the AI writing a paragraph about a user and returning a clean JSON object with name, email, and role fields. It makes AI outputs machine-readable, so your code can actually USE the response without parsing poetry.

Real Talk

Structured output constrains LLM responses to follow a specific schema, typically JSON matching a predefined structure. Modern APIs support this via JSON mode, function calling schemas, or grammar-constrained generation. This ensures programmatic reliability — outputs can be parsed, validated, and consumed by downstream code without string manipulation or error-prone extraction.

Show Me The Code

// Structured output with OpenAI
const response = await openai.chat.completions.create({
  model: "gpt-4o",
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "user",
      schema: {
        type: "object",
        properties: {
          name: { type: "string" },
          age: { type: "integer" },
          role: { type: "string" }
        }
      }
    }
  },
  messages: [{ role: "user", content: "Extract: John, 32, engineer" }]
});

When You'll Hear This

"Use structured output so we can parse the model's response reliably." / "Structured output eliminated all our JSON parsing errors."

Made with passive-aggressive love by manoga.digital. Powered by Claude.