Pydantic Models for Structured Outputs:

  1. Pydantic is a Python library that helps define data structures.
  2. Acts like a “blueprint” for data
  3. Uses Python’s type hints (like str, int) to enforce correct data types

How it works in LangChain/ LangGraph?

  1. Define a class with the fields you need (name, capital, language)
  2. Add descriptions to explain what each field means
  3. Use with _structured_output()_ (Magic Method) to tell the LLM to follow your format

Pydantic

from pydantic import BaseModel, Field
# from langchain_openai import ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI
import os
 
# llm = ChatOpenAI(model="gpt-4o")
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", api_key=os.getenv("GOOGLE_API_KEY"))
 
# This will be considered as tool for the LLM
class Country(BaseModel):
	"""Information about a country"""
	name: str = Field(description="name of the country")
	language: str = Field(description="language of the country")
	capital: str = Field(description="Capital of the country")
 
structured_llm = llm.with_structured_output(Country)
structured_llm
 
response = structured_llm.invoke("Tell me about France")
print(response)
 

TypedDict

from typeing_extensions import Annotated, TypedDict
from typing import Optional
 
# TypedDict
class Joke(TypedDict):
    """Joke to tell user."""
 
    # Annotated[type, default_value, description]
    setup: Annotated[str, ..., "The setup of the joke"]
 
    # Alternatively, we could have specified setup as
 
    # setup: str                      # no default, no description
    # setup: Annotated[str, ...]      # no default, no description
    # setup: Annotated[str, "foo"]    # default, no description
 
    punchline: Annotated[str, ..., "The punchline of the joke."]
    rating: Annotated[Optional[int], None, "How funny the joke is, from 1 to 10"]
 
structured_llm = llm.with_structured_output(Joke)
 
response = structured_llm.invoke("Tell me a joke about cats.")
print(response)
 

json_schema

{
  "title": "Joke",
  "description": "Joke to tell user.",
  "type": "object",
  "properties": {
    "setup": {
      "type": "string",
      "description": "The setup of the joke"
    },
    "punchline": {
      "type": "string",
      "description": "The punchline of the joke."
    },
    "rating": {
      "type": ["integer", "null"],
      "description": "How funny the joke is, from 1 to 10"
    }
  },
  "required": ["setup", "punchline"]
}