AI / LangGraph LangChain Interview questions
What are output parsers in LangChain?
Output parsers sit at the end of a chain and transform the raw text or message returned by an LLM into a more structured or usable form. Without a parser, chain.invoke() returns an AIMessage object; with a parser, you get a plain string, a Python dict, a validated Pydantic model, or a list — whatever your downstream code expects.
The most common parsers:
- StrOutputParser — extracts
.contentfrom an AIMessage, returns a string. Used in virtually every chain:prompt | model | StrOutputParser() - JsonOutputParser — parses the model's text as JSON and returns a Python dict. Works best when the prompt instructs the model to return valid JSON.
- PydanticOutputParser — validates parsed JSON against a Pydantic schema. The parser injects format instructions into the prompt automatically via
parser.get_format_instructions(). - CommaSeparatedListOutputParser — splits a comma-delimited response into a Python list.
- StructuredOutputParser — uses a JSON schema for more flexible structured output.
from langchain_core.output_parsers import JsonOutputParser
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
parser = JsonOutputParser(pydantic_object=Person)
chain = prompt | model | parser
result = chain.invoke({"query": "John is 30 years old"})
# result: {'name': 'John', 'age': 30}
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
