Skip to content

Commit e3d12f5

Browse files
committed
docs: add docstrings to add_node overloads
1 parent 630bd9a commit e3d12f5

File tree

1 file changed

+215
-4
lines changed

1 file changed

+215
-4
lines changed

libs/langgraph/langgraph/graph/state.py

Lines changed: 215 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,56 @@ def add_node(
299299
**kwargs: Unpack[DeprecatedKwargs],
300300
) -> Self:
301301
"""Add a new node to the `StateGraph`, input schema is inferred as the state schema.
302+
302303
Will take the name of the function/runnable as the node name.
304+
305+
Args:
306+
node: The function or runnable this node will run.
307+
defer: Whether to defer the execution of the node until the run is about to end.
308+
metadata: The metadata associated with the node.
309+
input_schema: The input schema for the node. (Default: the graph's state schema)
310+
retry_policy: The retry policy for the node.
311+
312+
If a sequence is provided, the first matching policy will be applied.
313+
cache_policy: The cache policy for the node.
314+
destinations: Destinations that indicate where a node can route to.
315+
316+
Useful for edgeless graphs with nodes that return `Command` objects.
317+
318+
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.
319+
320+
If a `tuple` is provided, the values will be used as the target node names.
321+
322+
!!! warning
323+
324+
This is only used for graph rendering and doesn't have any effect on the graph execution.
325+
326+
Example:
327+
```python
328+
from typing_extensions import TypedDict
329+
330+
from langchain_core.runnables import RunnableConfig
331+
from langgraph.graph import START, StateGraph
332+
333+
334+
class State(TypedDict):
335+
x: int
336+
337+
338+
def my_node(state: State, config: RunnableConfig) -> State:
339+
return {"x": state["x"] + 1}
340+
341+
342+
builder = StateGraph(State)
343+
builder.add_node(my_node) # node name will be 'my_node'
344+
builder.add_edge(START, "my_node")
345+
graph = builder.compile()
346+
graph.invoke({"x": 1})
347+
# {'x': 2}
348+
```
349+
350+
Returns:
351+
Self: The instance of the `StateGraph`, allowing for method chaining.
303352
"""
304353
...
305354

@@ -316,8 +365,61 @@ def add_node(
316365
destinations: dict[str, str] | tuple[str, ...] | None = None,
317366
**kwargs: Unpack[DeprecatedKwargs],
318367
) -> Self:
319-
"""Add a new node to the `StateGraph`, input schema is specified.
368+
"""Add a new node to the `StateGraph` where input schema is specified.
369+
320370
Will take the name of the function/runnable as the node name.
371+
372+
Args:
373+
node: The function or runnable this node will run.
374+
defer: Whether to defer the execution of the node until the run is about to end.
375+
metadata: The metadata associated with the node.
376+
input_schema: The input schema for the node.
377+
retry_policy: The retry policy for the node.
378+
379+
If a sequence is provided, the first matching policy will be applied.
380+
cache_policy: The cache policy for the node.
381+
destinations: Destinations that indicate where a node can route to.
382+
383+
Useful for edgeless graphs with nodes that return `Command` objects.
384+
385+
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.
386+
387+
If a `tuple` is provided, the values will be used as the target node names.
388+
389+
!!! warning
390+
391+
This is only used for graph rendering and doesn't have any effect on the graph execution.
392+
393+
Example:
394+
```python
395+
from typing_extensions import TypedDict
396+
397+
from langchain_core.runnables import RunnableConfig
398+
from langgraph.graph import START, StateGraph
399+
400+
401+
class State(TypedDict):
402+
x: int
403+
404+
405+
class NodeInput(TypedDict):
406+
x: int
407+
408+
409+
def my_node(state: NodeInput, config: RunnableConfig) -> State:
410+
return {"x": state["x"] + 1}
411+
412+
413+
builder = StateGraph(State)
414+
builder.add_node(my_node, input_schema=NodeInput) # node name will be 'my_node'
415+
builder.add_edge(START, "my_node")
416+
graph = builder.compile()
417+
graph.invoke({"x": 1})
418+
# {'x': 2}
419+
```
420+
421+
Returns:
422+
Self: The instance of the `StateGraph`, allowing for method chaining.
321423
"""
322424
...
323425

@@ -335,7 +437,57 @@ def add_node(
335437
destinations: dict[str, str] | tuple[str, ...] | None = None,
336438
**kwargs: Unpack[DeprecatedKwargs],
337439
) -> Self:
338-
"""Add a new node to the `StateGraph`, input schema is inferred as the state schema."""
440+
"""Add a new node to the `StateGraph`, input schema is inferred as the state schema.
441+
442+
Args:
443+
node: The name of the node.
444+
action: The function or runnable this node will run.
445+
defer: Whether to defer the execution of the node until the run is about to end.
446+
metadata: The metadata associated with the node.
447+
input_schema: The input schema for the node. (Default: the graph's state schema)
448+
retry_policy: The retry policy for the node.
449+
450+
If a sequence is provided, the first matching policy will be applied.
451+
cache_policy: The cache policy for the node.
452+
destinations: Destinations that indicate where a node can route to.
453+
454+
Useful for edgeless graphs with nodes that return `Command` objects.
455+
456+
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.
457+
458+
If a `tuple` is provided, the values will be used as the target node names.
459+
460+
!!! warning
461+
462+
This is only used for graph rendering and doesn't have any effect on the graph execution.
463+
464+
Example:
465+
```python
466+
from typing_extensions import TypedDict
467+
468+
from langchain_core.runnables import RunnableConfig
469+
from langgraph.graph import START, StateGraph
470+
471+
472+
class State(TypedDict):
473+
x: int
474+
475+
476+
def my_node(state: State, config: RunnableConfig) -> State:
477+
return {"x": state["x"] + 1}
478+
479+
480+
builder = StateGraph(State)
481+
builder.add_node("my_fair_node", my_node)
482+
builder.add_edge(START, "my_fair_node")
483+
graph = builder.compile()
484+
graph.invoke({"x": 1})
485+
# {'x': 2}
486+
```
487+
488+
Returns:
489+
Self: The instance of the `StateGraph`, allowing for method chaining.
490+
"""
339491
...
340492

341493
@overload
@@ -352,7 +504,65 @@ def add_node(
352504
destinations: dict[str, str] | tuple[str, ...] | None = None,
353505
**kwargs: Unpack[DeprecatedKwargs],
354506
) -> Self:
355-
"""Add a new node to the `StateGraph`, input schema is specified."""
507+
"""Add a new node to the `StateGraph`, input schema is specified.
508+
509+
Args:
510+
node: The function or runnable this node will run.
511+
512+
If a string is provided, it will be used as the node name, and action will be used as the function or runnable.
513+
action: The action associated with the node.
514+
515+
Will be used as the node function or runnable if `node` is a string (node name).
516+
defer: Whether to defer the execution of the node until the run is about to end.
517+
metadata: The metadata associated with the node.
518+
input_schema: The input schema for the node.
519+
retry_policy: The retry policy for the node.
520+
521+
If a sequence is provided, the first matching policy will be applied.
522+
cache_policy: The cache policy for the node.
523+
destinations: Destinations that indicate where a node can route to.
524+
525+
Useful for edgeless graphs with nodes that return `Command` objects.
526+
527+
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.
528+
529+
If a `tuple` is provided, the values will be used as the target node names.
530+
531+
!!! warning
532+
533+
This is only used for graph rendering and doesn't have any effect on the graph execution.
534+
535+
Example:
536+
```python
537+
from typing_extensions import TypedDict
538+
539+
from langchain_core.runnables import RunnableConfig
540+
from langgraph.graph import START, StateGraph
541+
542+
543+
class State(TypedDict):
544+
x: int
545+
546+
547+
class NodeInput(TypedDict):
548+
x: int
549+
550+
551+
def my_node(state: NodeInput, config: RunnableConfig) -> State:
552+
return {"x": state["x"] + 1}
553+
554+
555+
builder = StateGraph(State)
556+
builder.add_node("my_fair_node", my_node, input_schema=NodeInput)
557+
builder.add_edge(START, "my_fair_node")
558+
graph = builder.compile()
559+
graph.invoke({"x": 1})
560+
# {'x': 2}
561+
```
562+
563+
Returns:
564+
Self: The instance of the `StateGraph`, allowing for method chaining.
565+
"""
356566
...
357567

358568
def add_node(
@@ -375,6 +585,7 @@ def add_node(
375585
376586
If a string is provided, it will be used as the node name, and action will be used as the function or runnable.
377587
action: The action associated with the node.
588+
378589
Will be used as the node function or runnable if `node` is a string (node name).
379590
defer: Whether to defer the execution of the node until the run is about to end.
380591
metadata: The metadata associated with the node.
@@ -391,7 +602,7 @@ def add_node(
391602
392603
If a `tuple` is provided, the values will be used as the target node names.
393604
394-
!!! note
605+
!!! warning
395606
396607
This is only used for graph rendering and doesn't have any effect on the graph execution.
397608

0 commit comments

Comments
 (0)