Mod 2.2Implementing Structured Outputs with JSON and Pydantic
Level 2›Module 2.2
Level 2: Core Implementation & WorkflowsModule 2.2

Implementing Structured Outputs with JSON and Pydantic

Structured Outputs

Level 2 • Core Implementation & Workflows
Est. ~15 mins
5 Key Topics
🎁 Free Learner Perk

Unlock Verified Certificate & Daily Streak Tracker

Ready to master Implementing Structured Outputs with JSON and Pydantic? Enable cloud sync to record your daily streak 🔥 and earn your Informational Completion Badge for your study milestones.

Day 1 Streak ActiveFree Completion BadgeSync Laptop & Phone

🎯 By the end of this module, you will:

Define Pydantic BaseModel schemas that force LLM output into structured types
Use with_structured_output() to bind schemas to any LangChain model
Add Field validators (ge, le, Literal) to enforce business logic at the LLM layer
Handle ValidationError exceptions and implement structured retry patterns
Schema Engineering • Type Safety

Structured Outputs with Pydantic

Free-form LLM text is useless downstream. Pydantic + with_structured_output turns any model into a typed data extractor — no regex, no brittle JSON parsing.

1. Schema Design: Field DefinitionsStep 1
# Step 1: Define output schema as a Pydantic model
from pydantic import BaseModel, Field
from typing import Literal

class SentimentAnalysis(BaseModel):
    sentiment: Literal["positive", "negative", "neutral"]
    confidence: float = Field(ge=0.0, le=1.0)
    issue_type: str = Field(description="Category of the user's issue")
    requires_escalation: bool

📋 Mental Model: Unstructured vs Structured

❌ Free-Form Text Output

"The sentiment is negative and confidence around 90%. The issue seems to be latency."

Cannot write to DB. Cannot trigger API. Requires brittle regex.

✅ Pydantic Structured Output
{"sentiment": "negative",
 "confidence": 0.90,
 "requires_escalation": true}

Directly writable to PostgreSQL. Zero parsing code.

🔬 Schema Builder Visualizer

Pydantic & JSON Schema VisualizerType Safety

Configure schema fields and observe how Python type hints convert into enforceable JSON Schema for LLMs

Model Field Schema4 Fields Defined
summarystr

A one-sentence summary of the text.

sentimentstr

One of: positive, negative, neutral.

confidencefloat

Confidence score between 0.0 and 1.0.

action_itemslist[str]

Key actionable takeaways.

from pydantic import BaseModel, Field
from typing import Optional, List

class IncidentReport(BaseModel):
    summary: str = Field(
        description="A one-sentence summary of the text."
    )
    sentiment: str = Field(
        description="One of: positive, negative, neutral."
    )
    confidence: float = Field(
        description="Confidence score between 0.0 and 1.0."
    )
    action_items: Optional[list[str]] = Field(
        description="Key actionable takeaways."
    )
✍️ Instructor Note: "Always use temperature=0 with structured outputs. Any temperature > 0 can cause the LLM to randomly deviate from your schema constraints — especially for Literal and constrained float fields!"
💡 Mental Model: Think of Pydantic like a customs officer at an airport. Every piece of luggage (LLM output) must match the declared manifest (schema) — wrong type, wrong format, or missing field? Confiscated! ValidationError raised.
📌 Core Rule: Write Field() descriptions in your schema like instructions to the LLM, not documentation for developers. "The severity score from 1 to 10 where 10 means immediate P0 outage" is far more reliable than "severity score".

Structured Output Traps

TRAP #1: Ambiguous Field Descriptions

A field named score: float with no description will cause the model to guess what to put there. Always include a Field() description that specifies the exact meaning, range, and expected values.

TRAP #2: Deeply Nested Optional Fields

Complex nested schemas with many Optional fields confuse smaller models. Flatten your schema to 1-2 levels. For complex extractions, break into multiple sequential structured calls.

Key Takeaways

  • 1.Schema-First Design: Define your Pydantic model before writing the prompt. The schema IS the interface contract between your LLM and your application code.
  • 2.Field() Descriptions are LLM Instructions: Write field descriptions as if explaining to the LLM exactly what value to put there — with examples and boundary conditions.
  • 3.Catch ValidationError Early: Wrap every structured output call in try/except. Log the field-level Pydantic errors and implement an automatic retry with a clarifying prompt.
Up Next • Module 2.3

Integrating External Tools into an Agent

Connect Tavily Search, calculator tools, and external APIs to give your agent real-world access — and learn the golden rules of tool selection.

Continue to Module 2.3