Search⌘ K
AI Features

Parsing Outputs with LangChain

Explore methods to parse and structure outputs from large language models using LangChain. Learn to use output parsers like CSV, JSON, and Pydantic as well as the simpler with_structured_output method to convert LLM text responses into usable, formatted data. Understand when to choose each approach for effective AI application development.

LLMs typically output a string of text. However, when creating an LLM-powered application, we often need a more structured, formatted output that delivers concise information instead of requiring us to read the complete response. There are two ways to get structured information out of a model today:

  1. Output parsers: They instruct the model (via prompt text) to produce output in a specific format, then we parse that string ourselves.

  2. .with_structured_output(): It asks the model to produce a schema-conforming object directly, using the provider’s native tool-calling or JSON-schema features.

Both are covered in this lesson. .with_structured_output() is the recommended default for nearly everything now, including single-value extraction, such as dates. We’ll see why once we’ve built the same example both ways. Manual output parsers from langchain_core still earn their place for a handful of narrower jobs: pulling plain text out of a message in an LCEL pipeline, or producing a format .with_structured_output() doesn’t target directly, like XML.

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.

NumberedListOutputParser

Returns a list parsed from a numbered list (1. foo\n2. bar). Same family as CommaSeparatedListOutputParser.

MarkdownListOutputParser

Returns a list parsed from a markdown bullet list (- foo\n- bar). Same family as CommaSeparatedListOutputParser.

JsonOutputParser

Returns a JSON object as specified. You can specify a Pydantic model, and it will return JSON for that model.

PydanticOutputParser

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

XMLOutputParser

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

Most parsers support two common methods:

  • ...