Structured Output in LangChain allows an LLM to return data in a predefined structure instead of returning only free-form text. It is especially useful when you want to connect an LLM with APIs, databases, RAG applications, AI agents, and frontend applications.
What is Structured Output in LangChain? #
Normally, Large Language Models (LLMs) return natural language responses. For example:
Rahul is 25 years old and works as a Python Developer.
This response is easy for a human to understand, but it is not always convenient for a Python application to process.
With structured output, we can define exactly what information we want:
{
"name": "Rahul",
"age": 25,
"job": "Python Developer"
}
In LangChain, we can define this structure using a schema such as
a Pydantic model and then use with_structured_output().
Why Do We Need Structured Output? #
LLMs are designed to generate text, while software applications usually need predictable and machine-readable data.
For example, suppose we are building a resume parser. We may need:
- Name
- Skills
- Education
- Experience
Without structured output, the LLM might return a paragraph:
Rahul Kumar is a Python Developer.
His email is [email protected].
He has experience with Python, Django and SQL.
Our application would then need additional logic to extract each value.
With structured output, we can directly get:
{
"name": "Rahul Kumar",
"email": "[email protected]",
"skills": ["Python", "Django", "SQL"]
}
Now the application can easily access individual values.
Normal LLM Output vs Structured Output #
Normal LLM Output #
User Input
↓
LLM
↓
Free-form Text
↓
Manual Parsing
Structured Output #
User Input
↓
LLM
↓
Defined Schema
↓
Structured Data
↓
Application
This makes structured output much more useful when an LLM is part of a larger software system.
How to Use Structured Output in LangChain? #
One of the simplest approaches is to use a Pydantic model as the output schema.
Step 1: Import Pydantic #
from pydantic import BaseModel, Field
Step 2: Create a Schema #
class Person(BaseModel):
name: str = Field(description="Person's name")
age: int = Field(description="Person's age")
job: str = Field(description="Person's profession")
Here we have defined three fields:
name→ Stringage→ Integerjob→ String
Step 3: Create the LLM #
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-5.4"
)
The model name should be changed according to the model and provider you are using.
Step 4: Add Structured Output #
structured_llm = llm.with_structured_output(Person)
This is the most important line.
It tells LangChain that we want the model’s response to follow the
Person schema.
Step 5: Invoke the Model #
result = structured_llm.invoke(
"Rahul is 25 years old and works as a Python Developer."
)
print(result)
The result will look similar to:
Person(
name="Rahul",
age=25,
job="Python Developer"
)
Access Individual Values #
Because the result is a Pydantic object, we can access individual fields directly.
print(result.name)
print(result.age)
print(result.job)
Output:
Rahul
25
Python Developer
Complete Structured Output Example #
Here is the complete example:
from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI
# Define output schema
class Person(BaseModel):
name: str = Field(
description="Person's name"
)
age: int = Field(
description="Person's age"
)
job: str = Field(
description="Person's profession"
)
# Create LLM
llm = ChatOpenAI(
model="gpt-5.4"
)
# Add structured output
structured_llm = llm.with_structured_output(Person)
# Invoke model
result = structured_llm.invoke(
"Rahul is 25 years old and works as a Python Developer."
)
# Print complete result
print(result)
# Access individual fields
print("Name:", result.name)
print("Age:", result.age)
print("Job:", result.job)
Structured Output with Lists #
Structured output can also contain lists. For example, we can create a schema for a student’s skills:
from pydantic import BaseModel
class Student(BaseModel):
name: str
age: int
skills: list[str]
The model can return:
Student(
name="Rahul",
age=25,
skills=[
"Python",
"SQL",
"Machine Learning",
"FastAPI"
]
)
Structured Output with Nested Objects #
We can also create nested schemas.
from pydantic import BaseModel
class Address(BaseModel):
city: str
country: str
class Person(BaseModel):
name: str
age: int
address: Address
For example, the output can be:
Person(
name="Rahul",
age=25,
address=Address(
city="Patna",
country="India"
)
)
Real-World Example: Resume Parser #
One of the practical applications of structured output is extracting information from resumes.
from pydantic import BaseModel, Field
class Resume(BaseModel):
name: str = Field(
description="Candidate's full name"
)
email: str = Field(
description="Candidate's email address"
)
skills: list[str] = Field(
description="Candidate's technical skills"
)
experience_years: float = Field(
description="Years of professional experience"
)
We can then create a structured LLM:
structured_llm = llm.with_structured_output(Resume)
Suppose the input resume contains:
Rahul Kumar
Python Developer
Email: [email protected]
Skills:
Python, Django, FastAPI, SQL, Machine Learning
Experience:
2 years
The LLM can return structured information such as:
Resume(
name="Rahul Kumar",
email="[email protected]",
skills=[
"Python",
"Django",
"FastAPI",
"SQL",
"Machine Learning"
],
experience_years=2
)
This information can then be stored in a database or passed to another part of the application.
Structured Output in RAG Applications #
Structured output is also useful in Retrieval-Augmented Generation (RAG). For example, instead of returning only an answer, we can define:
class Answer(BaseModel):
answer: str
confidence: float
sources: list[str]
The application can then receive:
Answer(
answer="Python is a programming language.",
confidence=0.95,
sources=["document_1"]
)
The frontend can display the answer, confidence, and sources separately.
Structured Output vs Asking the LLM for JSON #
A common beginner approach is to simply tell the LLM:
Return the answer in JSON format.
Although this can work, it does not provide the same level of schema control as using structured output.
Prompt-Based JSON #
LLM
↓
"Return JSON"
↓
Text that looks like JSON
↓
JSON Parser
Structured Output #
LLM
↓
Defined Schema
↓
Structured Response
↓
Application
Structured output is therefore a better approach when your application requires predictable data.
Structured Output vs Tool Calling #
Structured output and tool calling are related, but they have different purposes.
Tool Calling #
Tool calling allows an LLM to request that a function or tool be executed.
LLM
↓
Tool Call
↓
get_weather("Patna")
↓
Weather Result
Structured Output #
Structured output is primarily about returning data in a predefined schema.
LLM
↓
Schema
↓
Structured Data
Depending on the model and provider, LangChain can use provider-native structured output or tool/function calling to achieve structured results.
Where is Structured Output Used? #
- Resume parsing
- Information extraction
- RAG applications
- AI agents
- Customer support systems
- Job recommendation systems
- Sentiment analysis
- Document processing
- FastAPI and other APIs
- Database applications
- AI dashboards
- Frontend applications
Key Method to Remember #
If you are learning LangChain, remember this basic pattern:
from pydantic import BaseModel
from langchain_openai import ChatOpenAI
class Person(BaseModel):
name: str
age: int
job: str
llm = ChatOpenAI(model="gpt-5.4")
structured_llm = llm.with_structured_output(Person)
result = structured_llm.invoke(
"Rahul is 25 years old and works as a Python Developer."
)
print(result)
Summary #
Structured Output in LangChain allows an LLM to return predictable, schema-defined data instead of only free-form text.
The basic workflow is:
Define Schema
↓
Create LLM
↓
with_structured_output()
↓
Invoke LLM
↓
Receive Structured Data
The most important method to remember is:
llm.with_structured_output(Schema)
Structured output becomes especially valuable when an LLM is connected to real software systems such as APIs, databases, RAG pipelines, recommendation systems, and AI agents.
Frequently Asked Questions #
What is Structured Output in LangChain? #
Structured Output allows an LLM to return information according to a predefined schema rather than returning only free-form text.
Why use Structured Output? #
It makes LLM responses easier for applications to validate, process, store, and pass to other components.
What is the main method for Structured Output? #
The main method for direct LangChain model calls is
with_structured_output().
Can Pydantic be used with Structured Output? #
Yes. Pydantic is a common choice for defining structured-output schemas in Python LangChain applications.
Is Structured Output the same as JSON? #
No. JSON is a data format, while structured output is a mechanism for obtaining model responses that conform to a defined schema. The resulting data may be represented as a Pydantic object or dictionary depending on the schema and approach used.
Structured Output MCQ Quiz #
Q.1 In the context of Large Language Models (LLMs), what is primarily considered an 'unstructured' output?
A JSON object with key-value pairs.
A natural language text response.
A CSV file containing tabular data.
A SQL query generated for a database.
Explanation
By default, LLMs generate natural language text, which is considered unstructured because it does not follow a predefined machine-readable format like JSON.
Q.2 What is the most significant advantage of generating structured output from an LLM?
It reduces the token usage and cost of the model.
It allows the LLM to integrate easily with other machines and systems.
It prevents the model from hallucinating facts.
It makes the LLM response more human-readable.
Explanation
Structured output enables seamless integration with APIs, databases, applications, and other software systems because the output follows a predictable format.
Q.3 When building an AI Agent that uses a calculator tool, why is structured output necessary?
Structured output increases the mathematical accuracy of the LLM.
Agents are only allowed to communicate via JSON schemas.
Calculators cannot interpret natural language instructions directly.
Tools are designed to verify the sentiment of the user prompt.
Explanation
Calculator tools expect structured inputs such as numbers and parameters. They cannot reliably understand free-form natural language responses from an LLM.
Q.4 Which LangChain function is used to simplify the process of obtaining structured data from models that natively support it?
format_as_pydantic()
invoke_as_json()
parse_output_structure()
with_structured_output()
Explanation
LangChain’s with_structured_output() method automatically configures supported models to return data matching a schema such as Pydantic, TypedDict, or JSON Schema.
Q.5 What is a major limitation of using Python's 'TypedDict' for defining output schemas in LangChain?
It requires a separate license for commercial use.
It cannot be converted into a JSON format.
It does not support string-based keys.
It does not perform runtime data validation.
Explanation
TypedDict provides static type hints for developers but performs no runtime validation, so invalid data types may pass through unnoticed.
Q.6 In Pydantic, which class must your schema inherit from to define a structured data model?
SchemaObject
BaseModel
DataClass
StructuredDict
Explanation
Every Pydantic schema inherits from BaseModel, which provides validation, parsing, serialization, and other useful features.
Q.7 What happens in Pydantic if you pass the string '25' to a field defined as an integer?
The model ignores the input and sets the value to 'None'.
Pydantic throws an Immediate ValidationError.
Pydantic automatically converts (coerces) the string to the integer 25.
The value is stored as a string regardless of the definition.
Explanation
Pydantic performs type coercion whenever possible. Since ’25’ is a valid numeric string, it is automatically converted into the integer 25.
Q.8 Which scenario would most benefit from using 'JSON Schema' over 'Pydantic' for defining structured output?
When running models that do not support JSON mode.
A project built entirely in Python.
A project where the schema needs to be shared between Python and JavaScript systems.
When the developer wants the simplest possible syntax.
Explanation
JSON Schema is language-independent, making it ideal for projects where multiple programming languages need to use the same schema definition.
Q.9 What is the purpose of the 'Field' function in Pydantic schemas?
To increase the speed of the LLM's response generation.
To define metadata like descriptions and constraints for specific attributes.
To encrypt the data before sending it to the LLM.
To tell the LLM which database table to use.
Explanation
Field() allows developers to add descriptions, validation rules, default values, and constraints that help both Pydantic and the LLM understand the expected data.
Q.10 If you are using an open-source model like 'TinyLlama' that does not natively support structured output, what is the recommended approach in LangChain?
Use Pydantic with the 'with_structured_output' function.
Convert the TinyLlama model into a GPT-4 model.
Use Output Parsers to process the raw text output.
Force the model to use 'function_calling' mode.
Explanation
For models without native structured output support, LangChain recommends using Output Parsers to transform raw text responses into structured data.
Q.11 In the 'with_structured_output' function, when should you typically set the 'method' parameter to 'function_calling'?
When you want the output in a raw string format.
When you are using a model that only supports JSON mode.
When the model needs to call a human for help.
When using OpenAI models that support specialized API calls for tools.
Explanation
The function_calling method leverages OpenAI’s tool-calling capability to produce reliable structured outputs and is the recommended approach for supported models.
Q.12 How can you make a field optional in a Pydantic model so that it doesn't cause an error if the LLM fails to provide it?
Set the type to 'NoneType'.
Wrap the type in 'Optional' and provide a default value of 'None'.
Delete the field from the class definition.
Use the 'skip_validation' decorator.
Explanation
(or type | None in newer Python versions) with a default value of None, allowing the field to be omitted without raising a validation error.”]