teff.graph¶
teff.graph
¶
Graph data structure for representing agent workflows.
The package is split into focused modules:
- :mod:
teff.graph.edge— the :class:~teff.graph.Edgemodel and hooks. - :mod:
teff.graph.conditions— edge condition evaluation. - :mod:
teff.graph.execution— the execution engine behindrun(). - :mod:
teff.graph.render— Mermaid / YAML serialization. - :mod:
teff.graph.graph— the :class:~teff.graph.Graphfacade.
Edge, Graph, and Hook are re-exported here for convenience.
Modules:
| Name | Description |
|---|---|
conditions |
Edge condition parsing and evaluation. |
edge |
Graph edge model and hook protocol. |
execution |
The graph execution engine. |
graph |
Graph data structure for representing agent workflows. |
render |
Graph serialization: Mermaid diagrams and YAML topology. |
Classes:
| Name | Description |
|---|---|
Edge |
A directed edge between two nodes with an optional condition. |
Graph |
A directed graph of nodes connected by edges with conditions. |
TurnResult |
Structured outcome of one interrupt-aware conversation turn. |
Attributes:
| Name | Type | Description |
|---|---|---|
Hook |
Signature for observability hooks: |
Hook
module-attribute
¶
Hook = Callable[..., Any]
Signature for observability hooks: (node_id, node, state).
Hooks may be synchronous or asynchronous — async hooks are awaited by the
executor. on_node_end additionally receives the result dict and
on_node_error additionally receives the exception.
Edge
dataclass
¶
A directed edge between two nodes with an optional condition.
Attributes:
| Name | Type | Description |
|---|---|---|
source_id |
str
|
ID of the source node. |
target_id |
str
|
ID of the target node. |
condition |
str | Callable[[dict], bool] | None
|
Expression |
Source code in teff/graph/edge.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
Graph
¶
A directed graph of nodes connected by edges with conditions.
The graph executes by walking from the entry_point node, following edges whose conditions match the current state, and shallow-merging each node's output back into the state.
Error handling::
Edge("parse", "fallback", "__error__") # catch exceptions
Observability hooks::
await graph.run(state, hooks={
"on_node_start": callback,
"on_node_end": callback,
"on_node_error": callback,
})
Hook callbacks receive (node_id, node, state).
on_node_end additionally receives the result dict and runs after
the result is merged into state (so it observes the node's effect).
on_node_error additionally receives the exception.
Hooks may be sync or async; async hooks are awaited.
Methods:
| Name | Description |
|---|---|
aclose |
Close connection-backed tools (e.g. MCP servers) opened by this |
get_state |
Return the durable state for checkpoint_id, or |
last_reply |
Return the latest assistant reply for session_id ( |
pending |
Return the interrupt this session is paused on, or |
run |
Execute the graph starting from the entry point. |
stream |
Stream events as the graph executes. |
to_mermaid |
Render this graph as a Mermaid flowchart diagram. |
to_yaml |
Serialize this graph to a YAML string. |
update_state |
Edit the durable state of an existing run and persist it. |
Source code in teff/graph/graph.py
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 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
aclose
async
¶
aclose()
Close connection-backed tools (e.g. MCP servers) opened by this graph. Idempotent; safe to call after a partial or cancelled run.
Conveniently, the graph is also an async context manager, so
async with graph: closes everything on exit::
async with graph:
result = await graph.run(state, tools=tools)
Source code in teff/graph/graph.py
136 137 138 139 140 141 142 143 144 145 146 147 148 | |
get_state
async
¶
get_state(checkpoint_id, *, checkpointer, owner=DEFAULT_OWNER)
Return the durable state for checkpoint_id, or None.
Reads the latest checkpoint (paused or completed). The internal
__interrupt__ bookkeeping key is stripped — use
:meth:pending to inspect a paused run's interrupt.
Pairs with :meth:update_state for human-in-the-loop
corrections: read the state, fix a value, save it back, then
resume with run(resume=...).
Source code in teff/graph/graph.py
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
last_reply
async
¶
last_reply(session_id, *, checkpointer, messages_key='messages', owner=DEFAULT_OWNER)
Return the latest assistant reply for session_id ("" if none).
Reads the durable checkpoint, so it works even for agents that do not
stream tokens (e.g. tool-using agents): the CLI prints this at the end
of a turn instead of relying on token events alone.
Source code in teff/graph/graph.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | |
pending
async
¶
pending(session_id, *, checkpointer, owner=DEFAULT_OWNER)
Return the interrupt this session is paused on, or None.
The interrupt bookkeeping lives in durable state: when run pauses
on an :class:~teff.node.Interrupt it writes a __interrupt__ entry
into the saved checkpoint. This reads it back so the caller — without
a try/except or an in-memory pending map — can tell whether the
next message is a fresh turn or the operator's answer to resume.
Source code in teff/graph/graph.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | |
run
async
¶
run(
state: dict | State,
tools: "Sequence[Tool | McpToolGroup] | None" = None,
registry: NodeRegistry | None = None,
reducers: dict[str, Reducer] | None = None,
hooks: dict[str, Callable] | None = None,
node_timeout: float | None = None,
max_iterations: int | None = None,
checkpointer: Checkpointer | None = None,
checkpoint_id: str | None = None,
owner: str = DEFAULT_OWNER,
resume: dict | None = None,
tracer: RunTracer | None = None,
state_schema: dict | None = None,
emit: "Callable[[StreamEvent], Awaitable[None]] | None" = None,
providers: "dict[str, Provider] | ProviderRegistry | None" = None,
default_provider: str | None = None,
default_model: str | None = None,
on_llm_payload: "Callable[..., Awaitable[None]] | None" = None,
*,
message: None = None,
initial_state: "Callable[[], Mapping[str, object]] | None" = None,
transient_keys: tuple[str, ...] = (),
messages_key: str = "messages",
) -> "dict | State"
run(
state: dict | State,
tools: "Sequence[Tool | McpToolGroup] | None" = None,
registry: NodeRegistry | None = None,
reducers: dict[str, Reducer] | None = None,
hooks: dict[str, Callable] | None = None,
node_timeout: float | None = None,
max_iterations: int | None = None,
checkpointer: Checkpointer | None = None,
checkpoint_id: str | None = None,
owner: str = DEFAULT_OWNER,
resume: dict | None = None,
tracer: RunTracer | None = None,
state_schema: dict | None = None,
emit: "Callable[[StreamEvent], Awaitable[None]] | None" = None,
providers: "dict[str, Provider] | ProviderRegistry | None" = None,
default_provider: str | None = None,
default_model: str | None = None,
on_llm_payload: "Callable[..., Awaitable[None]] | None" = None,
*,
message: str,
initial_state: "Callable[[], Mapping[str, object]] | None" = None,
transient_keys: tuple[str, ...] = (),
messages_key: str = "messages",
) -> TurnResult
run(
state,
tools=None,
registry=None,
reducers=None,
hooks=None,
node_timeout=None,
max_iterations=None,
checkpointer=None,
checkpoint_id=None,
owner=DEFAULT_OWNER,
resume=None,
tracer=None,
state_schema=None,
emit=None,
providers=None,
default_provider=None,
default_model=None,
on_llm_payload=None,
*,
message=None,
initial_state=None,
transient_keys=(),
messages_key="messages",
)
Execute the graph starting from the entry point.
A single entry point for both plain workflows and durable, interrupt-aware conversation turns:
- Plain run (no message): walk the graph from the entry
point and return the final state. When an
Interruptnode is reached the run pauses and raises :class:~teff.node.interrupt.GraphInterrupt; resume by callingrunagain with the same checkpoint_id and aresumedict mapping the interrupt's key to the operator's answer. - Conversation turn (with message): the run is driven as
one durable turn against a session — checkpoint_id is the
session id. If the session is paused on an interrupt, message
is the operator's answer and the run auto-resumes from the
checkpoint; otherwise message starts (or continues) the
conversation (seeded via initial_state, appended to the
messages_keylist). A pause is not raised: it is folded into the returned :class:TurnResult(waiting=Truewith the prompt and key), so the same loop works across any number of interrupts. This is the same primitive the :class:~teff.assistant.Assistantwrapper exposes asrun/stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict | State
|
Initial workflow state (plain |
required |
tools
|
'Sequence[Tool | McpToolGroup] | None'
|
Optional list of Tool instances available to nodes. |
None
|
providers
|
'dict[str, Provider] | ProviderRegistry | None'
|
Optional |
None
|
default_provider
|
str | None
|
Optional default provider name used by LLM
nodes that don't set |
None
|
registry
|
NodeRegistry | None
|
Node registry (defaults to |
None
|
reducers
|
dict[str, Reducer] | None
|
Per-key merge strategies
(see :func: |
None
|
hooks
|
dict[str, Callable] | None
|
Observability hooks (see class docstring). |
None
|
node_timeout
|
float | None
|
Max seconds per node. |
None
|
max_iterations
|
int | None
|
Max total node executions before raising
|
None
|
checkpointer
|
Checkpointer | None
|
Optional persistence backend. When set, a
checkpoint is written before each node execution, so a
crashed or interrupted run can be resumed by calling
|
None
|
checkpoint_id
|
str | None
|
Key identifying a run (e.g. |
None
|
owner
|
str
|
Scopes checkpoint_id to a user/session/tenant. The
same ID under different owners never collides, and
|
DEFAULT_OWNER
|
resume
|
dict | None
|
When a :class: |
None
|
tracer
|
RunTracer | None
|
Optional :class: |
None
|
emit
|
'Callable[[StreamEvent], Awaitable[None]] | None'
|
Optional async sink receiving
:class: |
None
|
state_schema
|
dict | None
|
Optional YAML |
None
|
message
|
str | None
|
Operator message for one durable conversation turn
(see above). |
None
|
initial_state
|
'Callable[[], Mapping[str, object]] | None'
|
Fresh-session seed for conversation turns (only used with message). |
None
|
transient_keys
|
tuple[str, ...]
|
Per-turn scratch keys cleared at the start of each conversation turn (only used with message). |
()
|
messages_key
|
str
|
Name of the messages list in the conversation state (only used with message). |
'messages'
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If max_iterations is exceeded. |
GraphInterrupt
|
When an |
Returns:
| Type | Description |
|---|---|
'TurnResult | dict | State'
|
Final state (same type as passed in) on a plain run, or a |
'TurnResult | dict | State'
|
class: |
Source code in teff/graph/graph.py
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 | |
stream
async
¶
stream(
state,
tools=None,
registry=None,
reducers=None,
hooks=None,
node_timeout=None,
max_iterations=None,
checkpointer=None,
checkpoint_id=None,
owner=DEFAULT_OWNER,
resume=None,
tracer=None,
state_schema=None,
providers=None,
default_provider=None,
default_model=None,
on_llm_payload=None,
*,
message=None,
initial_state=None,
transient_keys=(),
messages_key="messages",
)
Stream events as the graph executes.
Behaves like :meth:run but yields a :class:StreamEvent for
each observable step instead of returning a final state::
async for event in graph.stream(state):
if event.type == "token":
print(event.data["token"], end="", flush=True)
Event types: run_start, node_start, node_end,
node_error, edge, token, llm, structured,
interrupt, interrupt_resume, checkpoint, and a final
run_end.
Token events are only emitted when the running LLM node streams
(any node with no tool calls streams automatically in this mode).
A run paused at an Interrupt node ends with an interrupt
event followed by a run_end event with status: "interrupted";
call :meth:stream or :meth:run again
with a resume dict and the same checkpoint_id to continue.
A failed run yields a run_end event with status: "error".
With message the stream is one durable conversation turn (see
:meth:run): a paused session auto-resumes with the operator's
answer, and a re-work pause surfaces an interrupt event (with
key/prompt in its data) where the stream ends — call
this again with the operator's answer to continue.
Parameters mirror :meth:run (including owner).
Source code in teff/graph/graph.py
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 | |
to_mermaid
¶
to_mermaid(show_conditions=True)
Render this graph as a Mermaid flowchart diagram.
Produces a flowchart TD definition: every node becomes a box
labelled node_id[node.type] and every edge an arrow. The entry
point is filled blue, __error__ edges are dashed and red, and
conditional edges carry their condition as an edge label (when
show_conditions is true).
Returns:
| Type | Description |
|---|---|
str
|
The Mermaid diagram as a string (no code fence). |
Source code in teff/graph/graph.py
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
to_yaml
¶
to_yaml()
Serialize this graph to a YAML string.
Source code in teff/graph/graph.py
1065 1066 1067 1068 1069 | |
update_state
async
¶
update_state(checkpoint_id, values, *, checkpointer, owner=DEFAULT_OWNER, as_node=None)
Edit the durable state of an existing run and persist it.
Loads the latest checkpoint for checkpoint_id, overrides the
given keys with values, and saves it back — the HITL "fix the
data then resume" primitive. A run paused on an interrupt stays
paused: the next run(resume=...) continues from the interrupt
with the edited state.
as_node is accepted for LangGraph parity but is informational — DraftFlow resumes from the interrupt automatically, so update attribution is not needed.
Raises:
| Type | Description |
|---|---|
KeyError
|
If checkpoint_id has no checkpoint yet. |
Source code in teff/graph/graph.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 | |
TurnResult
dataclass
¶
Structured outcome of one interrupt-aware conversation turn.
Attributes:
| Name | Type | Description |
|---|---|---|
session_id |
str
|
The session the turn ran against. |
reply |
str
|
The latest assistant reply ( |
waiting |
bool
|
|
prompt |
str | None
|
The interrupt's question (only when |
key |
str | None
|
The interrupt's state key (only when |
state |
dict | None
|
Final state for a completed turn ( |
Source code in teff/graph/graph.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |