Lesson 8 / 31
Output Parsers and Structured Output
Turn model text into validated Python objects.
Parse, validate, fail loudly
Models return text; programs need objects. Output parsers convert the reply: StrOutputParser for plain text, JsonOutputParser for JSON, and PydanticOutputParser to validate against a Pydantic schema with types and constraints (for example urgency between 1 and 5). A parser can also produce format instructions to put in the prompt. If the reply breaks the schema the parser raises an OutputParserException, which you can catch and retry. Many chat models also support native structured output through model.with_structured_output(Schema), which uses the provider's tool-calling or JSON-schema feature and is usually more reliable than parsing free text.
Validating with Pydantic, run
I ran this offline in a Python virtual environment with langchain-core 1.6.6, langchain-text-splitters 1.1.2 and llama-index-core 0.14.25. No API key or network call is needed because a fake model or a toy embedding stands in for the real one. The valid reply becomes a Ticket object. A reply with urgency 9 violates the le=5 constraint, so the parser raises OutputParserException.
from pydantic import BaseModel, Field
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.exceptions import OutputParserException
class Ticket(BaseModel):
category: str = Field(description="billing, technical or other")
urgency: int = Field(ge=1, le=5)
parser = PydanticOutputParser(pydantic_object=Ticket)
print(parser.parse('{"category": "billing", "urgency": 4}'))
try:
parser.parse('{"category": "billing", "urgency": 9}')
except OutputParserException as e:
print("rejected:", type(e).__name__)
print("format hint mentions:", "urgency" in parser.get_format_instructions())
Output:
category='billing' urgency=4 rejected: OutputParserException format hint mentions: True
Quick check: What happens when the reply breaks the Pydantic schema?
- The model is retrained
- The bad value is silently accepted
- The parser raises an exception you can catch and retry
- The program ends without error
Answer
The parser raises an exception you can catch and retry — Failing loudly lets you handle bad output explicitly.