Structured Output with LangChain: with_structured_output and Pydantic

By Carlos Montiel | Enterprise AI Specialist
Leer en español →
Published: 2026-07-28 | By: Carlos Montiel | Reading time: ~4 minutes

with_structured_output makes forcing valid JSON out of an LLM look like magic — but underneath, what you're really doing is writing it a more detailed prompt through your Pydantic schema.

What with_structured_output does

The `with_structured_output` method lets you pass a Pydantic BaseModel class to a chat model, which then returns structured responses, with the option of also including the raw output. It's LangChain's core mechanism for imposing specific data formats on LLM-generated results.

Why the docstring and descriptions matter as much as the type

With Pydantic models, the class name, the docstring, and the names and descriptions provided for the parameters matter, since `with_structured_output` typically uses the model's function/tool-calling API and appends this information to the model's prompt. Class and field descriptions are crucial because they dictate the content of the output the large model generates — a poorly described field produces malformed output, even when the data type is technically correct.

Code example

from pydantic import BaseModel, Field from langchain_openai import ChatOpenAI class Ticket(BaseModel): """Extract the key information from a support ticket.""" category: str = Field(description="One of: bug, question, feature request") urgency: int = Field(description="From 1 (low) to 5 (critical)") summary: str = Field(description="One-sentence summary of the problem") model = ChatOpenAI(model="gpt-4o-mini") structured_model = model.with_structured_output(Ticket) result = structured_model.invoke("Login crashes every time I use 2FA, urgent") print(result.category, result.urgency)

What happens with models that don't support native function calling

For models that don't support native structured output, LangChain uses tool calling to achieve the same result, which works with any model that supports tool calling (most modern models) — LangChain's abstraction shields you from having to manually implement that fallback depending on which model you're using.

Pydantic is the strictest option — and that's an advantage

Pydantic is the most powerful and strict of the structured output methods available in LangChain. For pipelines where the output feeds directly into a downstream system (a database, another service), that rigidity is exactly what you need — prefer Pydantic over looser alternatives like TypedDict when schema correctness is critical, not just an approximate guide.

Carlos Montiel
Enterprise AI Solutions Architect
Specialist in LLMs, Agents, and Orchestration
guatemalia.com/en/#contact · info@guatemalia.com

Need to implement AI at your company?

Carlos Montiel is an enterprise AI solutions architect. He implements LLMs, Agents, RAG, and orchestrators for companies across Guatemala and Latin America. Reach out for a consultation.

Contact Carlos Montiel

info@guatemalia.com