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.
| Parser | Input to output | Use when |
|---|---|---|
StrOutputParser | AIMessage to str | Free text: summaries, replies |
JsonOutputParser | text to dict | JSON without strict validation; streams partial objects |
PydanticOutputParser | text to Pydantic object | Validation when structured output is not available |
with_structured_output(Schema) | constrained generation to object | The default for extraction on modern models |
Format instructions
1from langchain_core.output_parsers import PydanticOutputParser23parser = PydanticOutputParser(pydantic_object=Invoice)4prompt = ChatPromptTemplate.from_messages([5 ("system", "Extract the invoice fields.\n{format_instructions}"),6 ("human", "{text}"),7]).partial(format_instructions=parser.get_format_instructions())8chain = prompt | llm | parserget_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
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 inlangchain_classic.output_parsers. - "Is a parser still needed with structured output?" — No;
with_structured_outputalready returns the parsed object.