diff --git a/libs/langgraph/langgraph/graph/state.py b/libs/langgraph/langgraph/graph/state.py index f3299de92..608c492b0 100644 --- a/libs/langgraph/langgraph/graph/state.py +++ b/libs/langgraph/langgraph/graph/state.py @@ -300,7 +300,56 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): **kwargs: Unpack[DeprecatedKwargs], ) -> Self: """Add a new node to the `StateGraph`, input schema is inferred as the state schema. + Will take the name of the function/runnable as the node name. + + Args: + node: The function or runnable this node will run. + defer: Whether to defer the execution of the node until the run is about to end. + metadata: The metadata associated with the node. + input_schema: The input schema for the node. (Default: the graph's state schema) + retry_policy: The retry policy for the node. + + If a sequence is provided, the first matching policy will be applied. + cache_policy: The cache policy for the node. + destinations: Destinations that indicate where a node can route to. + + Useful for edgeless graphs with nodes that return `Command` objects. + + If a `dict` is provided, the keys will be used as the target node names and the values will be used as the labels for the edges. + + If a `tuple` is provided, the values will be used as the target node names. + + !!! warning + + This is only used for graph rendering and doesn't have any effect on the graph execution. + + Example: + ```python + from typing_extensions import TypedDict + + from langchain_core.runnables import RunnableConfig + from langgraph.graph import START, StateGraph + + + class State(TypedDict): + x: int + + + def my_node(state: State, config: RunnableConfig) -> State: + return {"x": state["x"] + 1} + + + builder = StateGraph(State) + builder.add_node(my_node) # node name will be 'my_node' + builder.add_edge(START, "my_node") + graph = builder.compile() + graph.invoke({"x": 1}) + # {'x': 2} + ``` + + Returns: + Self: The instance of the `StateGraph`, allowing for method chaining. """ ... @@ -317,8 +366,61 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): destinations: dict[str, str] | tuple[str, ...] | None = None, **kwargs: Unpack[DeprecatedKwargs], ) -> Self: - """Add a new node to the `StateGraph`, input schema is specified. + """Add a new node to the `StateGraph` where input schema is specified. + Will take the name of the function/runnable as the node name. + + Args: + node: The function or runnable this node will run. + defer: Whether to defer the execution of the node until the run is about to end. + metadata: The metadata associated with the node. + input_schema: The input schema for the node. + retry_policy: The retry policy for the node. + + If a sequence is provided, the first matching policy will be applied. + cache_policy: The cache policy for the node. + destinations: Destinations that indicate where a node can route to. + + Useful for edgeless graphs with nodes that return `Command` objects. + + If a `dict` is provided, the keys will be used as the target node names and the values will be used as the labels for the edges. + + If a `tuple` is provided, the values will be used as the target node names. + + !!! warning + + This is only used for graph rendering and doesn't have any effect on the graph execution. + + Example: + ```python + from typing_extensions import TypedDict + + from langchain_core.runnables import RunnableConfig + from langgraph.graph import START, StateGraph + + + class State(TypedDict): + x: int + + + class NodeInput(TypedDict): + x: int + + + def my_node(state: NodeInput, config: RunnableConfig) -> State: + return {"x": state["x"] + 1} + + + builder = StateGraph(State) + builder.add_node(my_node, input_schema=NodeInput) # node name will be 'my_node' + builder.add_edge(START, "my_node") + graph = builder.compile() + graph.invoke({"x": 1}) + # {'x': 2} + ``` + + Returns: + Self: The instance of the `StateGraph`, allowing for method chaining. """ ... @@ -336,7 +438,57 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): destinations: dict[str, str] | tuple[str, ...] | None = None, **kwargs: Unpack[DeprecatedKwargs], ) -> Self: - """Add a new node to the `StateGraph`, input schema is inferred as the state schema.""" + """Add a new node to the `StateGraph`, input schema is inferred as the state schema. + + Args: + node: The name of the node. + action: The function or runnable this node will run. + defer: Whether to defer the execution of the node until the run is about to end. + metadata: The metadata associated with the node. + input_schema: The input schema for the node. (Default: the graph's state schema) + retry_policy: The retry policy for the node. + + If a sequence is provided, the first matching policy will be applied. + cache_policy: The cache policy for the node. + destinations: Destinations that indicate where a node can route to. + + Useful for edgeless graphs with nodes that return `Command` objects. + + If a `dict` is provided, the keys will be used as the target node names and the values will be used as the labels for the edges. + + If a `tuple` is provided, the values will be used as the target node names. + + !!! warning + + This is only used for graph rendering and doesn't have any effect on the graph execution. + + Example: + ```python + from typing_extensions import TypedDict + + from langchain_core.runnables import RunnableConfig + from langgraph.graph import START, StateGraph + + + class State(TypedDict): + x: int + + + def my_node(state: State, config: RunnableConfig) -> State: + return {"x": state["x"] + 1} + + + builder = StateGraph(State) + builder.add_node("my_fair_node", my_node) + builder.add_edge(START, "my_fair_node") + graph = builder.compile() + graph.invoke({"x": 1}) + # {'x': 2} + ``` + + Returns: + Self: The instance of the `StateGraph`, allowing for method chaining. + """ ... @overload @@ -353,7 +505,65 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): destinations: dict[str, str] | tuple[str, ...] | None = None, **kwargs: Unpack[DeprecatedKwargs], ) -> Self: - """Add a new node to the `StateGraph`, input schema is specified.""" + """Add a new node to the `StateGraph`, input schema is specified. + + Args: + node: The function or runnable this node will run. + + If a string is provided, it will be used as the node name, and action will be used as the function or runnable. + action: The action associated with the node. + + Will be used as the node function or runnable if `node` is a string (node name). + defer: Whether to defer the execution of the node until the run is about to end. + metadata: The metadata associated with the node. + input_schema: The input schema for the node. + retry_policy: The retry policy for the node. + + If a sequence is provided, the first matching policy will be applied. + cache_policy: The cache policy for the node. + destinations: Destinations that indicate where a node can route to. + + Useful for edgeless graphs with nodes that return `Command` objects. + + If a `dict` is provided, the keys will be used as the target node names and the values will be used as the labels for the edges. + + If a `tuple` is provided, the values will be used as the target node names. + + !!! warning + + This is only used for graph rendering and doesn't have any effect on the graph execution. + + Example: + ```python + from typing_extensions import TypedDict + + from langchain_core.runnables import RunnableConfig + from langgraph.graph import START, StateGraph + + + class State(TypedDict): + x: int + + + class NodeInput(TypedDict): + x: int + + + def my_node(state: NodeInput, config: RunnableConfig) -> State: + return {"x": state["x"] + 1} + + + builder = StateGraph(State) + builder.add_node("my_fair_node", my_node, input_schema=NodeInput) + builder.add_edge(START, "my_fair_node") + graph = builder.compile() + graph.invoke({"x": 1}) + # {'x': 2} + ``` + + Returns: + Self: The instance of the `StateGraph`, allowing for method chaining. + """ ... def add_node( @@ -376,6 +586,7 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): If a string is provided, it will be used as the node name, and action will be used as the function or runnable. action: The action associated with the node. + Will be used as the node function or runnable if `node` is a string (node name). defer: Whether to defer the execution of the node until the run is about to end. metadata: The metadata associated with the node. @@ -392,7 +603,7 @@ class StateGraph(Generic[StateT, ContextT, InputT, OutputT]): If a `tuple` is provided, the values will be used as the target node names. - !!! note + !!! warning This is only used for graph rendering and doesn't have any effect on the graph execution.