Module 11 Lesson 2: Output Parsers (Pydantic & JSON)
Parsing the Mess. Learning how to use OutputParsers to extract structured data even from older or less capable models.
Output Parsers: Enforcing the Shape
Before the with_structured_output method existed (Lesson 1), we used Output Parsers. While Lesson 1 is easier, Output Parsers are still essential because they give you Instructions you can put inside your prompt to tell the model how to output JSON.
1. The PydanticOutputParser
This parser does two things:
- Provides Format Instructions: "Your output must be JSON that matches this schema..."
- Parses the result: Converts the string returned by the LLM into a Python object.
from langchain.output_parsers import PydanticOutputParser
# 1. Initialize
parser = PydanticOutputParser(pydantic_object=Person)
# 2. Get Instructions
instructions = parser.get_format_instructions()
print(instructions)
# This will output a wall of text that the LLM understands as a JSON template.
2. Using it in an LCEL Chain
prompt = ChatPromptTemplate.from_template(
"Answer the user query.\n{format_instructions}\n{query}"
)
chain = prompt | model | parser
# The result is already a Pydantic object!
record = chain.invoke({
"query": "Sally is twenty",
"format_instructions": instructions
})
3. Visualizing the Parser Logic
graph TD
In[User Input] --> P[Prompt with Format Instructions]
P --> M[LLM Model]
M -->|"Raw: { 'name': 'Sally', 'age': 20 }"| Parser[PydanticParser]
Parser -->|Verify Types| Obj[Python: Person(name='Sally', age=20)]
4. Resilience: The Retry Logic
What if the model outputs invalid JSON (e.g., forgets a comma)?
- Some specialized parsers, like the
OutputFixingParser, can take the error message and the faulty JSON, send it back to the LLM, and say: "You made a mistake here. Fix the JSON and try again."
5. Engineering Tip: When to use Parsers vs. Lesson 1
- Use
with_structured_outputfor OpenAI/Anthropic (Native support). - Use
PydanticOutputParserfor Local Models (Ollama) or older models that don't support native function calling.
Key Takeaways
- Output Parsers provide explicit instructions to the AI.
get_format_instructions()is the key to ensuring JSON compliance.- Pipes like
model | parserautomate the conversion to Python objects. - Parsers are more flexible for models without native tool-calling features.