Skip to content

Flow and branches

Declare the edges between nodes and route by the state. See Graphs and flow.

Start, end and edges

START

START = StartSentinel()

The entry of a flow: START >> node names the first node of a graph.

END

END = EndSentinel()

The end of a flow: node >> END, a branch target, or a goto target.

FlowStartEdge

FlowStartEdge(target)

Edge from START to the first node.

Attributes
targetNode

FlowEdge

FlowEdge(source, target)

Static edge between two nodes, written as a >> b.

Attributes
sourceNode
targetNode | EndSentinel

FlowBranchEdge

FlowBranchEdge(source, branch)

Conditional edge, written as a >> branch(...).

Attributes
sourceNode
branchFlowBranch

StartSentinel

Marker for the entry of a graph flow, used as START >> node.

EndSentinel

Marker for the end of a graph flow.

Branches

branch

branch(router, mapping, *, ref=None) -> FlowBranch

Route to one of several targets by a key computed from the state.

Parameters
routerCallable[[Any], Any]

Receives the state and returns a key of mapping, matched by equality. Any other key raises GraphExecutionError at run time.

mappingMapping[Any, Node | EndSentinel]

Router keys to target nodes or END.

refstr | None

Import path of the router, for Graph.to_spec.

Raises
TypeError

If router is not callable.

GraphConfigError

If mapping is empty.

when

when(predicate, target, *, otherwise) -> FlowBranch

Route to target when a predicate holds, else to otherwise.

Parameters
predicateCallable[[Any], bool]

Receives the state and returns a bool; any other value raises GraphExecutionError at run time.

targetNode | EndSentinel

Target when the predicate is true.

otherwiseNode | EndSentinel

Target when the predicate is false.

Raises
TypeError

If predicate is not callable.

FlowBranch

FlowBranch(router, mapping, router_ref=None, requires_bool=False)

Conditional routing table produced by branch or when.

Attributes
routerCallable[[Any], Any]

Called with the state; its result picks the target in mapping.

mappingMapping[Any, Node | EndSentinel]

Router result to the next node or END.

router_refstr | None

module:qualname import path of the router, for Graph.to_spec.

requires_boolbool

The router must return a bool; set by when.