-
Notifications
You must be signed in to change notification settings - Fork 0
How To Route with BranchPipe
Fernando Koch edited this page Mar 30, 2026
·
2 revisions
Use BranchPipe when exactly one matching branch should execute.
This is the GraphK tool for routing-style control flow.
graphk.BranchPipe contains multiple branch pipelines and multiple matcher tests.
At execution time it:
- resolves the active session and scoped context
- evaluates each branch matcher
- collects all matching branch indices
- selects one branch according to the selection policy
- executes only that branch
Good use cases:
- route one request to one handler
- choose one workflow path based on state
- dispatch to one pipeline based on scoped context
- implement controlled alternatives where only one result path should run
Use BranchPipe when exactly one route should be chosen.
Use MultiPipe when multiple routes should execute.
Important constants:
BranchPipe.SELECT_FIRSTBranchPipe.SELECT_RANDOM
Use when:
- matching order should determine the winner
- routing must be deterministic
Use when:
- nondeterministic branch selection is intentional
- or when randomness is explicitly seeded for testing/demo purposes
import graphk
branch = graphk.BranchPipe(
context=graphk.Context(route="alpha"),
selection_policy=graphk.BranchPipe.SELECT_FIRST,
nodes=[
(
graphk.Matcher(task="route", **{"@Pipeline/route": "alpha", "@Runner/mode": "strict"}),
graphk.SequencePipe(nodes=[graphk.demo.RouteNode("A")]),
),
(
graphk.Matcher(task="route", **{"@Pipeline/route": "beta", "@Runner/mode": "strict"}),
graphk.SequencePipe(nodes=[graphk.demo.RouteNode("B")]),
),
],
)
pipeline = graphk.SequencePipe(nodes=[branch])
session = graphk.Session(task="route")
runner = graphk.Runner(pipeline, session)
runner.start(context=graphk.Context(mode="strict"))
runner.run()
print(session.to_dict())Each branch is expected to be a Pipeline, not just a raw node.
This gives each route its own composable execution space and allows:
- one-node branches
- multi-node branches
- nested sub-workflows per route
Choose handler A or B depending on route, task, mode, or user state.
Choose one of several processing strategies based on session values.
Select one branch by deterministic priority or seeded randomness.
- Do not put raw nodes directly into branch definitions; use branch pipelines.
- Do not expect all matching branches to execute;
BranchPipeselects only one. - Do not use
SELECT_RANDOMunless nondeterministic behavior is actually desired.
Recommended references: