Loading...
Loading...
Compare original and translation side by side
from langgraph.graph import StateGraph, START, END, MessagesState
from langchain_core.messages import AIMessage
class State(MessagesState):
pass
def chat_node(state: State):
return {"messages": [AIMessage(content="Hello!")]}
graph = StateGraph(State).add_node("chat", chat_node)
graph.add_edge(START, "chat").add_edge("chat", END)
app = graph.compile()from langgraph.graph import StateGraph, START, END, MessagesState
from langchain_core.messages import AIMessage
class State(MessagesState):
pass
def chat_node(state: State):
return {"messages": [AIMessage(content="Hello!")]}
graph = StateGraph(State).add_node("chat", chat_node)
graph.add_edge(START, "chat").add_edge("chat", END)
app = graph.compile()MessagesStatemessagesadd_messagesfrom langgraph.graph import MessagesState
class State(MessagesState):
documents: list[str]
query: strMessagesStateadd_messagesmessagesfrom langgraph.graph import MessagesState
class State(MessagesState):
documents: list[str]
query: strimport { StateGraph, StateSchema, MessagesValue, ReducedValue, START, END } from "@langchain/langgraph";
import { AIMessage } from "@langchain/core/messages";
import { z } from "zod/v4";
const State = new StateSchema({
messages: MessagesValue,
documents: z.array(z.string()).default(() => []),
count: new ReducedValue(
z.number().default(0),
{ reducer: (current, update) => current + update }
),
});
const graph = new StateGraph(State)
.addNode("chat", (state) => ({ messages: [new AIMessage("Hello!")] }))
.addEdge(START, "chat")
.addEdge("chat", END)
.compile();import { StateGraph, StateSchema, MessagesValue, ReducedValue, START, END } from "@langchain/langgraph";
import { AIMessage } from "@langchain/core/messages";
import { z } from "zod/v4";
const State = new StateSchema({
messages: MessagesValue,
documents: z.array(z.string()).default(() => []),
count: new ReducedValue(
z.number().default(0),
{ reducer: (current, update) => current + update }
),
});
const graph = new StateGraph(State)
.addNode("chat", (state) => ({ messages: [new AIMessage("Hello!")] }))
.addEdge(START, "chat")
.addEdge("chat", END)
.compile();| Pattern | Use Case | Key Fields |
|---|---|---|
| Chat | Conversational agents | Built-in |
| Research | Information gathering | |
| Workflow | Task orchestration | |
| Tool-Calling | Agents with tools | |
| RAG | Retrieval-augmented generation | |
assets/assets/chat_state.pyassets/research_state.pyassets/workflow_state.pyassets/tool_calling_state.py| 模式 | 适用场景 | 核心字段 |
|---|---|---|
| 聊天 | 对话式Agent | 来自 |
| 研究 | 信息收集 | |
| 工作流 | 任务编排 | |
| 工具调用 | 带工具的Agent | |
| RAG | 检索增强生成 | |
assets/assets/chat_state.pyassets/research_state.pyassets/workflow_state.pyassets/tool_calling_state.py(existing_value, new_value)(existing_value, new_value)from typing import Annotated
import operator
from langgraph.graph import MessagesState
class State(MessagesState):
# Overwrite (no reducer)
query: str
# Sum integers
count: Annotated[int, operator.add]
# Custom reducer
results: Annotated[list[str], lambda left, right: left + right]from typing import Annotated
import operator
from langgraph.graph import MessagesState
class State(MessagesState):
# 覆盖(无reducer)
query: str
# 整数求和
count: Annotated[int, operator.add]
# 自定义reducer
results: Annotated[list[str], lambda left, right: left + right]const State = new StateSchema({
query: z.string(), // Last-write-wins
messages: MessagesValue, // Built-in message reducer
count: new ReducedValue( // Custom reducer
z.number().default(0),
{ reducer: (current, update) => current + update }
),
});const State = new StateSchema({
query: z.string(), // 最后写入者获胜
messages: MessagesValue, // 内置消息reducer
count: new ReducedValue( // 自定义reducer
z.number().default(0),
{ reducer: (current, update) => current + update }
),
});| Reducer | Import | Behavior |
|---|---|---|
| | Append, update by ID, delete |
| | Numeric addition or list concatenation |
| | JS equivalent of |
| Reducer | 导入路径 | 行为 |
|---|---|---|
| | 追加、按ID更新、删除 |
| | 数值相加或列表拼接 |
| | JS版本的 |
from langgraph.types import Overwrite
def reset_messages(state: State):
return {"messages": Overwrite(["fresh start"])}from langgraph.types import Overwrite
def reset_messages(state: State):
return {"messages": Overwrite(["fresh start"])}from langchain_core.messages import RemoveMessage
from langgraph.graph.message import REMOVE_ALL_MESSAGESfrom langchain_core.messages import RemoveMessage
from langgraph.graph.message import REMOVE_ALL_MESSAGES
For advanced reducer patterns (deduplication, deep merge, conditional update, size-limited accumulators), see [references/reducers.md](references/reducers.md).
高级Reducer模式(去重、深度合并、条件更新、限长累加器)请参考[references/reducers.md](references/reducers.md)。| Backend | Package | Use Case |
|---|---|---|
| InMemorySaver | | Development, testing |
| SqliteSaver | | Local workflows, single-instance |
| PostgresSaver | | Production, multi-instance |
| CosmosDBSaver | | Azure production |
Agent Server note: When using LangGraph Agent Server, checkpointers are configured automatically — no manual setup needed.
| 后端 | 包 | 适用场景 |
|---|---|---|
| InMemorySaver | | 开发、测试 |
| SqliteSaver | | 本地工作流、单实例 |
| PostgresSaver | | 生产环境、多实例 |
| CosmosDBSaver | | Azure生产环境 |
Agent Server注意事项:使用LangGraph Agent Server时,检查点工具会自动配置,无需手动设置。
undefinedundefinedresult = graph.invoke(
{"messages": [{"role": "user", "content": "Hi"}]},
{"configurable": {"thread_id": "session-1"}}
)undefinedresult = graph.invoke(
{"messages": [{"role": "user", "content": "Hi"}]},
{"configurable": {"thread_id": "session-1"}}
)undefined// Development
import { MemorySaver } from "@langchain/langgraph";
const graph = builder.compile({ checkpointer: new MemorySaver() });
// Production (PostgreSQL)
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
const checkpointer = PostgresSaver.fromConnString(DB_URI);
// await checkpointer.setup(); // Run once
const graph = builder.compile({ checkpointer });// 开发环境
import { MemorySaver } from "@langchain/langgraph";
const graph = builder.compile({ checkpointer: new MemorySaver() });
// 生产环境(PostgreSQL)
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
const checkpointer = PostgresSaver.fromConnString(DB_URI);
// await checkpointer.setup(); // 首次运行初始化
const graph = builder.compile({ checkpointer });thread_idconfig = {"configurable": {"thread_id": "user-123-session-1"}}
result = graph.invoke({"messages": [...]}, config)thread_idconfig = {"configurable": {"thread_id": "user-123-session-1"}}
result = graph.invoke({"messages": [...]}, config)parent_graph = parent_builder.compile(checkpointer=checkpointer)parent_graph = parent_builder.compile(checkpointer=checkpointer)
To give a subgraph its own separate memory:
```python
subgraph = sub_builder.compile(checkpointer=True)
要为子图设置独立的内存:
```python
subgraph = sub_builder.compile(checkpointer=True)from typing import TypedDict, Annotated, Literal
class AgentState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
next: Literal["agent1", "agent2", "FINISH"]
context: dictNote:state schemas supportcreate_agentfor custom agent state. Prefer TypedDict for agent state extensions.TypedDict
from typing import TypedDict, Annotated, Literal
class AgentState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
next: Literal["agent1", "agent2", "FINISH"]
context: dict注意:状态模式支持create_agent用于自定义Agent状态。扩展Agent状态时优先使用TypedDict。TypedDict
import { StateSchema, MessagesValue, ReducedValue, UntrackedValue } from "@langchain/langgraph";
import { z } from "zod/v4";
const AgentState = new StateSchema({
messages: MessagesValue,
currentStep: z.string(),
retryCount: z.number().default(0),
// Custom reducer
allSteps: new ReducedValue(
z.array(z.string()).default(() => []),
{ inputSchema: z.string(), reducer: (current, newStep) => [...current, newStep] }
),
// Transient state (not checkpointed)
tempCache: new UntrackedValue(z.record(z.string(), z.unknown())),
});
// Extract types for use outside the graph builder
type State = typeof AgentState.State;
type Update = typeof AgentState.Update;import { StateSchema, MessagesValue, ReducedValue, UntrackedValue } from "@langchain/langgraph";
import { z } from "zod/v4";
const AgentState = new StateSchema({
messages: MessagesValue,
currentStep: z.string(),
retryCount: z.number().default(0),
// 自定义reducer
allSteps: new ReducedValue(
z.array(z.string()).default(() => []),
{ inputSchema: z.string(), reducer: (current, newStep) => [...current, newStep] }
),
// 临时状态(不存入检查点)
tempCache: new UntrackedValue(z.record(z.string(), z.unknown())),
});
// 提取类型以供图构建器外部使用
type State = typeof AgentState.State;
type Update = typeof AgentState.Update;uv run scripts/validate_state_schema.py my_agent/state.py:MyState --verboseuv run scripts/validate_state_schema.py my_agent/state.py:MyState --verboseuv run scripts/test_reducers.py my_agent/reducers.py:extend_list --verboseuv run scripts/test_reducers.py my_agent/reducers.py:extend_list --verboseundefinedundefined
`inspect_checkpoints.py` accepts either a direct SQLite DB path or a directory containing `checkpoints.db`.
`inspect_checkpoints.py`支持直接传入SQLite数据库路径或包含`checkpoints.db`的目录。undefinedundefined
Migration script format:
```python
def migrate(old_state: dict) -> dict:
new_state = old_state.copy()
new_state["new_field"] = "default_value" # Add field
new_state.pop("deprecated_field", None) # Remove field
return new_state
迁移脚本格式:
```python
def migrate(old_state: dict) -> dict:
new_state = old_state.copy()
new_state["new_field"] = "default_value" # 添加字段
new_state.pop("deprecated_field", None) # 删除字段
return new_state| Symptom | Likely Cause | Fix |
|---|---|---|
| State not updating | Missing reducer | Add |
| Messages overwritten | No | Use |
| Duplicate entries | Reducer appends without dedup | Use dedup reducer from references/reducers.md |
| State grows unbounded | No cleanup | Use |
| Agent state schema rejected | Non-TypedDict | Use a |
| Parallel update conflict | Multiple | Only one node per super-step can use |
| 症状 | 可能原因 | 修复方案 |
|---|---|---|
| 状态未更新 | 缺少Reducer | 添加 |
| 消息被覆盖 | 无 | 使用 |
| 重复条目 | Reducer追加时未去重 | 使用references/reducers.md中的去重Reducer |
| 状态无限增长 | 无清理机制 | 使用 |
| Agent状态模式被拒绝 | | 使用 |
| 并行更新冲突 | 同一超级步骤中有多个 | 每个超级步骤仅允许一个节点使用 |
| Script | Purpose |
|---|---|
| Validate schema structure and typing |
| Test reducer functions |
| Inspect checkpoint data |
| Migrate checkpoint state values |
| 脚本 | 用途 |
|---|---|
| 验证模式结构和类型 |
| 测试Reducer函数 |
| 检查检查点数据 |
| 迁移检查点状态值 |
| File | Content |
|---|---|
| references/schema-patterns.md | Schema examples for chat, research, workflow, RAG, tool-calling |
| references/reducers.md | Reducer patterns, Overwrite, custom reducers, testing |
| references/persistence-backends.md | Backend setup, thread management, migration |
| references/state-typing.md | TypedDict, Pydantic, Zod, validation strategies |
| references/state-debugging.md | Debugging techniques, LangSmith tracing, common issues |
| 文件 | 内容 |
|---|---|
| references/schema-patterns.md | 聊天、研究、工作流、RAG、工具调用的模式示例 |
| references/reducers.md | Reducer模式、Overwrite、自定义Reducer、测试 |
| references/persistence-backends.md | 后端设置、线程管理、迁移 |
| references/state-typing.md | TypedDict、Pydantic、Zod、验证策略 |
| references/state-debugging.md | 调试技巧、LangSmith追踪、常见问题 |
| File | Pattern |
|---|---|
| Chat with |
| Research with custom reducers |
| Workflow with Literal status |
| Tool-calling agent with |
| 文件 | 模式 |
|---|---|
| 基于 |
| 带自定义Reducer的研究Agent |
| 带Literal状态的工作流 |
| 基于 |