LangGraph 总览(2026年最新)
LangChain 官方有状态工作流引擎。2025年10月发布 1.0,2026年5月更新到 1.2.2。
最新版本(2026年)
| 版本 | 发布 | 说明 |
|---|
| LangGraph 1.2.2 | 2026年5月27日 | 2026年最新稳定版 |
| LangGraph 1.0 | 2025年10月 | 首个稳定版,与 LangChain 1.0 同步 |
| LangGraph 0.x | 2024年 | 测试版,已停止维护 |
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]
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 年强化 |