Skip to content

pydantic_graph.basenode

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.

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

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

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=object)

DepsT

Type variable for the dependencies of a graph and node.

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

RunEndT

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

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

NodeRunEndT

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

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