How to use PydanticOutputParser in LangChain
Quick answer
Use
PydanticOutputParser in LangChain by defining a pydantic.BaseModel schema for your expected output, then instantiate PydanticOutputParser with that model. Pass the parser's get_format_instructions() to your prompt and parse the AI response with parse() to get typed Python objects.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install langchain openai pydantic
Setup
Install the required packages and set your OpenAI API key in the environment.
pip install langchain openai pydantic Step by step
Define a Pydantic model for your structured output, create a PydanticOutputParser with it, include the parser's format instructions in your prompt, then parse the AI response to get a typed object.
import os
from pydantic import BaseModel
from langchain_openai import ChatOpenAI
from langchain.output_parsers import PydanticOutputParser
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
# Define your structured output model
class MovieInfo(BaseModel):
title: str
year: int
director: str
# Instantiate the parser with the Pydantic model
parser = PydanticOutputParser(pydantic_object=MovieInfo)
# Prepare prompt with format instructions
prompt = ChatPromptTemplate.from_messages([
HumanMessagePromptTemplate.from_template(
"""
Extract movie information in JSON format.
{format_instructions}
Movie description: {movie_desc}
"""
)
])
# Format instructions to tell the model how to output
format_instructions = parser.get_format_instructions()
# Create the final prompt
final_prompt = prompt.format_prompt(
movie_desc="The movie Inception was directed by Christopher Nolan and released in 2010.",
format_instructions=format_instructions
)
# Initialize LangChain ChatOpenAI client
chat = ChatOpenAI(model_name="gpt-4o", temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])
# Get the response
response = chat.invoke(final_prompt.to_messages())
# Parse the response content into the Pydantic model
movie_info = parser.parse(response.content)
print(movie_info)
print(f"Title: {movie_info.title}, Year: {movie_info.year}, Director: {movie_info.director}") output
title='Inception' year=2010 director='Christopher Nolan' Title: Inception, Year: 2010, Director: Christopher Nolan
Common variations
- Use
PydanticOutputParserwith other LLMs likeAnthropicby adapting the client call. - For async usage, call the async client methods and parse the output similarly.
- Customize the Pydantic model to include nested objects or optional fields for complex structured outputs.
Troubleshooting
- If parsing fails with validation errors, verify the model schema matches the expected output format.
- Ensure the prompt includes
parser.get_format_instructions()so the model outputs valid JSON. - Check that the AI response is well-formed JSON; if not, consider adding stricter prompt constraints or post-processing.
Key Takeaways
- Define a Pydantic model to specify the exact structured output schema.
- Use PydanticOutputParser to generate format instructions and parse AI responses into typed objects.
- Always include the parser's format instructions in your prompt to guide the model output.
- PydanticOutputParser works with any LangChain-compatible chat model client.
- Validate and troubleshoot by matching model schema and ensuring well-formed JSON output.