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.
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.
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.
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 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 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