> ## 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.

# Parsing Model Output Demo 3

> Explains using a Pydantic output parser to convert LLM responses into typed Python objects

In this lesson you'll learn how to use a Pydantic-based output parser to convert LLM responses into a typed Python object. Using Pydantic (via LangChain utilities), you declare a schema with Python type annotations and then validate/parse JSON-like model output into instances of that schema—giving you robust guarantees about structure and types when working with LLM-generated data.

Key concepts covered:

* Defining a Pydantic model for the expected response.
* Creating a `PydanticOutputParser`.
* Embedding parser format instructions in the prompt so the LLM returns JSON matching the schema.
* Invoking the model and parsing the output into a typed Python object.

## Complete example

Below is a concise, runnable example showing the full workflow: define a model, create a parser, add format instructions to the prompt, call the model, and parse the response.

```python theme={null}
# imports
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

# initialize model
model = OpenAI()

# define the Pydantic model for a ticket reservation
class Ticket(BaseModel):
    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")

# create the Pydantic output parser
parser = PydanticOutputParser(pydantic_object=Ticket)

# prompt template with placeholders for the movie query and parser format instructions
ticket_template = '''
Book us a movie ticket for two this Friday at 6:00 PM.
Choose any theater, it doesn't matter. 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()},
)
```

<Callout icon="lightbulb" color="#1CB2FE">
  This example shows how the parser provides `format_instructions` (a JSON Schema-like specification). Including these instructions in your prompt guides the LLM to emit well-formed JSON that the parser can validate and load into a Pydantic model.
</Callout>

## Notes on imports and model invocation

Import paths and how you call the model can differ between LangChain versions and wrappers. Common variations:

* `from langchain import OpenAI` vs `from langchain_openai import OpenAI`
* `BaseModel` and `Field` may come directly from `pydantic`
* Model invocation can use `model.invoke(...)`, `model.predict(...)`, or `model(...)` depending on the wrapper

Be sure to consult your installed LangChain / OpenAI wrapper docs if you hit import or invocation errors.

## Build the final prompt

Supply a movie name (for example, `Interstellar`) and render the prompt. The `format_instructions` partial will contain the JSON Schema-like instructions the model should follow.

```python theme={null}
input = prompt.format_prompt(query="Interstellar")
print(input.to_string())
```

The embedded format instructions will look similar to this excerpt (JSON Schema-like):

```json theme={null}
{
  "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"]
}
```

## Invoke the model and parse the response

Call the model with the formatted prompt string and capture the output:

```python theme={null}
output = model.invoke(input.to_string())
```

A typical LLM response (the exact wrapper text depends on the model) containing JSON might be:

```json theme={null}
{
  "date": "Friday",
  "time": "6:00 PM",
  "theater": "AMC",
  "count": 2,
  "movie": "Interstellar"
}
```

Pass the model output through the `PydanticOutputParser` to convert it into a typed `Ticket` instance:

```python theme={null}
reservation = parser.parse(output)
print(reservation)
print(type(reservation))
```

Example parser output:

```text theme={null}
Ticket(date='Friday', time='6:00 PM', theater='AMC', count=2, movie='Interstellar')
<class '__main__.Ticket'>
```

## Field reference

| Field     | Type      | Description       | Example        |
| --------- | --------- | ----------------- | -------------- |
| `date`    | `string`  | Show date         | `Friday`       |
| `time`    | `string`  | Show time         | `6:00 PM`      |
| `theater` | `string`  | Theater name      | `AMC`          |
| `count`   | `integer` | Number of tickets | `2`            |
| `movie`   | `string`  | Preferred movie   | `Interstellar` |

## Why this pattern is useful

* You get strong guarantees about the shape and types of the data your application receives from an LLM.
* Parsing into a Pydantic model makes downstream processing, validation, and IDE/autocomplete support straightforward.
* Embedding `parser.get_format_instructions()` in the prompt aligns the LLM output to the schema, reducing parsing errors.

<Callout icon="lightbulb" color="#1CB2FE">
  The parser validates and converts JSON to an instance of the `Ticket` model. If the model's output does not conform to the schema, the parser will raise a validation error—so including `format_instructions` in the prompt is important to guide the LLM toward valid output.
</Callout>

## Summary

Steps to follow:

1. Define a Pydantic model (subclass of `BaseModel`) describing the expected fields and types.
2. Create a `PydanticOutputParser` using that Pydantic model.
3. Include `parser.get_format_instructions()` as a partial variable in your prompt template.
4. Invoke the LLM with the formatted prompt.
5. Use `parser.parse(...)` to convert the model output into a typed Python object.

This approach makes it straightforward to work with LLM-generated data in a type-safe way and reduces runtime errors caused by unexpected response formats.

## Links and references

* LangChain Output Parsers: [https://langchain.readthedocs.io/](https://langchain.readthedocs.io/)
* Pydantic: [https://pydantic-docs.helpmanual.io/](https://pydantic-docs.helpmanual.io/)
* OpenAI API / SDK docs: [https://platform.openai.com/docs](https://platform.openai.com/docs)

A future article will cover adding short-term and long-term memory to LLMs.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/ae260750-791b-496c-991f-0d0333f61e40/lesson/31aecd1b-7ea9-4bab-898d-b8bc6b35ab62" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/ae260750-791b-496c-991f-0d0333f61e40/lesson/932a32b6-0fa6-445f-b1f6-7db4e21dc369" />
</CardGroup>
