Gates

Gate nodes control execution flow. They make routing decisions but produce no data outputs.

  • IfElseNode - Binary gate for true/false routing decisions

  • @ifelse - Decorator to create an IfElseNode from a boolean function

  • RouteNode - Routes execution to target nodes based on a function's return value

  • @route - Decorator to create a RouteNode from a function

  • END - Sentinel indicating execution should terminate

  • GateNode - Abstract base class for all gate types

@ifelse Decorator

Create an IfElseNode from a boolean function. Simplest way to branch on true/false.

Signature

def ifelse(
    when_true: str | type[END],
    when_false: str | type[END],
    *,
    cache: bool = False,
    hide: bool = False,
    default_open: bool = True,
    name: str | None = None,
    rename_inputs: dict[str, str] | None = None,
    emit: str | tuple[str, ...] | None = None,
    wait_for: str | tuple[str, ...] | None = None,
) -> Callable[[Callable[..., bool]], IfElseNode]: ...

Parameters

Parameter
Type
Default
Description

when_true

str | END

required

Target when function returns True

when_false

str | END

required

Target when function returns False

cache

bool

False

Whether to cache routing decisions

hide

bool

False

Whether to hide from visualization

default_open

bool

True

If True, targets may execute before the gate runs. If False, targets are blocked until the gate executes.

name

str | None

None

Node name (default: function name)

rename_inputs

dict | None

None

Mapping to rename inputs {old: new}

emit

str | tuple | None

None

Ordering-only output(s). Auto-produced when the gate runs

wait_for

str | tuple | None

None

Ordering-only input(s). Gate waits until these values are fresh

Return Value

The decorated function must return exactly True or False (not truthy/falsy values):

  • True - Routes to when_true target

  • False - Routes to when_false target

Basic Usage

With END

Input Renaming


IfElseNode Class

Binary gate that routes based on boolean decision. Use @ifelse decorator for most cases.

Constructor

Properties

name: str

Public node name.

inputs: tuple[str, ...]

Input parameter names from function signature.

outputs: tuple[str, ...]

Always empty tuple. Gates produce no data outputs.

targets: list[str | type[END]]

Always [when_true, when_false] (2 elements).

when_true: str | type[END]

Target when function returns True.

when_false: str | type[END]

Target when function returns False.

descriptions: dict[bool, str]

Fixed {True: "True", False: "False"}.

cache: bool

Whether routing decisions are cached.

func: Callable

The wrapped boolean function.

is_async: bool

Always False. Routing functions must be synchronous.

is_generator: bool

Always False. Routing functions cannot be generators.

definition_hash: str

SHA256 hash of function source code.

Methods

has_default_for(param: str) -> bool

Check if parameter has a default value.

get_default_for(param: str) -> Any

Get default value for a parameter. Raises KeyError if no default.

with_name(name: str) -> IfElseNode

Return a new node with a different name.

with_inputs(mapping=None, /, **kwargs) -> IfElseNode

Return a new node with renamed inputs.

__call__(*args, **kwargs) -> bool

Call the boolean function directly.

__repr__() -> str

Informative string representation.


@route Decorator

Create a RouteNode from a routing function.

Signature

Parameters

Parameter
Type
Default
Description

targets

list or dict

required

Valid target node names. Dict values are descriptions for documentation.

fallback

str | END | None

None

Default target if function returns None

multi_target

bool

False

If True, function returns list of targets

cache

bool

False

Whether to cache routing decisions

hide

bool

False

Whether to hide from visualization

default_open

bool

True

If True, targets may execute before the gate runs. If False, targets are blocked until the gate executes.

name

str | None

None

Node name (default: function name)

rename_inputs

dict | None

None

Mapping to rename inputs {old: new}

emit

str | tuple | None

None

Ordering-only output(s). Auto-produced when the gate runs

wait_for

str | tuple | None

None

Ordering-only input(s). Gate waits until these values are fresh

Return Value

The decorated function should return:

  • str - Target node name to activate

  • END - Terminate execution along this path

  • None - Use fallback (if set) or activate no targets

  • list[str | END] - Multiple targets (only with multi_target=True)

Basic Usage

With Descriptions

With Fallback

Multi-Target Mode

Note: When multi_target=True, target nodes must have unique output names.


RouteNode Class

Concrete gate that routes to target nodes based on a routing function.

Constructor

Parameters are the same as @route. Use the decorator for most cases.

Properties

name: str

Public node name.

inputs: tuple[str, ...]

Input parameter names from function signature.

outputs: tuple[str, ...]

Always empty tuple. Gates produce no data outputs.

targets: list[str | type[END]]

List of valid target names.

descriptions: dict[str | type[END], str]

Target descriptions (empty if not provided).

fallback: str | type[END] | None

Default target when function returns None.

multi_target: bool

Whether function returns list of targets.

cache: bool

Whether routing decisions are cached.

func: Callable

The wrapped routing function.

is_async: bool

Always False. Routing functions must be synchronous.

is_generator: bool

Always False. Routing functions cannot be generators.

definition_hash: str

SHA256 hash of function source code.

Methods

has_default_for(param: str) -> bool

Check if parameter has a default value.

get_default_for(param: str) -> Any

Get default value for a parameter.

with_name(name: str) -> RouteNode

Return a new node with a different name.

with_inputs(mapping=None, /, **kwargs) -> RouteNode

Return a new node with renamed inputs.

__call__(*args, **kwargs)

Call the routing function directly.

__repr__() -> str

Informative string representation.


END Sentinel

Class indicating execution should terminate along this path.

Usage

Properties

  • END is a class, not an instance. Use it directly.

  • Cannot be instantiated: END() raises TypeError

  • String representation: "END"

Checking for END


GateNode Base Class

Abstract base class for all gate types. You typically won't use this directly.

Attributes

All GateNode subclasses have:


Validation Errors

At Decoration Time

Async functions not allowed:

Generator functions not allowed:

Empty targets:

Fallback with multi_target:

IfElseNode with same targets:

String 'END' as target:

At Graph Build Time

Invalid target (not a node in graph):

Self-targeting:

Multi-target with shared outputs:

At Runtime

Invalid return value:

Wrong return type for multi_target:

IfElseNode returns non-bool:

IfElseNode strictly validates boolean returns. 1, "yes", or any truthy value that isn't True will fail.


Complete Example

Last updated