tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > LangChain > LangChain Output Parsers

LangChain Output Parsers

Author: Venkata Sudhakar

By default, LangChain chains return raw text from the LLM. Output parsers transform that raw text into structured Python objects - a list, a dictionary, a Pydantic model, or a boolean. This is essential when your downstream code needs to work with the LLM output programmatically rather than just display it. Instead of writing custom string parsing for each use case, you declare the structure you want and let the parser handle conversion and validation.

LangChain provides several built-in parsers. StrOutputParser simply returns the text as a string - useful at the end of most chains. PydanticOutputParser takes a Pydantic model class, adds formatting instructions to your prompt automatically, and validates the LLM response against the model. CommaSeparatedListOutputParser extracts a plain comma-separated list into a Python list. JsonOutputParser extracts JSON objects. The parser plugs into an LCEL chain with the pipe operator after the LLM call.

The below example shows extracting a structured migration assessment from LLM output using PydanticOutputParser, so the result is a validated Python object instead of raw text.


It gives the following output,


Risk level: HIGH
Risks: ["200 stored procedures need conversion to PL/pgSQL",
        "4-week timeline is aggressive for this complexity",
        "Oracle-specific SQL syntax may not translate automatically"]
Days: 60
Advice: Extend timeline to 60 days and use AWS SCT for stored procedure conversion.

# result is a validated Pydantic object - not a string to parse manually
# result.risks is already a Python list
# result.estimated_days is already an int

It gives the following output,


1. Verify row counts match between source and target
2. Confirm all foreign key constraints are intact
3. Test application queries against target database
4. Check CDC lag is zero before switching connections
5. Validate index performance on critical query paths

If the LLM returns badly formatted output, PydanticOutputParser raises an OutputParserException. Wrap the chain with OutputFixingParser.from_llm(parser, llm=llm) to automatically retry with the malformed output and ask the LLM to fix its own formatting error - this adds resilience without custom error handling code.


 
  


  
bl  br