Structured output: answers in a fixed shape
model.with_structured_output(Schema) makes the model return data in a shape you define, not free text, so your code gets fields it can trust instead of a string to parse.
Last updated: 27 Sep, 2026 · LangChain 1.4
A model's reply is text, worded differently every time. When a node needs a decision your code will act on, such as a category and whether a ticket is urgent, you want a fixed shape, not a sentence.
The with_structured_output call
from pydantic import BaseModel
class Triage(BaseModel):
category: str
urgent: bool
structured = model.with_structured_output(Triage)
result = structured.invoke("I was charged twice and I am furious")
print(result.category, result.urgent) # -> billing TrueThe output schema
First describe the shape you want back. A Pydantic model lists the field names and their types.
from pydantic import BaseModel
class Triage(BaseModel): # the shape: two fields with types
category: str
urgent: boolAn object of that shape
with_structured_output hands back an object of this type. To show what a node receives without calling a real model, build one Triage directly.
# what with_structured_output gives back is an object of this type
result = Triage(category="billing", urgent=True)Reading the fields
Read the fields straight off the object. There is no string to parse and no wording to guess at.
print(result.category, result.urgent) # read fields directlyReading a structured result end to end
The same pieces in one file, ready to run.
from pydantic import BaseModel
class Triage(BaseModel):
category: str
urgent: bool
# what with_structured_output gives back is an object of this type
result = Triage(category="billing", urgent=True)
print(result.category, result.urgent)With a real model, structured = model.with_structured_output(Triage) and then structured.invoke(...) hands back exactly this kind of Triage object, so your node reads result.category and result.urgent the same way.
Why the fields are safe to read
- You describe the shape with a Pydantic model: field names and types.
with_structured_outputreturns a model that fills that shape and hands you an object, not text.- Your node then reads
result.categorydirectly, with no parsing and no guessing at the wording.
Free text vs structured output
| Free text | Structured output | |
|---|---|---|
| What you get | A string | An object with fields |
| Your code must | Parse and hope | Read a field |
| Fails when | The wording changes | The model cannot fit the schema |
Where structured output fits
- Classifying a ticket into a category your code branches on.
- Extracting fields (an order id, a date, a yes or no) the next node needs.
Related
- Previous: ToolNode and tools_condition
- Next: The agent loop
- Reference: Structured output
- Add a
reasonfield toTriage. - Make a schema with an enum-like category using a Literal type.
This is what real progress feels like.