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 |
None
|
choices
|
List[str]
|
List of possible routing labels. |
[]
|
reads
|
Optional[List[str]]
|
State keys this node reads — default |
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__ ¶
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.