LangGraph 总览(2026年最新)

LangChain 官方有状态工作流引擎。2025年10月发布 1.0,2026年5月更新到 1.2.2。


最新版本(2026年)

版本发布说明
LangGraph 1.2.22026年5月27日2026年最新稳定版
LangGraph 1.02025年10月首个稳定版,与 LangChain 1.0 同步
LangGraph 0.x2024年测试版,已停止维护

GitHub:https://github.com/langchain-ai/langgraph


LangChain vs LangGraph

维度LCEL(LangChain)LangGraph
数据结构有向无环图(DAG)有向循环图
循环支持
持久化✅(Checkpointer)
多Agent基础✅(内置 Supervisor)
适用场景线性流程复杂迭代工作流

核心概念

1. State(状态)

1
2
3
4
5
6
from typing import TypedDict
from langgraph.graph import StateGraph

class AgentState(TypedDict):
messages: list[str]
next_action: str

2. Node(节点)

1
2
3
def agent(state: AgentState):
"""处理逻辑"""
return {"messages": ["processed"]}

3. Edge(边)

1
2
3
4
5
6
7
8
# 固定边
graph.add_edge("agent", "tools")

# 条件边
graph.add_conditional_edges(
"agent",
lambda s: "finish" if s["done"] else "continue"
)

4. Checkpoint(持久化)

1
2
3
4
5
6
7
8
9
10
11
from langgraph.checkpoint.memory import MemorySaver

checkpointer = MemorySaver()
graph = graph.compile(checkpointer=checkpointer)

# 保存状态
config = {"configurable": {"thread_id": "user-123"}}
result = graph.invoke({"messages": ["hi"]}, config=config)

# 恢复状态
history = graph.get_state(config)

实战:ReAct Agent(2026年标准实现)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool

llm = ChatOpenAI(model="gpt-4o-mini")

@tool
def search(query: str):
"""搜索信息"""
return f"搜索结果:{query}"

@tool
def calculator(expr: str):
"""计算表达式"""
return str(eval(expr))

tools = [search, calculator]

# ReAct 模式
def react_agent(state):
messages = state["messages"]
response = llm.bind_messages(messages).invoke()
return {"messages": [response]}

graph = StateGraph(AgentState)
graph.add_node("agent", react_agent)
graph.set_entry_point("agent")
graph.add_edge("agent", END)

app = graph.compile()

2026 年更新亮点

功能说明
增量 HNSW 构建生产环境中推荐系统场景实时性提升
多数据区域LangSmith 支持 US/EU/APAC
更好的错误处理2026 年强化