Skip to main content
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.
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.

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.
The embedded format instructions will look similar to this excerpt (JSON Schema-like):

Invoke the model and parse the response

Call the model with the formatted prompt string and capture the output:
A typical LLM response (the exact wrapper text depends on the model) containing JSON might be:
Pass the model output through the PydanticOutputParser to convert it into a typed Ticket instance:
Example parser output:

Field reference

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

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. A future article will cover adding short-term and long-term memory to LLMs.

Watch Video

Practice Lab