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}
More Related questions...