So far, weβve learned how to give instructions (Prompts) and get responses (Models). But thereβs a problem: AI loves to talk. If you ask for a list of three cities, it might give you a whole paragraph explaining why those cities are great.
If youβre building an app, you donβt want a paragraph; you want a Python list or a JSON object. Today, we learn how to "extract" exactly what we need using Output Parsers.
π§ Why do we need Parsers?
When you call an LLM in LangChain, it doesn't return a simple string. It returns an AIMessage object that contains the text, metadata, and token usage.
An Output Parser is the final link in your chain that:
- Takes that messy object.
- Extracts the useful text.
- Transforms it into a format your code can actually use (like a Dictionary or a List).
π 1. The Simple List Parser
Letβs say you want the AI to suggest three niche business ideas. You want them as a clean Python list.
from langchain_core.output_parsers import CommaSeparatedListOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
parser = CommaSeparatedListOutputParser()
# The parser even gives us instructions to tell the AI!
prompt = PromptTemplate(
template="List 3 high-value {industry} niches.\n{format_instructions}",
input_variables=["industry"],
partial_variables={"format_instructions": parser.get_format_instructions()}
)
chain = prompt | ChatOpenAI() | parser
result = chain.invoke({"industry": "AI Automation"})
print(result)
# Output: ['Customer Support Bots', 'Legal Document Review', 'Automated Content Grading']
π 2. The Powerhouse: Pydantic (JSON) Parser
This is where things get serious. If you are building a professional tool, you need JSON. The best way to get it is using a library called Pydantic to define exactly what your data should look like.
from langchain_core.output_parsers import JsonOutputParser
from pydantic import BaseModel, Field
# 1. Define your "Target" data structure
class StartupIdea(BaseModel):
name: str = Field(description="Name of the startup")
revenue_model: str = Field(description="How it makes money")
complexity_score: int = Field(description="Score from 1 to 10")
# 2. Initialize the parser
parser = JsonOutputParser(pydantic_object=StartupIdea)
# 3. Chain it up!
prompt = PromptTemplate(
template="Generate a startup idea for {topic}.\n{format_instructions}",
input_variables=["topic"],
partial_variables={"format_instructions": parser.get_format_instructions()}
)
chain = prompt | ChatOpenAI() | parser
print(chain.invoke({"topic": "Sustainable Fashion"}))
β‘ How it works under the hood
When you use parser.get_format_instructions(), LangChain injects a very specific set of rules into your prompt, telling the AI: "Your output must be JSON and follow this exact schema. Do not include any conversational text."
This is how we get the "brain in a jar" to behave like a structured database!
π― Day 4 Summary
Today we completed the "Core Trinity" of LangChain:
Prompts (Input)
Models (Processing)
Parsers (Output)
You now have the power to build chains that output clean, reliable data for your apps.
Your Homework: Try using the CommaSeparatedListOutputParser to get a list of your 5 favorite books. See if you can get the AI to output only the titles.
See you tomorrow! β
Top comments (0)