LangChain Mastery

Course Content

LangChain Mastery

7 sections · 109 lessons

What is the role of output parsers in LangChain chains?


What you need to know

A chat model returns an AIMessage, not a string or a dict. Without a parser, the next step receives the whole message object.

ParserInput to outputUse when
StrOutputParserAIMessage to strFree text: summaries, replies
JsonOutputParsertext to dictJSON without strict validation; streams partial objects
PydanticOutputParsertext to Pydantic objectValidation when structured output is not available
with_structured_output(Schema)constrained generation to objectThe default for extraction on modern models

Format instructions

Python
from langchain_core.output_parsers import PydanticOutputParserparser = PydanticOutputParser(pydantic_object=Invoice)prompt = ChatPromptTemplate.from_messages([    ("system", "Extract the invoice fields.\n{format_instructions}"),    ("human", "{text}"),]).partial(format_instructions=parser.get_format_instructions())chain = prompt | llm | parser

get_format_instructions() returns text describing the JSON schema. The model reads it, writes JSON, and the parser validates it. This works with any model, but the model can still ignore the instructions.

Structured output is stronger

Python
chain = prompt | llm.with_structured_output(Invoice)

Here the schema goes to the provider as a JSON schema or tool definition, and generation is constrained to match. It fails far less often than parsing free text, and needs no format instructions in the prompt.

Streaming

StrOutputParser and JsonOutputParser both stream. JsonOutputParser yields growing partial dicts ({}, then {"vendor": "Acme"}, then the full object), so a UI can fill in fields as they arrive.

A real-life example

An invoice extraction chain first used JsonOutputParser with a prompt that said "Return JSON". About 2% of replies wrapped the JSON in a sentence ("Here is the extracted data: ...") or used a trailing comma, and the nightly job crashed on the first bad one. Moving to with_structured_output(Invoice) brought malformed output close to zero, and the Pydantic model caught the remaining value errors, such as a negative GST amount. The team kept StrOutputParser for the separate "explain this invoice" feature, where free text was what they wanted.

Follow-up questions to expect

  • "What happens when parsing fails?" — The parser raises OutputParserException; catch it, retry once with the error in the prompt, or send the case to review.
  • "What is OutputFixingParser?" — A wrapper that sends bad output back to a model to repair; it now lives in langchain_classic.output_parsers.
  • "Is a parser still needed with structured output?" — No; with_structured_output already returns the parsed object.