Structured Output

Structured output prompts tell the model exactly what format to return — JSON, tables, specific fields. Essential for building AI applications that chain outputs to the next step.

March 30, 20264 min read1 / 6

When you need the same format every time, you don't describe it — you show it. Structured output prompting tells the model exactly what shape to return data in, and it does.

This is the technique that moves prompt engineering from "chat" to "production."

Why Format Consistency Matters

In a chat UI, variable output format is fine — you can read whatever the model gives you. In an AI application, variable format breaks everything downstream.

Plain text
// If I always get JSON, I can do this: const { errorCode, timestamp, endpoint } = parsedOutput; // If output format varies, I don't know what I'm receiving: // "Error 404 occurred at 3:45pm when hitting /user/profile" (string) // { code: 404, time: "15:45", path: "/user/profile" } (JSON) // - Error 404\n- Time: 3:45pm\n- Path: /user/profile (markdown list)

LLMs don't fail the way traditional code fails. They don't throw errors — they hallucinate and return unexpected formats. Structured output prompting makes unexpected formats detectable and handleable.

The Simplest Form: "Format Like This"

You don't need complex schema definitions. Just show the format:

Prompt:

Plain text
Extract the meeting details from this email and format them like this: Date: [date] Time: [time] Location: [location] Topic: [topic] Email: "Let's meet tomorrow at 2pm in Conference Room B to discuss the Q4 budget."

Output:

Plain text
Date: Tomorrow Time: 2pm Location: Conference Room B Topic: Q4 budget discussion

The model returns exactly the fields you asked for, in exactly the layout you specified. You can now parse this reliably.

JSON Output

For AI applications, JSON is usually what you want:

Prompt:

Plain text
Convert this error into JSON. Return only the JSON, no explanation. Error: 404 occurred at 3:45pm when trying to access /user/profile endpoint.

Output:

JSON
{ "errorCode": 404, "timestamp": "3:45pm", "endpoint": "/user/profile", "message": "404 error occurred at 3:45pm when accessing /user/profile" }

Adding "Return only the JSON, no explanation" costs about 5 extra tokens and eliminates all the wrapping text that would make parsing harder.

Practical Example: Metadata Tracking System

Here's a structured output prompt for a real feature — adding a metadata tracking system to a Prompt Library app. Notice how the output schema is explicitly defined:

Plain text
Create a metadata tracking system for a Prompt Library web application. Function specifications: 1. trackModel(modelName: string, content: string, metadata: object) - Accept any non-empty string for model name - Auto-generate a timestamp (createdAt) - Estimate tokens from content (base: 0.75 × word count) - Code content: multiply estimate by 1.3 - Confidence scores: high (<1000 tokens), medium (1000-5000), low (>5000) 2. updateTimestamp(metadata: object) - Update the updatedAt field - Validate updatedAt is after createdAt Validation rules: - All dates must be valid ISO 8601 strings - Model name must be non-empty string, max 100 characters - Throw descriptive errors for invalid inputs Output schema (each stored prompt): { id: string, title: string, content: string, metadata: { model: string, createdAt: ISO string, updatedAt: ISO string, tokenEstimate: { min: number, max: number, confidence: "high" | "medium" | "low" } } } Visual display: Create an HTML/CSS component that shows model name, timestamps, and token estimate with color-coded confidence (green = high, yellow = medium, red = low). Constraints: Pure JavaScript, no external libraries, browser-compatible, include try/catch error handling.

The explicit output schema means the model knows exactly what fields to create, what types they should be, and what validation to apply.

When Structured Output Is Most Useful

ScenarioWhy it helps
AI application pipelinesOutput feeds into next step — format must be predictable
Batch processingExtracting data from many inputs into a consistent structure
Code generationSpecify which files, what functions, what data shapes
Data extractionParsing emails, errors, logs into structured records
DocumentationConsistent format across many generated docs

Combining with Other Techniques

Structured output works best when combined with other techniques:

Plain text
// Structured output + few-shot: show 2 examples of input → output, // then the model knows both the pattern AND the format. // Structured output + chain-of-thought: ask it to think step-by-step, // but deliver the final answer in a specific JSON format.

Practice: Try It Yourself

Exercise 1: Extract structured data from freeform text

Plain text
Extract the following fields from this job posting and return as JSON. Return only JSON, no explanation. Fields: role, company, location, salary_range (or null if not listed), required_years_experience (number or null), remote (true/false/null) Job posting: [paste any job posting text]

Exercise 2: Generate consistent code documentation

Plain text
Generate a JSDoc comment for this function. Use exactly this format: /** * [One sentence description] * * @param {type} name - description * @returns {type} description * @example * functionName(arg) // expected output */ Function: function calculateDiscount(price, percentage) { return price * (1 - percentage / 100); }

Exercise 3: Error to JSON pipeline Copy 3–5 error messages from your app's logs. Ask the model to extract structured data from each one using a consistent JSON schema you define. Notice how reliable the output is compared to asking for a plain explanation.

Enjoyed this? Get more like it.

Deep dives on system design, React, web development, and personal finance — straight to your inbox. Free, always.