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

推荐订阅源

博客园_首页
H
Help Net Security
量子位
The Cloudflare Blog
博客园 - Franky
博客园 - 聂微东
博客园 - 司徒正美
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
罗磊的独立博客
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
MongoDB | Blog
MongoDB | Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
LangChain fundamentals part 2: structured outputs and too...
Godspower An · 2026-05-01 · via DEV Community

Godspower Anthony-Ikpe

This is where LangChain stops being a convenience layer and starts becoming genuinely powerful.

If you missed part 1, start there — it covers the Runnable protocol and LCEL, the mental model everything else builds on.

Concept 4 — structured outputs

Raw LLM responses are strings. Real applications need JSON — predictable, parseable, typed data your code can actually work with.

LangChain gives you two approaches. The more reliable one is .with_structured_output(), which uses your Pydantic model to constrain what the LLM returns.

from langchain_anthropic import ChatAnthropic
from langchain_core.pydantic_v1 import BaseModel, Field
from typing import List

class CodeReview(BaseModel):
    issues: List[str] = Field(description="List of code issues found")
    severity: str = Field(description="low, medium, or high")
    refactored_snippet: str = Field(description="Improved version of the code")

llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")
structured_llm = llm.with_structured_output(CodeReview)

result = structured_llm.invoke("Review this code: for i in range(len(items)): print(items[i])")
print(result.issues)
print(result.severity)

Enter fullscreen mode Exit fullscreen mode

The Pydantic field descriptions are not just documentation — the LLM reads them to understand what each field should contain. Write them like instructions, not labels.

The alternative is PydanticOutputParser, which asks the LLM to format its response as JSON then parses it. It works, but .with_structured_output() is more consistent in production — use that by default.

Concept 5 — tool calling

Tool calling is the bridge to agents. It lets the LLM decide at runtime which Python functions to call based on the user's intent — and when to call them.

A tool is just a Python function decorated with @tool:

from langchain_core.tools import tool

@tool
def get_contact(email: str) -> dict:
    """Fetch a CRM contact by email address.

    Use this when the user wants to look up, retrieve, or find
    information about a specific contact using their email.
    """
    return db.query("SELECT * FROM contacts WHERE email = ?", email)

llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")
llm_with_tools = llm.bind_tools([get_contact])

response = llm_with_tools.invoke(
    "Look up admin@example.com and tell me if they've shown interest in the product"
)
print(response)

Enter fullscreen mode Exit fullscreen mode

The @tool decorator reads your docstring and type hints to generate the tool schema automatically. The LLM uses that schema to decide when and how to call the tool. Write good docstrings — they are not optional documentation, they are instructions the model actually reads.

That example is pulled from something I actually built — a CRM integration where the agent looks up contacts and updates their interest flags based on conversation context. The docstring is what makes the LLM call the right tool at the right moment.

Why this all fits together

Structured outputs give you reliable data extraction from unstructured language. Tool calling gives the LLM agency to act on your systems. Put them together and you have the foundation for any serious AI feature — automated pipelines, conversational agents, multi-step workflows.

And because everything is still a Runnable, it all chains together with | the same way you learned in part 1.


Next: LangGraph — when your LLM needs to make decisions across multiple steps, loop back, and maintain state. That's where single chains stop being enough.