DEV Community

Cover image for Structured Output in LangChain
Abhishek Jaiswal
Abhishek Jaiswal

Posted on

Structured Output in LangChain

When I started building LLM applications, one thing became obvious very quickly:

Getting a response from an LLM is easy.

Getting a reliable, predictable, machine-readable response from an LLM is the real challenge.

A chatbot returning:

"Here is your answer..."

is fine for a demo.

But in an enterprise AI system, we usually need something much more strict:

  • Extract customer information from documents
  • Generate JSON responses for APIs
  • Classify support tickets
  • Extract financial data
  • Validate AI-generated decisions
  • Trigger workflows based on AI output

A traditional LLM response is just text. Your application cannot safely depend on random text.

This is where LangChain Structured Output becomes extremely useful.

In this article, we will understand:

  • What structured output means in LangChain
  • Why normal LLM responses fail in production
  • How LangChain structured output works internally
  • Using Pydantic models
  • JSON schema based outputs
  • Structured output with agents
  • Enterprise-level examples
  • Production best practices

What is Structured Output in LangChain?

Structured output means forcing an LLM to return data in a predefined format instead of plain text.

For example, instead of:

The customer Babu Rao has an account with premium subscription and his payment failed yesterday.
Enter fullscreen mode Exit fullscreen mode

we want:

{
  "customer_name": "Babu Rao",
  "subscription": "premium",
  "issue": "payment_failed",
  "priority": "high"
}
Enter fullscreen mode Exit fullscreen mode

Now your backend can directly consume this response.

A structured response can be:

  • JSON
  • Pydantic object
  • Typed dictionary
  • Custom schema

Why Do We Need Structured Output?

Imagine building an AI customer support automation system.

Without structured output:

User message
      |
      v
     LLM
      |
      v
Random text response
Enter fullscreen mode Exit fullscreen mode

Your backend has no guarantee.

The model might return:

The issue seems related to payment. Please contact support.
Enter fullscreen mode Exit fullscreen mode

or:

{
 "category":"billing"
}
Enter fullscreen mode Exit fullscreen mode

or:

Here is the information:
{
 "category":"billing"
}
Enter fullscreen mode Exit fullscreen mode

Every format is different.

Your code breaks.

With structured output:

User message
      |
      v
     LLM
      |
      v
Validated Schema
      |
      v
Backend Workflow
Enter fullscreen mode Exit fullscreen mode

Now your application knows exactly what to expect.

LangChain Structured Output with Pydantic

The most common approach in production is using Pydantic models.

Pydantic gives us:

  • Type validation
  • Required fields
  • Data consistency
  • Error handling

Install dependencies:

pip install langchain langchain-openai pydantic
Enter fullscreen mode Exit fullscreen mode

Example:

from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI


class CustomerIssue(BaseModel):

    customer_name: str = Field(
        description="Name of the customer"
    )

    issue_type: str = Field(
        description="Category of customer problem"
    )

    priority: str = Field(
        description="Priority level: low, medium, high"
    )


llm = ChatOpenAI(
    model="gpt-4.1",
    temperature=0
)


structured_llm = llm.with_structured_output(
    CustomerIssue
)


response = structured_llm.invoke(
    """
    Customer Babu Rao reported that his credit card payment
    failed multiple times and he cannot complete checkout.
    """
)


print(response)
Enter fullscreen mode Exit fullscreen mode

Output:

CustomerIssue(
    customer_name="Babu Rao",
    issue_type="payment_failure",
    priority="high"
)
Enter fullscreen mode Exit fullscreen mode

Now instead of handling strings, we work with Python objects.

Understanding with_structured_output()

This line:

structured_llm = llm.with_structured_output(CustomerIssue)
Enter fullscreen mode Exit fullscreen mode

changes the behavior of the model.

Internally LangChain does something like:

  1. Reads your schema
  2. Converts it into a format the model understands
  3. Sends structured output instructions
  4. Receives model response
  5. Validates the response
  6. Returns the parsed object

Basically:

Pydantic Model
        |
        v
JSON Schema
        |
        v
LLM Instructions
        |
        v
Validated Response
Enter fullscreen mode Exit fullscreen mode

Enterprise Example: AI Document Extraction System

A common enterprise use case:

Extract invoice information automatically.

Input document:

Invoice Number: INV-10291

Customer:
ABC Technologies

Amount:
$25,000

Payment Status:
Pending
Enter fullscreen mode Exit fullscreen mode

We want:

{
 "invoice_id":"INV-10291",
 "customer":"ABC Technologies",
 "amount":25000,
 "payment_status":"pending"
}
Enter fullscreen mode Exit fullscreen mode

Implementation:

from pydantic import BaseModel


class Invoice(BaseModel):

    invoice_id: str

    customer_name: str

    amount: float

    payment_status: str



invoice_llm = llm.with_structured_output(
    Invoice
)


result = invoice_llm.invoke(
"""
Extract invoice details:

Invoice Number: INV-10291

Customer:
ABC Technologies

Amount:
25000

Payment Status:
Pending
"""
)


print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

Invoice(
 invoice_id="INV-10291",
 customer_name="ABC Technologies",
 amount=25000,
 payment_status="Pending"
)
Enter fullscreen mode Exit fullscreen mode

Now this output can directly go into:

  • Database
  • ERP system
  • Payment workflow
  • Analytics pipeline

Structured Output in RAG Applications

RAG systems are one of the biggest enterprise use cases.

Normally:

User Query

      |
      v

Retriever

      |
      v

Documents

      |
      v

LLM

      |
      v

Answer
Enter fullscreen mode Exit fullscreen mode

But enterprise systems often need:

Answer
+
Sources
+
Confidence Score
+
Action
Enter fullscreen mode Exit fullscreen mode

Example:

from pydantic import BaseModel


class RAGResponse(BaseModel):

    answer: str

    confidence: float

    sources: list[str]

    action: str



rag_llm = llm.with_structured_output(
    RAGResponse
)


response = rag_llm.invoke(
"""
Based on company policy documents,
answer:

Can employees work remotely?
"""
)
Enter fullscreen mode Exit fullscreen mode

Output:

{
 "answer":"Employees can work remotely 3 days per week",
 "confidence":0.94,
 "sources":[
   "remote_policy.pdf"
 ],
 "action":"inform_user"
}
Enter fullscreen mode Exit fullscreen mode

This is much easier to integrate into an enterprise application.

Structured Output with LangChain Agents

Agents are powerful but unpredictable.

An agent may:

  • Call tools
  • Reason internally
  • Decide next actions

Structured output helps control agent behavior.

Example:

from pydantic import BaseModel


class AgentDecision(BaseModel):

    next_action: str

    tool_required: bool

    reason: str



agent_llm = llm.with_structured_output(
    AgentDecision
)


decision = agent_llm.invoke(
"""
A customer wants to cancel subscription.
Decide the next action.
"""
)


print(decision)
Enter fullscreen mode Exit fullscreen mode

Output:

{
 "next_action":"billing_agent",
 "tool_required":true,
 "reason":"Cancellation requires account verification"
}
Enter fullscreen mode Exit fullscreen mode

Now your orchestration layer can route requests safely.

Structured Output vs JSON Mode

Many developers confuse these two.

JSON Mode

Example:

llm.invoke(
"Return JSON only"
)
Enter fullscreen mode Exit fullscreen mode

Problem:

The model can still return invalid JSON.

Example:

{
"name":"Babu Rao",
}
Enter fullscreen mode Exit fullscreen mode

Invalid.

Structured Output

With LangChain:

llm.with_structured_output(MySchema)
Enter fullscreen mode Exit fullscreen mode

You get:

  • Schema validation
  • Type checking
  • Better reliability

For production applications, structured output is usually the better choice.

Handling Validation Errors

Production systems need error handling.

Example:

try:

    response = structured_llm.invoke(
        user_input
    )

except Exception as e:

    print(
        "LLM output validation failed",
        e
    )
Enter fullscreen mode Exit fullscreen mode

In real systems, you can:

  • Retry generation
  • Ask model to correct output
  • Log failures
  • Send to human review

Advanced Pattern: Multiple Output Types

Sometimes AI responses depend on the situation.

Example:

Customer support:

from typing import Union


class RefundRequest(BaseModel):

    order_id:str

    refund_reason:str



class Complaint(BaseModel):

    category:str

    description:str
Enter fullscreen mode Exit fullscreen mode

Your application can route based on the returned schema.

Production Best Practices

1. Keep schemas simple

Bad:

class Response:

    everything_possible:str
Enter fullscreen mode Exit fullscreen mode

Good:

class Response:

    category:str

    confidence:float
Enter fullscreen mode Exit fullscreen mode

Clear schemas produce better outputs.

2. Use descriptions

Instead of:

priority:str
Enter fullscreen mode Exit fullscreen mode

Use:

priority:str = Field(
description="Urgency level: low, medium, high"
)
Enter fullscreen mode Exit fullscreen mode

Descriptions improve model understanding.

3. Use temperature 0 for extraction tasks

For structured extraction:

ChatOpenAI(
temperature=0
)
Enter fullscreen mode Exit fullscreen mode

You want consistency, not creativity.

4. Always validate AI output

Never blindly trust an LLM response.

AI output should go through:

LLM
 |
 v
Validation
 |
 v
Business Rules
 |
 v
Database / API
Enter fullscreen mode Exit fullscreen mode

Real Enterprise Architecture

A production AI application usually looks like:

                 User

                  |

              API Gateway

                  |

             AI Service

                  |

        LangChain Orchestration

                  |

       Structured Output Layer

                  |

        Validation + Business Logic

                  |

        Database / External APIs
Enter fullscreen mode Exit fullscreen mode

Structured output becomes the contract between AI and your application.

Final Thoughts

LLMs are amazing at generating human-like responses, but enterprise software needs reliability.

Structured output is one of the techniques that helps bridge this gap.

With LangChain structured output, you can build AI systems that are:

  • More predictable
  • Easier to maintain
  • Safer for production
  • Easier to integrate with APIs and databases

The future of enterprise AI is not just generating text.

It is generating structured intelligence that software can trust.

Top comments (0)