How to Intermediate · 4 min read

How to use PydanticOutputParser in LangChain

Quick answer
Use PydanticOutputParser in LangChain to parse AI-generated text into typed Pydantic models, ensuring structured and validated output. Define a Pydantic model for your expected output schema, then instantiate PydanticOutputParser with it and pass it to your chain or prompt to automatically parse responses.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install langchain pydantic openai>=1.0

Setup

Install the required packages and set your OpenAI API key in the environment variables.

  • Install LangChain and Pydantic: pip install langchain pydantic openai
  • Set your API key in your shell: export OPENAI_API_KEY='your_api_key_here' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key_here" (Windows)
bash
pip install langchain pydantic openai

Step by step

Define a Pydantic model for the expected output, create a PydanticOutputParser with it, then use it with a LangChain prompt and OpenAI chat model to parse the response automatically.

python
import os
from pydantic import BaseModel
from langchain_openai import ChatOpenAI
from langchain.output_parsers import PydanticOutputParser
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate

# Define your Pydantic model for structured output
class UserInfo(BaseModel):
    name: str
    age: int
    email: str

# Create the output parser
parser = PydanticOutputParser(pydantic_object=UserInfo)

# Create a prompt template that instructs the model to respond in JSON matching the model
prompt = ChatPromptTemplate.from_messages([
    HumanMessagePromptTemplate.from_template(
        """
        Provide user information in JSON format matching this schema:
        {parser.get_format_instructions()}
        User: John Doe, 30 years old, email john.doe@example.com
        """
    )
])

# Initialize the chat model
chat = ChatOpenAI(model="gpt-4o", temperature=0)

# Format the prompt
messages = prompt.format_messages(parser=parser)

# Get the response
response = chat(messages=messages)

# Parse the response content into the Pydantic model
user_info = parser.parse(response.content)

print(user_info)
print(f"Name: {user_info.name}, Age: {user_info.age}, Email: {user_info.email}")
output
name='John Doe' age=30 email='john.doe@example.com'
Name: John Doe, Age: 30, Email: john.doe@example.com

Common variations

You can use PydanticOutputParser with different models like Anthropic Claude or Google Gemini by adapting the client and prompt accordingly. Async usage is supported by calling async methods on the chat client. You can also customize the prompt instructions to fit your schema and use streaming if supported by the model.

python
import os
import asyncio
from pydantic import BaseModel
from langchain_openai import ChatOpenAI
from langchain.output_parsers import PydanticOutputParser
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate

class Product(BaseModel):
    id: int
    name: str
    price: float

async def main():
    parser = PydanticOutputParser(pydantic_object=Product)
    prompt = ChatPromptTemplate.from_messages([
        HumanMessagePromptTemplate.from_template(
            f"Provide product info in JSON format matching this schema:\n{parser.get_format_instructions()}\nProduct: id=101, name=Widget, price=19.99"
        )
    ])

    chat = ChatOpenAI(model="gpt-4o", temperature=0)
    messages = prompt.format_messages(parser=parser)
    response = await chat.agenerate(messages=[messages])
    product = parser.parse(response.generations[0][0].message.content)
    print(product)

if __name__ == "__main__":
    asyncio.run(main())
output
id=101 name='Widget' price=19.99

Troubleshooting

  • If parsing fails with validation errors, ensure the model output strictly matches the Pydantic schema and JSON format.
  • Use parser.get_format_instructions() in your prompt to guide the model to output correctly formatted JSON.
  • If you get incomplete or malformed JSON, try lowering temperature or adding explicit instructions to output valid JSON only.
  • Check your API key environment variable if you get authentication errors.

Key Takeaways

  • Define a Pydantic model to specify the expected structured output schema.
  • Use PydanticOutputParser with LangChain prompts to parse AI responses into typed models automatically.
  • Include parser.get_format_instructions() in your prompt to ensure the model outputs valid JSON.
  • PydanticOutputParser supports sync and async usage with different chat models.
  • Validate your environment variables and model output format to avoid parsing errors.
Verified 2026-04 · gpt-4o
Verify ↗