Open-Source Wikis

/

LangChain

/

Primitives

/

Output parsers

langchain-ai/langchain

Output parsers

Turn a model's output into a typed value. Source: libs/core/langchain_core/output_parsers/.

Purpose

A BaseOutputParser[T] is a Runnable[LanguageModelOutput, T]. Common implementations parse a string into structured data (JSON, Pydantic, XML), strip wrapping noise (markdown fences, list bullets), or fix invalid output by reprompting.

The classic LCEL chain prompt | model | parser produces a typed T at the end.

Directory layout

libs/core/langchain_core/output_parsers/
├── __init__.py
├── base.py                  # BaseOutputParser, BaseLLMOutputParser, BaseGenerationOutputParser
├── format_instructions.py   # Helpers for telling the model what shape to return
├── json.py                  # JsonOutputParser, SimpleJsonOutputParser
├── list.py                  # ListOutputParser, CommaSeparatedListOutputParser, NumberedListOutputParser, MarkdownListOutputParser
├── openai_functions.py      # JsonOutputFunctionsParser, PydanticOutputFunctionsParser
├── openai_tools.py          # JsonOutputToolsParser, PydanticToolsParser
├── pydantic.py              # PydanticOutputParser
├── string.py                # StrOutputParser
├── transform.py             # BaseTransformOutputParser (streaming-friendly)
└── xml.py                   # XMLOutputParser

Key abstractions

Symbol File Description
BaseOutputParser[T] libs/core/langchain_core/output_parsers/base.py Abstract base — defines parse(text) -> T, parse_with_prompt, get_format_instructions
BaseTransformOutputParser libs/core/langchain_core/output_parsers/transform.py Streams output as it arrives; what most production parsers extend
StrOutputParser libs/core/langchain_core/output_parsers/string.py The "just give me the string" parser — turns AIMessage into str
JsonOutputParser libs/core/langchain_core/output_parsers/json.py Parses JSON, optionally validates against a schema
PydanticOutputParser libs/core/langchain_core/output_parsers/pydantic.py Parses into a Pydantic model
XMLOutputParser libs/core/langchain_core/output_parsers/xml.py Parses XML (used by Claude prompt patterns)
JsonOutputToolsParser, PydanticToolsParser libs/core/langchain_core/output_parsers/openai_tools.py Pull tool calls out of an AIMessage and parse them
JsonOutputFunctionsParser, PydanticOutputFunctionsParser libs/core/langchain_core/output_parsers/openai_functions.py Legacy OpenAI-functions equivalents
OutputParserException libs/core/langchain_core/exceptions.py Raised when parsing fails; used by retry-on-parse-error patterns

How a parser fits into a chain

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field

class Person(BaseModel):
    name: str = Field(...)
    age: int = Field(...)

parser = PydanticOutputParser(pydantic_object=Person)

prompt = ChatPromptTemplate.from_messages([
    ("user", "Extract the person's data: {input}\n\n{format_instructions}"),
]).partial(format_instructions=parser.get_format_instructions())

chain = prompt | model | parser
chain.invoke({"input": "Alice is 30 years old."})  # Person(name="Alice", age=30)

get_format_instructions returns a string that tells the model how to format its output. Inserting it in the prompt is the standard pattern for free-form parsers.

Streaming parsers

BaseTransformOutputParser overrides transform so that, given a stream of AIMessageChunks, the parser yields partially-parsed values as they arrive. JsonOutputParser is streaming-aware: it uses a partial JSON parser to emit each top-level key as it completes.

This is what makes chain.astream({"input": "..."}) produce structured deltas instead of waiting for the whole output.

Tool-call parsers

For models that support tool calling, the cleanest way to get structured output is to bind a tool whose schema matches the desired type and parse the tool call:

from langchain_core.output_parsers import PydanticToolsParser

model_with_tool = model.bind_tools([Person])
chain = prompt | model_with_tool | PydanticToolsParser(tools=[Person])
chain.invoke(...)

This is what BaseChatModel.with_structured_output(method="function_calling") does internally.

Format instructions

Every parser exposes get_format_instructions() — a string the prompt should include so the model knows what shape to return. For Pydantic, it's the JSON schema with explanatory text. For lists, it's "Return a comma-separated list.". For XML, it's a sample tagged document.

Integration points

  • Every chain that returns a typed value ends with a parser.
  • with_structured_output on BaseChatModel selects an appropriate parser internally based on the chosen method.
  • OutputFixingParser (in langchain-classic) wraps a base parser and reprompts the model on failure — a cheap way to handle malformed output.

Entry points for modification

  • For a new parser, subclass BaseTransformOutputParser so streaming works, and place it in libs/core/langchain_core/output_parsers/.
  • For a new "fix-up" strategy, look at how OutputFixingParser in langchain-classic wraps a base parser.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Output parsers – LangChain wiki | Factory