Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

LangGraph StateGraph 从多个节点收到了对不支持并发更新的状态属性的并发更新。 出现这种情况的一种方式是,当你在图中使用扇出或其他并行执行,并且你定义了如下所示的图:
class State(TypedDict):
    some_key: str

def node(state: State):
    return {"some_key": "some_string_value"}

def other_node(state: State):
    return {"some_key": "some_string_value"}


builder = StateGraph(State)
builder.add_node(node)
builder.add_node(other_node)
builder.add_edge(START, "node")
builder.add_edge(START, "other_node")
graph = builder.compile()
如果上述图中的一个节点返回 { "some_key": "some_string_value" },这将用 "some_string_value" 覆盖 "some_key" 的状态值。 然而,如果在单个步骤中(例如在扇出中)多个节点返回了 "some_key" 的值,图将抛出此错误,因为无法确定如何更新内部状态。 要解决此问题,你可以定义一个合并多个值的归约器(reducer):
import operator
from typing import Annotated

class State(TypedDict):
    # operator.add 归约函数使其变为仅追加模式  #
    some_key: Annotated[list, operator.add]
这将允许你定义处理从并行执行的多个节点返回的相同键的逻辑。

故障排除

以下方法可能有助于解决此错误:
  • 如果你的图并行执行节点,请确保你已为相关状态键定义了归约器(reducer)。