← Master Index
Vol. 13 Module 13.1 Lecture

Structured Output / Prompting

Prompting Techniques

How This Lesson Fits the Module & Volume

Module 13.1 moved from reasoning styles (chain of thought, reflection) and role prompting to a production concern: making model output machine-usable. Structured output prompting is the umbrella for forcing predictable shapes—fields, enums, schemas—so downstream code can parse without fragile string scraping.

This lecture frames the contract; the next three deepen the common carriers: JSON, XML, and Markdown.

Learning Objectives

By the end of this lesson, students should be able to:

  • Define structured output prompting and when it is required in product pipelines.
  • Specify schemas with required fields, types, and allowed values.
  • Choose among prompt-only structure, tool/function calling, and API JSON modes.
  • Validate and retry on parse or schema failures.
  • Avoid mixing free-form prose with strict structured payloads in one response.
  • Link structure choices to later prompt evaluation metrics.
Definition

Structured output prompting is the practice of instructing (and often constraining) a language model to emit responses in a predetermined machine-readable layout—such as JSON objects, XML trees, Markdown tables, or typed tool-call arguments—so parsers and business logic can consume results reliably.

Why Structure Beats Free Prose

Schema

Define fields & types.

Instruct

Put format in the prompt.

Constrain

API / grammar if available.

Validate

Parse, check, retry.

ApproachControlTypical use
Prompt-onlyInstructions + examplesQuick prototypes, any chat API
JSON / schema modeProvider enforces JSONExtraction, agents, APIs
Tool / function callingTyped argumentsActions, DB writes, workflows
Grammar / CFGToken-level constraintsStrict local or specialized stacks

Schema Design Checklist

Required

  • Field names & types
  • Enums for categories
  • “No extra keys” rule

Helpful

  • One worked example
  • Null / unknown policy
  • Max string lengths

Risky

  • Nested depth > 3
  • Ambiguous optional fields
  • Prose + JSON mixed

Practical Prompt + API Pattern

# Prompt fragment (system or user) """Extract ticket fields. Reply with ONLY a JSON object matching: { "priority": "low" | "medium" | "high", "product_area": string, "summary": string, // <= 140 chars "needs_human": boolean } No markdown fences. No commentary.""" # OpenAI-style chat call with JSON mode (illustrative) from openai import OpenAI client = OpenAI() resp = client.chat.completions.create( model="gpt-4o-mini", response_format={"type": "json_object"}, messages=[ {"role": "system", "content": SCHEMA_INSTRUCTIONS}, {"role": "user", "content": ticket_text}, ], ) import json data = json.loads(resp.choices[0].message.content) assert set(data) >= {"priority", "product_area", "summary", "needs_human"}

Strengths

  • Enables pipelines, UIs, and tools
  • Supports automated eval & logging
  • Reduces brittle regex parsing

Tradeoffs

  • Schema design is product work
  • Models can still invent keys
  • Over-strict schemas raise retries
Common Misconception

“Asking for JSON in the prompt guarantees valid JSON forever.” Prompting raises compliance rates; it does not replace validation. Always json.loads (or schema-validate), handle failure, and prefer provider JSON/schema modes or tool calling when available. Pair structure with guardrails for safety fields.

Knowledge Check

  1. Short Answer: What is structured output prompting? Answer: Instructing/constraining the model to emit a predetermined machine-readable layout.
  2. True/False: Free-form essays are ideal for writing database rows. Answer: False—parsers need stable fields.
  3. Multiple Choice: Tool/function calling primarily structures: (a) pixel colors, (b) typed arguments for actions, (c) GPU kernels. Answer: (b).
  4. Short Answer: Name one reason to forbid markdown fences around JSON. Answer: Fences break naive json.loads unless stripped.
  5. True/False: Schema validation should run after every structured call. Answer: True.
  6. Multiple Choice: Prompt-only structure is best for: (a) prototypes on any chat API, (b) hardware interrupts, (c) training CNNs. Answer: (a).
  7. Short Answer: What comes after Instruct in the four-step flow? Answer: Constrain (API/grammar), then Validate.
  8. Short Answer: Why keep enums in the schema? Answer: They limit categories to allowed values for safer routing.
  9. Multiple Choice: Mixing prose and JSON in one reply is: (a) usually risky for parsers, (b) required by transformers, (c) free. Answer: (a).
  10. True/False: Structured outputs make evaluation easier via field checks. Answer: True.

Key Takeaways

  • Structure turns LM text into product data.
  • Design schemas first; instruct, constrain, then validate.
  • Prefer JSON mode / tools over hope-based parsing.
  • Next: deepen the dominant format in JSON Prompting.
Trainer’s Guide

Hands-on: Give students a messy support email; have them design a 5-field schema and measure parse success over 20 runs.

Discussion: When is Markdown structure enough, and when must you graduate to JSON or tools?

Recap: Structured output prompting is the contract between models and code. Continue with JSON Prompting.