【LangGraph】03-LangGraph之state

state通常用于保存当前会话中的消息,也可以自定义State用来存储额外的信息字段,state在节点之间传递,可以在节点中访问和修改state,此外LangGraph中的短期记忆通常使用state实现。

一、访问State中的消息

python 复制代码
from langgraph.graph import StateGraph, MessagesState, START, END

# 定义节点
def node1(state: MessagesState):
    print(state)
    print(state['messages'])

#定义State类型为MessagesState
graph = StateGraph(MessagesState)
graph.add_node("node1", node1)

graph.add_edge(START, "node1")
graph.add_edge("node1", END)

graph = graph.compile()
graph.invoke({"messages": [{"role": "user", "content": "hi!"}]})

输出结果:

{'messages': HumanMessage(content='hi!', additional_kwargs={}, response_metadata={}, id='6bcb92b6-d508-4ba4-996c-f05af82c1c2a')}

HumanMessage(content='hi!', additional_kwargs={}, response_metadata={}, id='6bcb92b6-d508-4ba4-996c-f05af82c1c2a')

  • MessagesState是内置的state类型,默认只有"messages"一个字段,可用于保存图执行过程中产生的消息。

  • 在节点中可以访问state中的消息

二、自定义state

python 复制代码
from typing import TypedDict
from langgraph.graph import StateGraph, MessagesState, START, END

# 自定义State
class State(TypedDict):
    # 注意:这里也可以定义message字段来接收上下文消息,大家可以自行尝试
    name: str
    age: int

def node1(state: State):
    print(state['name'])
    print(state['age'])
    # print(state['name'])
    # return state

graph = StateGraph(State)
graph.add_node("node1", node1)

graph.add_edge(START, "node1")
graph.add_edge("node1", END)

graph = graph.compile()
# 调用图时可传入State的内容
result = graph.invoke({"messages": [{"role": "user", "content": "hi!"}], "name": '张三', 'age': 18})
print(result)

输出结果:

张三

18

{'name': '张三', 'age': 18}

  • 自定义state可通过继承TypedDict来实现
  • 在调用图时,可传入state的内容字段

三、修改State

python 复制代码
from typing import TypedDict
from langgraph.graph import StateGraph, MessagesState, START, END

class State(TypedDict):
    name: str
    age: int

def node1(state: State):
    print(state['name'])
    print(state['age'])
    state['name'] = '李四'
    state['age'] += 1
    return state

graph = StateGraph(State)
graph.add_node("node1", node1)

graph.add_edge(START, "node1")
graph.add_edge("node1", END)

graph = graph.compile()
result = graph.invoke({"messages": [{"role": "user", "content": "hi!"}], "name": '张三', 'age': 18})
print(result)

输出结果:

张三

18

{'name': '李四', 'age': 19}

  • 可以在节点中修改state中的字段

  • 节点必须返回修改后的state,否则修改无效

四、总结

  1. state是用于保存当前会话的上下文信息的,不同会话间不能共享,可用于实现短期记忆
  2. MessageState保存图执行过程中产生的消息;
  3. 自定义state可以额外增加字段属性,当然也可以接受上下文的消息,上述案例为了简单起见只是定义了两个字段;
  4. 节点中可修改state,但需要返回修改后的State否则修改无效。
相关推荐
L@ncor18 小时前
第六章 框架开发实践 · 学习笔记(AutoGen / AgentScope / CAMEL / LangGraph)
python·框架·autogen·langgraph·agentscope
L@ncor1 天前
第六章可能出现的问题:依赖冲突与成本乘法
python·依赖管理·langgraph·agentscope·避坑
Java的搬运工1 天前
2026 AI Agent 实战:五层架构、MCP 工具调用与十条避坑清单 合集 - AI行业观察
人工智能·架构·智能体·大模型应用·langgraph·aiagent·mcp
梦想不只是梦与想2 天前
LangGraph图:State、Node、Edge(一)
langchain·大模型·langgraph
ysu_03149 天前
【2026】AI Agent 工程化落地六大挑战:从路径坍缩到成本失控
人工智能·ai·rag·ai agent·大模型应用·langgraph·agent工程化
归去来 兮12 天前
LangChain从入门到精通
python·langchain·langgraph
余槐i12 天前
从useState到Agent状态:React状态管理为何在AI工作流中失灵?
react·状态管理·ai agent·langgraph·tool calling
XGeFei12 天前
【LangGraph—5—Capabilities—Persistence】
langgraph
小马92618 天前
Agent 编排层的分岔:开源框架分工与托管 Harness 的工程选型
开源·langgraph
梦因you而美20 天前
LangChain-ReAct-Agent 智能客服系统 · 项目技术文档
langchain·agent·fastapi·扫地机器人·langgraph·rag 检索增强·react 智能客服