teff.flow.control¶
teff.flow.control
¶
Branching, looping and routing builders for :class:~teff.flow.Flow.
:class:ControlBuilder implements the control-flow methods — parallel,
map, branch, interrupt, loop, route, command —
that wire edges between nodes. :class:Flow owns an instance and
delegates to it.
Classes:
| Name | Description |
|---|---|
ControlBuilder |
Build control-flow edges on a :class: |
ControlBuilder
¶
Build control-flow edges on a :class:~teff.flow.Flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
Flow
|
The owning |
required |
Methods:
| Name | Description |
|---|---|
add_flow |
Embed a sub-flow as a single node (SubFlow). |
branch |
Add conditional branching from the last added node. |
command |
Add a declarative |
converge |
Merge all branch ends into a single node. |
default |
Add a fallback node for the most recent guarded |
interrupt |
Pause the flow for human input at this point. |
interrupt_loop |
Ask the human through an interrupt and re-ask until the answer passes. |
loop |
Run a chain repeatedly until |
map |
Dynamically fan a state list out across parallel branches. |
parallel |
Run several branch chains concurrently from the last node. |
route |
Route between agent chains under a supervisor decider. |
Source code in teff/flow/control.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | |
add_flow
¶
add_flow(flow, id=None, **kw)
Embed a sub-flow as a single node (SubFlow).
The flow is compiled and wrapped in a SubFlow node.
Pass input_map / output_map as keyword arguments for key remapping.
Pass max_iterations to limit internal steps (see :class:SubFlow).
id optionally names the node in the compiled graph.
Source code in teff/flow/control.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
branch
¶
branch(key, *cases, default=None)
Add conditional branching from the last added node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
State key to evaluate. |
required |
*cases
|
One or more Case objects, each with a value. |
()
|
|
default
|
Optional fallback node (catches unmatched values). |
None
|
Each case creates an edge key=<case.value> from the last node.
The default creates an edge key!=<all case values>.
Multiple nodes within a case are chained sequentially.
Use converge() after branching to merge branches.
Source code in teff/flow/control.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
command
¶
command(*, routes=None, goto=None, update=None, id=None)
Add a declarative command node that routes by state.
Equivalent to step(CommandNode(...)) but resolves goto
/routes[].goto names through any :meth:label labels, so a
sugar route can jump back to a loop's decision point::
flow.loop(key="verdict", until="pass", body=body, done=done)
flow.label("refine")
flow.command(
routes=[{"when": "decision=rework", "goto": "refine"}],
goto="STOP",
)
Returns self for chaining.
Source code in teff/flow/control.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 | |
converge
¶
converge(node, id=None)
Merge all branch ends into a single node.
Adds edges from every branch end (set by the last branch()
call) to node. Use after branch() to rejoin paths::
flow.branch("sentiment",
Case("positive").add(on_pos),
Case("negative").add(on_neg),
).converge(shout_node)
id optionally names the node in the compiled graph.
Source code in teff/flow/control.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
default
¶
default(node, id=None)
Add a fallback node for the most recent guarded step().
The fallback fires only when the preceding step(..., when=...)
guard fails (the conditional edge wins when it matches)::
flow.step(decider)
flow.step(handler, when=lambda s: s["ok"])
flow.default(else_handler)
id optionally names the node in the compiled graph.
Source code in teff/flow/control.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
interrupt
¶
interrupt(key, prompt='', *, accept=None, id=None)
Pause the flow for human input at this point.
Appends an :class:~teff.node.interrupt.Interrupt node. When
execution reaches it, graph.run() raises
:class:~teff.node.interrupt.GraphInterrupt; resume by calling
run() again with the same checkpoint_id and a resume
dict mapping key to the operator's answer::
try:
await graph.run(state, checkpointer=cp, checkpoint_id="run-1")
except GraphInterrupt as interrupt:
print(interrupt.prompt)
answer = input("> ")
await graph.run(
state, checkpointer=cp,
checkpoint_id="run-1", resume={key: answer},
)
With accept (an :class:~teff.node.ask.Ask strategy) the raw
answer is not enough: an optional classifier
(:class:~teff.node.LLM) normalizes free-form answers into a
structured verdict, and a :class:~teff.node.ask.Validate node
decodes it into <accept.decision_key> (and captures an
arbitrary value into accept.value_key), so "yes", "ok",
"fine" all count as accept.pass_value. See
:meth:interrupt_loop for re-asking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
State key that receives the resume value. |
required |
prompt
|
str
|
Human-readable question shown to the operator. |
''
|
accept
|
Optional :class: |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in teff/flow/control.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
interrupt_loop
¶
interrupt_loop(key, *, accept, body, done, prompt='', id=None)
Ask the human through an interrupt and re-ask until the answer passes.
Composes an interrupt plus an :class:~teff.node.ask.Ask validation
strategy into one re-askable unit:
- An :class:
~teff.node.Interruptpauses the run and surfaces prompt; the operator's resume value lands in key. - If the strategy is
model, an :class:~teff.node.LLMnormalizes the free-form answer into a structured verdict object and a :class:~teff.node.ask.Validatenode decodes it intoaccept.decision_key(capturing an arbitrary value intoaccept.value_keywhen set) — so "yes", "fine", "ok" all count as accept.pass_value. - Otherwise the raw answer in key is matched by the strategy
(
equals/any_of/regex/check).
Wires::
decision --<decision_key>=<pass_value>--> done (continue)
decision --<decision_key>=<clarify_value>--> interrupt (re-ask, no body)
decision --<decision_key>!=<pass_value>--> body -> interrupt -> decision (re-ask)
body typically re-runs whatever produced key (e.g. a planner)
plus the ask nodes, so a "no" answer regenerates and re-asks; done
runs once the answer passes. When the model strategy declares
clear_field / clarify_value, an unclear answer (e.g. gibberish)
routes back to the interrupt for a plain re-ask without re-running
body — free-form replies never trigger an unwanted re-plan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
State key receiving the interrupt resume value. |
required |
accept
|
:class: |
required | |
prompt
|
str
|
Question shown to the operator. |
''
|
body
|
Chain re-run while the loop continues (fail branch). |
required | |
done
|
Chain run when the loop terminates. |
required | |
id
|
str | None
|
Prefix for the interrupt/classifier/validate node ids. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in teff/flow/control.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | |
loop
¶
loop(key, until, done, body, *, max_rounds=None)
Run a chain repeatedly until state[key] equals until.
Repeats the body chain, then checks a condition on
state[key]. When the value equals until, execution
proceeds to the done chain and continues after the loop;
otherwise the body chain runs and loops back to the decider
(the last node before this call)::
flow.step(draft_llm)
flow.interrupt("approved", "Approve?") # decider
flow.loop(
key="approved", until="yes",
done=final_llm, body=edit_llm,
)
Wires::
decider --key=until--> done -> ... (continue after loop)
decider --key!=until--> body -> ... -> decider (repeat)
The decider is any node that writes key (an Interrupt
whose resume value lands there, an LLM, a Transform, …).
Passing max_rounds bounds the repetition: the loop is then
compiled as a self-contained loop node that retries body at
most max_rounds times before giving up on until (a safe guard
against a body that can never reach until in the free-flow graph
form).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
State key to check. |
required |
until
|
str
|
Value of key that stops the loop. |
required |
done
|
Node or chain run when the loop terminates. |
required | |
body
|
Node or chain repeated while the loop continues. |
required | |
max_rounds
|
int | None
|
Maximum body re-runs before the loop gives up. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in teff/flow/control.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
map
¶
map(
processor,
*,
input_keys="",
output_key="",
chunk_size=None,
max_concurrency=None,
id=None,
**kwargs,
)
Dynamically fan a state list out across parallel branches.
Runs processor concurrently over each item of the list(s) at
input_keys, gathering the per-item results into a list at
output_key. Branch count is derived from the data at runtime
(see :class:~teff.node.map.Map)::
flow.map(
LLM(model="llama3.1:8b", input_key="chunk", output_key="summary"),
input_keys=["chunks"],
output_key="summaries",
)
id optionally names the node in the compiled graph.
Source code in teff/flow/control.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
parallel
¶
parallel(*branches, id=None)
Run several branch chains concurrently from the last node.
Each branch is a single :class:Node, a list of nodes (run
sequentially inside the branch), or a :class:Flow (embedded as
a :class:SubFlow). Branches execute via asyncio.gather on
isolated copies of the state; per-key reducers (append etc.)
merge their updates back without overwriting one another.
Combine with converge() to rejoin the parallel paths::
flow.parallel(
[Transform(action="uppercase", input_key="a", output_key="a")],
[Transform(action="uppercase", input_key="b", output_key="b")],
).converge(shout_node)
id optionally names the node in the compiled graph.
Source code in teff/flow/control.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
route
¶
route(key, *, finish=None, **agents)
Route between agent chains under a supervisor decider.
Wires the last added node (the decider) into a supervisor-style
loop. The decider writes key (e.g. "next_agent"); each
keyword in agents maps a value of key to the chain run for
it, and after that chain finishes control returns to the decider.
When key equals "finish" the loop exits through finish::
flow.step(supervisor) # LLM writing "next_agent"
flow.route(
"next_agent",
finish=final_llm,
planner=planner_chain,
estimator=estimator_chain,
)
Wires::
supervisor --next_agent=planner--> planner-chain -> supervisor
supervisor --next_agent=estimator--> estimator-chain -> supervisor
supervisor --next_agent=finish--> finish-chain -> (continue)
The finish chain is optional. When omitted the flow simply
terminates when key equals "finish" and no further nodes
may be chained; pass finish=<chain> to run something on exit
and keep building the flow afterwards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
State key written by the decider (the node last added before this call). |
required |
finish
|
Chain ( |
None
|
|
**agents
|
Each keyword is a value of key; its value is the
chain ( |
{}
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in teff/flow/control.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | |