Parsing Outputs with LangChain

Learn how to parse the output of an LLM as structured information using output parsers in the LangChain framework.

LLMs typically output a string of text. However, when creating an LLM-powered application, we might need a more structured, formatted output that delivers concise information instead of requiring us to read the complete response.

Output parsers

Parsers are a tool that can help us get a structured output. If we don’t use parsers for our responses, then the expected output will be plain text as a string. LangChain provides us with different types of parsers. All parsers take either a string or a Message as input.

The output depends on the type of parser being used. Let’s explore them:

Parser Type

Details

StrOutputParser

Parses texts from message objects. Useful for handling variable formats of message content (e.g., extracting text from content blocks).

CommaSeparatedListOutputParser

Returns a list of comma-separated values.

DatetimeOutputParser

Parses the response into a datetime string.

EnumOutputParser

Parses response into one of the provided enum values.

JsonOutputParser

Returns a JSON object as specified. You can specify a Pydantic model, and it will return JSON for that model. It is one of the most reliable output parsers for returning structured data without using function calls.

OutputFixingPasrser

Wraps another output parser. If that output parser errors, then this will pass the error message and the bad output to an LLM and ask it to fix the output.

PandasDataFrameOutputParser

Useful for doing operations with pandas DataFrames.

PydanticOutputParser

Takes a user-defined Pydantic model and returns data in that format.

RetryWithErrorOutputParser

Wraps another output parser. If that output parser errors, then this will pass the original inputs, the bad output, and the error message to an LLM and ask it to fix it. Compared to OutputFixingParser, this one also sends the original instructions.

StructuredOutputParser

An output parser that returns structured information. It is less powerful than other output parsers since it only allows for fields to be strings. This can be useful when you are working with smaller LLMs.

XMLOutputParser

Returns a dictionary of tags. Use when xml output is needed, and use it with models that are good at writing xml (like Anthropic's).

YAMLOutputParser

Takes a user-defined Pydantic model and returns data in that format. Uses yaml to encode it.

Most parsers support two common methods:

  • get_format_instructions(): This method returns the formatting instructions in string format. The model then uses this information to format the output according to our requirements.

  • parse(): This method parses the string output from the model and returns an object of the required type.

For the sake of simplicity, we’ll only discuss the Datetime, CSV, and Pydantic parsers in detail.

Datetime parser

The DatetimeOutputParser converts the date and time in a string to a datetime object in ...