Pydantic is a Python library that helps define data structures.
Acts like a “blueprint” for data
Uses Python’s type hints (like str, int) to enforce correct data types
How it works in LangChain/ LangGraph?
Define a class with the fields you need (name, capital, language)
Add descriptions to explain what each field means
Use with _structured_output()_ (Magic Method) to tell the LLM to follow your format
Pydantic
from pydantic import BaseModel, Field# from langchain_openai import ChatOpenAIfrom langchain_google_genai import ChatGoogleGenerativeAIimport 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 LLMclass 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_llmresponse = structured_llm.invoke("Tell me about France")print(response)
TypedDict
from typeing_extensions import Annotated, TypedDictfrom typing import Optional# TypedDictclass 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"]}