In the post Structured Output In LangChain we saw how to use structured output to get response from LLM in structured format. In this tutorial we’ll see how to do the same thing using output parsers in LangChain.
Output from LLMs, by default is a free-form text. In order to get formatted output from LLMs, LangChain provides many OutputParser classes. Though the newer way of using structured output to get reliable, schema-validated JSON is preferred but initially many LLMs didn’t have native support for producing structured output. Output parsers emerged as an early solution to obtain structured output from LLMs.
Output parsers are still required when working with models that do not support structured output natively, or when you require lightweight parsing, additional processing or validation of the model's output beyond its inherent capabilities.
OutputParser Classes in LangChain
LangChain provides many OutputParser classes that parse the output of an LLM call into structured data. There are variety of OutputParser classes because different use cases demand different ways of interpreting and structuring the raw text returned by an LLM. Some of the common OutputParser classes are listed below with examples. Note that all of the OutputParser classes have method get_format_instructions() to align the model’s output with the parser’s expectations.
1. StrOutputParser
It extracts the text content from a model message and returns it as a plain string, making it easy to chain with other components.
from langchain_core.prompts import ChatPromptTemplate
model = ChatOllama(model="llama3.1")
history_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a history expert."),
("human", "{query}"),
]
)
chain_history = history_prompt | model | StrOutputParser()
2. CommaSeparatedListOutputParser
Designed to parse the output of a model to a comma-separated list.
LangChain CommaSeparatedListOutputParser example
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
from langchain_core.output_parsers import CommaSeparatedListOutputParser
from langchain_ollama import ChatOllama
model = ChatOllama(model="llama3.1")
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()
system_message = SystemMessagePromptTemplate.from_template("You are an expert {field} analyst")
human_message = HumanMessagePromptTemplate.from_template("List 5 important trends in {field}. \n{format_instructions}")
prompt = ChatPromptTemplate.from_messages([system_message, human_message]).partial(format_instructions=format_instructions)
chain = prompt | model | output_parser
result = chain.invoke({"field": "AI"})
print(result)
partial method is used to get a new ChatPromptTemplate with some input variables already filled in. Since format_instructions is static (always coming from the parser), so it can be set as a partial variable.
3. JsonOutputParser
Parses the model's output into a JSON object.
LangChain JsonOutputParser example
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain_ollama import ChatOllama
from pydantic import BaseModel
# Define a schema for the JSON output
class Trend(BaseModel):
name: str
description: str
model = ChatOllama(model="llama3.1")
output_parser = JsonOutputParser(pydantic_object=Trend)
# Get format instructions from the parser
format_instructions = output_parser.get_format_instructions()
system_message = SystemMessagePromptTemplate.from_template("You are an expert {field} analyst")
human_message = HumanMessagePromptTemplate.from_template(
"List one important trend in {field} for 2026.\n{format_instructions}"
)
# Create ChatPromptTemplate with partial variable for format_instructions
prompt = ChatPromptTemplate.from_messages([system_message, human_message]).partial(
format_instructions=format_instructions
)
chain = prompt | model | output_parser
result = chain.invoke({"field": "AI"})
print(result)
Output
{'name': 'Increased Adoption of Edge AI', 'description': 'More organizations will implement AI models on edge devices to reduce latency and improve real-time processing capabilities'}
4. PydanticOutputParser
Uses Pydantic models to define and enforce a strict schema. It provides the most robust validation and type safety.
LangChain PydanticOutputParser example
from langchain_core.output_parsers import JsonOutputParser, PydanticOutputParser
from pydantic import BaseModel
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
from langchain_ollama import ChatOllama
class Trend(BaseModel):
name: str
description: str
output_parser = PydanticOutputParser(pydantic_object=Trend)
# Get format instructions from the parser
format_instructions = output_parser.get_format_instructions()
model = ChatOllama(model="llama3.1")
system_message = SystemMessagePromptTemplate.from_template("You are an expert {field} analyst")
human_message = HumanMessagePromptTemplate.from_template(
"List one important trend in {field} for 2026.\n{format_instructions}"
)
# Create ChatPromptTemplate with partial variable for format_instructions
prompt = ChatPromptTemplate.from_messages([system_message, human_message]).partial(
format_instructions=format_instructions
)
chain = prompt | model | output_parser
result = chain.invoke({"field": "AI"})
print(result)
That's all for this topic Output Parsers in LangChain With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
No comments:
Post a Comment