Output Formatting
Every chain ends with a parser — that is what invoke returns. The LCEL lesson used StrOutputParser for a plain string. This lesson adds PydanticOutputParser when you need fixed fields: tag, purpose, example. Same HTML tag question, two return types.
Parser sets the return type
The model replies with text. The parser at the end turns it into what your code expects — a str, a dict, or a Pydantic object.
Parser is the last step — it sets what invoke returns:
prompt
ChatPromptTemplate
model
ChatOpenAI
parser
OutputParser
result
str or object
StrOutputParser vs PydanticOutputParser
StrOutputParser pulls out the text string. On langchain-core 1.0+ the type may print as TextAccessor — still a str subclass (isinstance(…, str) is True). PydanticOutputParser parses JSON into HtmlTagInfo — read fields with info.tag, etc.
StrOutputParser
text = str_chain.invoke(
{"tag": "a"}
)
# type: TextAccessor (str-like)
# isinstance(text, str) → True
# "The <a> tag creates a hyperlink…"Print or pass straight to the next prompt
PydanticOutputParser
info = struct_chain.invoke(
{"tag": "a"}
)
# type: HtmlTagInfo
info.tag # str
info.purpose # str
info.example # strTyped fields — use dot notation
StrOutputParser. Here you add PydanticOutputParser for named fields.HtmlTagInfo schema
List the fields in a BaseModel. Pass it to PydanticOutputParser and put format_instructions in the prompt so the reply is JSON matching those fields.
HtmlTagInfo fields
class HtmlTagInfo(BaseModel):
tag: str # e.g. "a"
purpose: str # one-sentence explanation
example: str # minimal HTML snippetBuilding struct_chain
PydanticOutputParser(pydantic_object=HtmlTagInfo)— parser plusget_format_instructions()..partial(format_instructions=…)— fixes the JSON hint in the prompt template.struct_chain = prompt | model | parser— same pipe as other lessons; parser is the only change.
The demo script
output_formatting_demo.py runs both parsers on a and img.
Download the code
output_formatting_demo.py
StrOutputParser + PydanticOutputParser
langchain-course folder. Needs venv, .env, and packages from Project Setup.Run it
Activate the venv from Project Setup, then:
python output_formatting_demo.pya and img — scroll the terminal box if needed.Which parser to pick
StrOutputParser— one text block: print it or feed it to the next prompt (LCEL and the chain lessons so far).PydanticOutputParser— fixed fields you read with dot notation, likeinfo.purposehere.- Official reference: Output parsers concept.
What's Next
Next: RunnableParallel — two branches from one input.