Skip to content

pydantic_graph.nodes

GraphRunContext

Bases: Generic[StateT, DepsT]

Context for a graph.

Attributes

state

The state of the graph.

Type: StateT

deps

Dependencies for the graph.

Type: DepsT

BaseNode

Bases: ABC, Generic[StateT, DepsT, NodeRunEndT]

Base class for a node.

Attributes

docstring_notes

Set to True to generate mermaid diagram notes from the class’s docstring.

While this can add valuable information to the diagram, it can make diagrams harder to view, hence it is disabled by default. You can also customise notes overriding the get_note method.

Type: bool Default: False

Methods

run

@abstractmethod

@async

def run(
    ctx: GraphRunContext[StateT, DepsT],
) -> BaseNode[StateT, DepsT, Any] | End[NodeRunEndT]

Run the node.

This is an abstract method that must be implemented by subclasses.

Returns

BaseNode[StateT, DepsT, Any] | End[NodeRunEndT] — The next node to run or End to signal the end of the graph.

Parameters

ctx : GraphRunContext[StateT, DepsT]

The graph context.

get_node_id

@cached

@classmethod

def get_node_id(cls) -> str

Get the ID of the node.

Returns

str

get_note

@classmethod

def get_note(cls) -> str | None

Get a note about the node to render on mermaid charts.

By default, this returns a note only if docstring_notes is True. You can override this method to customise the node notes.

Returns

str | None

get_node_def

@classmethod

def get_node_def(
    cls,
    local_ns: dict[str, Any] | None,
) -> NodeDef[StateT, DepsT, NodeRunEndT]

Get the node definition.

Returns

NodeDef[StateT, DepsT, NodeRunEndT]

deep_copy
def deep_copy() -> Self

Returns a deep copy of the node.

Returns

Self

End

Bases: Generic[RunEndT]

Type to return from a node to signal the end of the graph.

Attributes

data

Data to return from the graph.

Type: RunEndT

Methods

deep_copy_data
def deep_copy_data() -> End[RunEndT]

Returns a deep copy of the end of the run.

Returns

End[RunEndT]

Edge

Annotation to apply a label to an edge in a graph.

Attributes

label

Label for the edge.

Type: str | None

StateT

Type variable for the state in a graph.

Default: TypeVar('StateT', default=None)

DepsT

Type variable for the dependencies of a graph and node.

Default: TypeVar('DepsT', default=None, contravariant=True)

RunEndT

Covariant type variable for the return type of a graph run.

Default: TypeVar('RunEndT', covariant=True, default=None)

NodeRunEndT

Covariant type variable for the return type of a node run.

Default: TypeVar('NodeRunEndT', covariant=True, default=Never)