0
Phase 6

Structured Outputs

~2 min read

Concept & How It Works

    Why Does It Exist?

    Loose JSON mode can return wrong keys or missing fields. Schema enforcement eliminates an entire class of validation bugs in agent pipelines.

    Real-World Analogy

    Structured outputs are a customs form with fixed boxes — name here, date there — not a blank page labeled 'JSON please.'
    Loading diagram...

    Visual Workflows

    What is Structured Outputs?

    Loading diagram...

    Example

    Scenario

    A ticket classifier must return {priority: 'high'|'medium'|'low', category: string, summary: string} — strict schema ensures no missing priority field.

    Solution

    In Tool Calling & Function Calling, apply Structured Outputs to this scenario: A ticket classifier must return {priority: 'high'|'medium'|'low', category: string, summary: string} — strict schema ensures no missing priority field. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to Structured Outputs (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where Structured Outputs happens in the code.

    Structured Outputs
    1from pydantic import BaseModel  # import dependencies2class Ticket(BaseModel):  # define a data structure or component3    priority: str4    category: str5    summary: str6
    7response = client.beta.chat.completions.parse(8    model="gpt-4o-mini",9    messages=[{"role": "user", "content": ticket_text}],10    response_format=Ticket,11)12ticket = response.choices[0].message.parsed

    Commands to Remember

    Commands to Remember

    • client.chat.completions.create(..., tools=[...]) # pass tool schemas to API
    • json.loads(response.choices[0].message.tool_calls[0].function.arguments) # parse tool args
    • pip install pydantic # validate tool inputs with schemas

    Common Mistakes

    • Treating Structured Outputs as a black box without evaluation
    • Ignoring cost and latency in production
    • Skipping error handling for structured outputs

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • Structured Outputs
    • Pydantic
    • Strict Schema
    • Output Parser