Skip to main content

Output Parsers

An output parser sits at the end of a chain and reshapes a model's raw output into something your code can use.

from langchain_core.output_parsers import JsonOutputParser, StrOutputParser

chain = prompt | model | StrOutputParser() # AIMessage -> str
json_chain = prompt | model | JsonOutputParser() # AIMessage -> dict, parsed from prose
ParserInput assumptionOutput
StrOutputParserany AIMessage.content as a plain string
JsonOutputParsermodel was prompted to produce JSONparsed dict
Pydantic-based parsingmodel was prompted to match a schemavalidated Pydantic instance
Pitfalls

A parser can only reshape what the model already produced — it cannot force the model to produce valid JSON in the first place. If the model returns prose with a stray sentence before the JSON block, or omits a field, the parser raises or silently drops data. Treat parsing as a fallible post-processing step, not a guarantee.

For anything where a malformed response is a real cost — extraction, tool arguments, anything downstream code trusts — prefer Structured Output, which constrains the model itself rather than parsing after the fact.

See also