Skip to content

ConditionNode

nae.ConditionNode

ConditionNode(name: Optional[str] = None, condition: Optional[Callable] = None, choices: List[str] = [], reads: Optional[List[str]] = None, cache_ttl: Optional[int] = None, retry: bool = False)

Bases: RouterNode, Node

Routing by a Python predicate over state — no LLM, no prompt, free and deterministic. condition(state) must return one of choices.

The code sibling of DecisionNode: reach for it whenever the branch is decidable in Python (a length check, a loop counter, a flag another node wrote) — you get zero-cost, reproducible routing with no model call. Wire it by indexing a choice, exactly like a DecisionNode: route["short"] > handler. The chosen label is written to the per-node decision_{name} channel, never into messages.

Examples:

from nae import AgentNode, ConditionNode, AgenticGraph

def is_short(state):
    return "short" if len(state["messages"][-1].content) < 20 else "long"

route = ConditionNode(condition=is_short, choices=["short", "long"])
brief = AgentNode(llm=llm, node_prompt="Give a one-line answer.")
detailed = AgentNode(llm=llm, node_prompt="Give a thorough answer.")

route["short"] > brief
route["long"] > detailed

graph = AgenticGraph(start_node=route, end_nodes={brief, detailed})

Initialize a ConditionNode.

Parameters:

Name Type Description Default
name Optional[str]

Unique identifier for the node

None
condition Optional[Callable]

Callable condition(state) -> str returning one of choices (the routing label).

None
choices List[str]

List of possible routing labels.

[]
reads Optional[List[str]]

State keys this node reads — default [] (the callable reads state directly). Pass reads=[...] so the validator can check those keys are available where this node runs.

None
cache_ttl Optional[int]

see Node — node-level result caching.

None
retry bool

see Node — node-level retry on exception.

False

Raises:

Type Description
AssertionError

If condition or choices is empty

__call__

__call__(state)

Evaluate the predicate and emit the routing label.

Calls condition(state), validates the result is one of choices (raising ValueError otherwise), and writes it to the per-node decision_{name} field that route() reads. Returns only the delta.