PydanticOutputParser
Author: Jaeho Kim
Peer Review: Donghak Lee, frimer
Proofread : BokyungisaGod
This is a part of LangChain Open Tutorial
Overview
This tutorial covers how to perform PydanticOutputParser using pydantic.
The PydanticOutputParser is a class that helps transform the output of a language model into structured information . This class can provide the information you need in a clear and organized form instead of a simple text response.
By utilizing this class, you can transform the output of your language model to fit a specific data model, making it easier to process and utilize the information
Main Method
A PydanticOutputParser primarily requires the implementation of two core methods.
get_format_instructions(): Provide instructions that define the format of the information that the language model should output. For example, you can return instructions as a string that describes the fields of data that the language model should output and how they should be formatted. These instructions are very important for the language model to structure the output and transform it to fit your specific data model.parse(): Takes the output of the language model (assumed to be a string) and analyzes and transforms it into a specific structure. Use a tool like Pydantic to validate the input string against a predefined schema and transform it into a data structure that follows that schema.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setups, useful functions, and utilities for tutorials.You can check out the
langchain-opentutorialfor more details.
Environment variables have been set successfully.
You can alternatively set API keys, such as OPENAI_API_KEY in a .env file and load them.
[Note] This is not necessary if you've already set the required API keys in previous steps.
Below is an example of an email conversation stored in the variable email_conversation .
Let's create a chain that utilizes the basic StrOutputParser.
Using the PydanticOutputParser
PydanticOutputParserWhen provided with email content like the one above, we can parse the email information using the class defined in the Pydantic style below.
For reference, the description inside the Field serves as guidance for extracting key information from text-based responses. LLMs rely on this description to extract the required information. Therefore, it is crucial that this description is accurate and clear.
Let's define a prompt including the following variables:
question: Receives the user's question.email_conversation: Inputs the content of the email conversation.format: Specifies the format.
Next, create a chain.
Execute the chain and review the results.
Finally, use the parser to parse the results and convert them into an EmailSummary object.
Creating a Chain Including the PydanticOutputParser
PydanticOutputParserYou can generate the output as a Pydantic object that you define.
with_structured_output()
with_structured_output()By adding an output parser using .with_structured_output(Pydantic), you can convert the output into a Pydantic object.
Note
One thing to note is that the .with_structured_output() method does not support the stream() method.
Last updated