Ch 3 — Prompts & Parsers Under the Hood

Template class hierarchy, Pydantic schema generation, format instructions, structured output internals, retry parsing
Under the Hood
-
Click play or press Space to begin...
Step- / 10
ATemplate Class HierarchyBasePromptTemplate → subclasses
1
account_tree
BasePromptTemplateAbstract base
with .invoke()
extends
short_text
PromptTemplatestr.format()
single string
extends
forum
ChatPromptTemplateList of message
templates
2
arrow_downward Inside ChatPromptTemplate: message template chain
BMessage Template InternalsHow each message gets its template
person
SystemMessage
PromptTemplate
Wraps a
PromptTemplate
+
chat
HumanMessage
PromptTemplate
Wraps a
PromptTemplate
+
3
history
Messages
Placeholder
Injects a list
of messages
4
arrow_downward .partial() — pre-filling variables
C.partial() and Format InstructionsPre-filling variables and injecting parser instructions
edit_note
.partial()Pre-fill some
variables early
e.g.
description
format_instructionsParser's schema
injected into prompt
result
check
Partial TemplateOnly {user_input}
left to fill
5
arrow_downward Pydantic model → JSON Schema generation
DPydantic to JSON SchemaHow your Python class becomes LLM instructions
code
Pydantic ModelPython class with
typed fields
generates
schema
JSON Schema.model_json_schema()
standard format
sent as
6
send
API Parameterresponse_format
or tool definition
7
arrow_downward with_structured_output() internals
EInside with_structured_output()Two strategies: tool calling vs JSON mode
build
Tool CallingSchema as a
"function" definition
or
data_object
JSON Moderesponse_format:
json_schema
then
8
verified
Pydantic.validate()Parse JSON into
model instance
9
arrow_downward When parsing fails: OutputFixingParser
FError Recovery & Retry ParsingWhat happens when the LLM doesn't comply
error
Parse FailsInvalid JSON or
wrong schema
catch
refresh
OutputFixingParserSends error back
to LLM to fix
retry
check_circle
Fixed OutputValid data on
second attempt
10
arrow_downward The full data flow: template → schema → validate
GComplete Data FlowEvery transformation from input dict to validated object
input
Input Dict{"text": "I love
this product!"}
format
forum
MessagesSystem + Human
with schema
API
cloud
LLM + SchemaConstrained
JSON generation
validate
verified
Pydantic ObjectTyped, validated,
ready to use
1
Detail