Skip to main content

Documentation Index

Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt

Use this file to discover all available pages before exploring further.

Learn how to validate and convert LLM outputs into Python objects using LangChain’s PydanticOutputParser. In this tutorial, we will:
  1. Define a Pydantic model for a movie ticket.
  2. Generate prompt templates including format instructions.
  3. Inspect the complete prompt JSON schema.
  4. Call the LLM and get a structured response.
  5. Parse the response into a Ticket instance.
  • Python 3.8+
  • langchain, pydantic installed
  • An OpenAI API key configured as OPENAI_API_KEY

1. Imports & Model Definition

Begin by importing the required modules and defining a Pydantic model for a movie ticket:
from typing import List
from langchain_openai import OpenAI
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain.prompts import PromptTemplate
from langchain.output_parsers import PydanticOutputParser

model = OpenAI()
class Ticket(BaseModel):
    """Schema for a movie ticket reservation."""
    date:    str = Field(description="show date")
    time:    str = Field(description="show time")
    theater: str = Field(description="theater name")
    count:   int = Field(description="number of tickets")
    movie:   str = Field(description="preferred movie")

Ticket Model Fields

FieldTypeDescription
datestringShow date
timestringShow time
theaterstringTheater name
countintegerNumber of tickets
moviestringPreferred movie title

2. Initialize the Parser & Build the Prompt

Create a PydanticOutputParser from the Ticket model. Then define a PromptTemplate that embeds the JSON schema instructions:
# Initialize the parser
parser = PydanticOutputParser(pydantic_object=Ticket)

# Template with a placeholder for {query} and format instructions
ticket_template = """\
Book us a movie ticket for two this Friday at 6:00 PM.
Choose any theater. Send the confirmation by email.
Our preferred movie is: {query}

Format instructions:
{format_instructions}
"""

prompt = PromptTemplate(
    template=ticket_template,
    input_variables=["query"],
    partial_variables={"format_instructions": parser.get_format_instructions()},
)
The parser.get_format_instructions() method injects a JSON Schema under “Format instructions.” The LLM must follow this schema exactly.

3. Inspect the Full Prompt

Format the prompt for the movie “Interstellar” and print it:
formatted_prompt = prompt.format_prompt(query="Interstellar")
print(formatted_prompt.to_string())
Example output:
Book us a movie ticket for two this Friday at 6:00 PM.
Choose any theater. Send the confirmation by email.
Our preferred movie is: Interstellar

Format instructions:
The output should be formatted as a JSON instance that conforms to the JSON Schema below.

As an example, for the schema
{"properties": {"foo": {"title":"Foo","description":"a list of strings","type":"array","items":{"type":"string"}}},"required":["foo"]}
the object {"foo":["bar","baz"]} is valid.
Here is the output schema:

{"properties":{
  "date":    {"title":"Date","description":"show date","type":"string"},
  "time":    {"title":"Time","description":"show time","type":"string"},
  "theater": {"title":"Theater","description":"theater name","type":"string"},
  "count":   {"title":"Count","description":"number of tickets","type":"integer"},
  "movie":   {"title":"Movie","description":"preferred movie","type":"string"}
},"required":["date","time","theater","count","movie"]}

4. Invoke the LLM

Send the formatted prompt to the model and capture its raw response:
llm_output = model.invoke(formatted_prompt.to_string())
print("LLM Response:\n", llm_output)

Example Raw Response

{
  "date": "Friday",
  "time": "6:00 PM",
  "theater": "AMC Lincoln Square",
  "count": 2,
  "movie": "Interstellar"
}
Ensure the LLM response is valid JSON matching the schema. Invalid or malformed JSON will cause parsing errors.

5. Parse into a Python Object

Finally, hand the LLM output back to PydanticOutputParser for validation and conversion:
reservation = parser.parse(llm_output)
print("Reservation object:", reservation)
print("Type:", type(reservation))
Output:
Reservation object: Ticket(date='Friday', time='6:00 PM', theater='AMC Lincoln Square', count=2, movie='Interstellar')
Type: <class 'Ticket'>

Conclusion

By leveraging PydanticOutputParser, you can:
  • Instruct the LLM to generate schema-compliant JSON.
  • Automatically validate and convert responses into Python objects.
This method ensures structured, reliable LLM outputs in your applications.

Watch Video

Practice Lab