惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
D
Docker
腾讯CDC
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
IT之家
IT之家
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
博客园 - 【当耐特】

Stories by HARSHA J S on Medium

Mastering LangChain 1.2: Part 13 — Agentic RAG: Why Your AI Needs to Decide When to Google Mastering LangChain 1.2: Part 12 — The Elephant’s Memory: Persistent AI Agents that Never Forget Mastering LangChain 1.2: Part 11 — Custom Middleware Hooks: Orchestrating the AI Lifecycle Mastering LangChain 1.2: Part 10 — Human-in-the-Loop: Adding an “Approval” Button to Your AI Agents Mastering LangChain 1.2: Mastering LangChain 1.2: Mastering LangChain 1.2: Part 6 — Dynamic Tool Security: The “Triple-Lock” Guardrail Mastering LangChain 1.2: Part 5 — Dynamic Model Routing: Escalating to Senior AI in Emergencies Mastering LangChain 1.2: Part 4 — Scaling with Middleware and Smart Summarization
Mastering LangChain 1.2: Part 9 — Structured Outputs: Tur...
HARSHA J S · 2026-03-26 · via Stories by HARSHA J S on Medium

HARSHA J S

This is a natural next step for the series! Here is the Medium-style article for Part 9, focusing on Structured Outputs and Data Extraction.

I models are incredible at understanding human language, but they are notoriously bad at following a “contract.” If you ask an LLM to “return only JSON,” you often get a chatty response like: “Sure! Here is your JSON: json ... ".

In production, you don’t need a polite chat; you need predictable, validated data. You need a Structured Output.

In LangChain 1.2, the response_format parameter allows you to force an agent to return an object that matches a specific Pydantic schema. No more regex, no more string parsing—just clean, typed objects.

1. Defining the “Contract” with Pydantic

First, we define exactly what we want the AI to give us using a Pydantic model. This acts as the “Standard Operating Procedure” for the agent.

from pydantic import BaseModel, Field

class InfraRequest(BaseModel):
"""Configuration for a cloud resource."""
service_type: str = Field(description="e.g. EC2, RDS, S3, Redis")
environment: str = Field(description="dev, staging, or prod")
project_name: str = Field(description="The app this belongs to")
instance_size: str = Field(description="small, medium, or large")

2. Choosing Your Strategy

LangChain 1.2 provides two powerful ways to enforce this structure:

Strategy A: ProviderStrategy (Modern & Native)

Many modern model providers (like OpenAI, Anthropic, or even local models via Ollama) support native structured output. This is the most reliable method because the model itself is optimized to follow the schema.

agent = create_agent(
model=model,
# FORCE the output to match our Schema
response_format=InfraRequest,
)

If your model doesn’t support native structured output, LangChain can use “Artificial Tool Calling.” It pretends there is a tool called

InfraRequest and forces the model to “call” it with the correct arguments.

agent = create_agent(
model=model,
# FORCE the output to match our Schema
response_format=InfraRequest,
)

3. From Slack Message to Cloud Config

Let’s see the transformation in action. Imagine a user drops a casual request in a Slack channel:

Input: “Hey, can we get a big redis cache for the billing team? It’s for the new production launch.”

The agent doesn’t reply with “Okay, I’ll set that up.” Instead, it analyzes the request and returns a structured response object.

from langchain.agents.structured_output import ToolStrategy

agent = create_agent(
model=model,
response_format=ToolStrategy(InfraRequest),
)

Why Is This a Game Changer?

  1. Direct Integration: You can take that config object and pass it directly to an API (like Terraform or AWS SDK) without any manual intervention.
  2. Validation: If the user asks for a “gigantic” size and your schema only allows “small, medium, or large,” the Pydantic layer can catch the error before it hits your infrastructure.
  3. UI Consistency: In the frontend, you don’t have to parse raw text. You receive a JSON-like object that you can immediately display in a clean table or form.

Conclusion: Chat is the Input, Objects are the Output

The goal of a professional AI agent isn’t always to talk to a human. Often, the goal is to translate human intent into computer-executable data.

By mastering structured outputs, you stop building “Chatbots” and start building “Intelligent Pipelines” that can bridge the gap between messy human communication and the rigid requirements of cloud infrastructure.

💬 What do you think?
Drop your thoughts, questions, or suggestions in the comments below!

Check out my YouTube channel for more exciting content! [YouTube Channel Link — Harsha Selvi]

Disclaimer: This text has been rephrased using AI tools, and some parts are derived from various sources to provide a comprehensive overview.

#AI #LangChain #DataEngineering #Python #Pydantic #Automation #SoftwareArchitecture #ModernDevelopment