Notes on the
StructuredOutputParser— a schema-based parser that returns fixed-field dictionaries.
Note:
StructuredOutputParseris an older LangChain abstraction for schema-based outputs. It has largely been superseded byJsonOutputParserandPydanticOutputParserin newer versions.
- What is Structured Output Parser?
- Why Do We Need Structured Output?
- Types of Structured Output Parsers
- StructuredOutputParser: Core Concept
- Usage Example
- When to Use It
- Limitations
- Comparison with Other Parsers
- Output Parsers Overview
- String Output Parser
- JSON Output Parser
- Pydantic Output Parser
- Structured Output Guide
- Runnables (LCEL)
A Structured Output Parser forces an LLM to return output in a pre-defined structure (schema) with:
- Fixed fields
- Fixed data types
- Validated output
It ensures the model output is machine-readable and reliable.
Free-text output is:
- Unpredictable
- May miss fields
- May change format
Real applications (APIs, forms, agents) need:
- Consistent keys
- Correct data types
- Validation
👉 Structured Output Parsers solve this.
| Parser | Schema Definition | Output | Validation |
|---|---|---|---|
StructuredOutputParser |
ResponseSchema |
dict |
❌ |
JsonOutputParser |
Format instructions | dict |
|
PydanticOutputParser |
Pydantic model | Object | ✅ |
- Defines expected fields using
ResponseSchema - Injects formatting instructions into the prompt
- Parses LLM output into a Python dictionary
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain_core.prompts import PromptTemplate
from langchain_ollama import ChatOllama
schemas = [
ResponseSchema(name="topic", description="Main topic"),
ResponseSchema(name="summary", description="Short explanation"),
ResponseSchema(name="difficulty", description="Easy / Medium / Hard"),
]
parser = StructuredOutputParser.from_response_schemas(schemas)
format_instructions = parser.get_format_instructions()
prompt = PromptTemplate(
template="Explain {topic}\n{format_instructions}",
input_variables=["topic"],
partial_variables={"format_instructions": format_instructions}
)
llm = ChatOllama(model="llama3")
chain = prompt | llm | parser
result = chain.invoke({"topic": "Black Holes"})
# → {"topic": "Black Hole", "summary": "...", "difficulty": "Medium"}
print(type(result)) # → <class 'dict'>Use StructuredOutputParser when you need:
- Fixed fields in the output
- Consistent format without strict type validation
- Legacy LangChain compatibility
Examples:
- Question–answer format
- Summary + keywords
- Explanation + difficulty level
- Tool input formatting
- ❌ Does not strictly validate data types
- ❌ Model may still hallucinate values
- ❌ Less powerful than
PydanticOutputParser
👉 For strict control → PydanticOutputParser
| Parser | Structure | Validation | Use Case |
|---|---|---|---|
StrOutputParser |
❌ | ❌ | Simple text |
JsonOutputParser |
❌ | Loose JSON | |
StructuredOutputParser |
✅ | ❌ | Fixed fields |
PydanticOutputParser |
✅ | ✅ | Production |
- Text →
StrOutputParser - JSON →
JsonOutputParser - Fields →
StructuredOutputParser - Types →
PydanticOutputParser
One-line definition: Structured Output Parser enforces a predefined response schema on LLM output, returning structured data as a dictionary.