pydantic_graph
Graph
dataclass
Bases: Generic[StateT, DepsT, RunEndT]
Definition of a graph.
In pydantic-graph
, a graph is a collection of nodes that can be run in sequence. The nodes define
their outgoing edges — e.g. which nodes may be run next, and thereby the structure of the graph.
Here's a very simple example of a graph which increments a number by 1, but makes sure the number is never 42 at the end.
from __future__ import annotations
from dataclasses import dataclass
from pydantic_graph import BaseNode, End, Graph, GraphRunContext
@dataclass
class MyState:
number: int
@dataclass
class Increment(BaseNode[MyState]):
async def run(self, ctx: GraphRunContext) -> Check42:
ctx.state.number += 1
return Check42()
@dataclass
class Check42(BaseNode[MyState, None, int]):
async def run(self, ctx: GraphRunContext) -> Increment | End[int]:
if ctx.state.number == 42:
return Increment()
else:
return End(ctx.state.number)
never_42_graph = Graph(nodes=(Increment, Check42))
See run
For an example of running graph, and
mermaid_code
for an example of generating a mermaid diagram
from the graph.
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
__init__
__init__(
*,
nodes: Sequence[type[BaseNode[StateT, DepsT, RunEndT]]],
name: str | None = None,
state_type: type[StateT] | Unset = UNSET,
run_end_type: type[RunEndT] | Unset = UNSET,
auto_instrument: bool = True
)
Create a graph from a sequence of nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes
|
Sequence[type[BaseNode[StateT, DepsT, RunEndT]]]
|
The nodes which make up the graph, nodes need to be unique and all be generic in the same state type. |
required |
name
|
str | None
|
Optional name for the graph, if not provided the name will be inferred from the calling frame on the first call to a graph method. |
None
|
state_type
|
type[StateT] | Unset
|
The type of the state for the graph, this can generally be inferred from |
UNSET
|
run_end_type
|
type[RunEndT] | Unset
|
The type of the result of running the graph, this can generally be inferred from |
UNSET
|
auto_instrument
|
bool
|
Whether to create a span for the graph run and the execution of each node's run method. |
True
|
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
run
async
run(
start_node: BaseNode[StateT, DepsT, RunEndT],
*,
state: StateT = None,
deps: DepsT = None,
persistence: (
BaseStatePersistence[StateT, RunEndT] | None
) = None,
infer_name: bool = True,
span: LogfireSpan | None = None
) -> GraphRunResult[StateT, RunEndT]
Run the graph from a starting node until it ends.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_node
|
BaseNode[StateT, DepsT, RunEndT]
|
the first node to run, since the graph definition doesn't define the entry point in the graph, you need to provide the starting node. |
required |
state
|
StateT
|
The initial state of the graph. |
None
|
deps
|
DepsT
|
The dependencies of the graph. |
None
|
persistence
|
BaseStatePersistence[StateT, RunEndT] | None
|
State persistence interface, defaults to
|
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
span
|
LogfireSpan | None
|
The span to use for the graph run. If not provided, a span will be created depending on the value of
the |
None
|
Returns:
Type | Description |
---|---|
GraphRunResult[StateT, RunEndT]
|
A |
Here's an example of running the graph from above:
from never_42 import Increment, MyState, never_42_graph
async def main():
state = MyState(1)
await never_42_graph.run(Increment(), state=state)
print(state)
#> MyState(number=2)
state = MyState(41)
await never_42_graph.run(Increment(), state=state)
print(state)
#> MyState(number=43)
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
run_sync
run_sync(
start_node: BaseNode[StateT, DepsT, RunEndT],
*,
state: StateT = None,
deps: DepsT = None,
persistence: (
BaseStatePersistence[StateT, RunEndT] | None
) = None,
infer_name: bool = True
) -> GraphRunResult[StateT, RunEndT]
Synchronously run the graph.
This is a convenience method that wraps self.run
with loop.run_until_complete(...)
.
You therefore can't use this method inside async code or if there's an active event loop.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_node
|
BaseNode[StateT, DepsT, RunEndT]
|
the first node to run, since the graph definition doesn't define the entry point in the graph, you need to provide the starting node. |
required |
state
|
StateT
|
The initial state of the graph. |
None
|
deps
|
DepsT
|
The dependencies of the graph. |
None
|
persistence
|
BaseStatePersistence[StateT, RunEndT] | None
|
State persistence interface, defaults to
|
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
Returns:
Type | Description |
---|---|
GraphRunResult[StateT, RunEndT]
|
The result type from ending the run and the history of the run. |
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
iter
async
iter(
start_node: BaseNode[StateT, DepsT, RunEndT],
*,
state: StateT = None,
deps: DepsT = None,
persistence: (
BaseStatePersistence[StateT, RunEndT] | None
) = None,
span: AbstractContextManager[Any] | None = None,
infer_name: bool = True
) -> AsyncIterator[GraphRun[StateT, DepsT, RunEndT]]
A contextmanager which can be used to iterate over the graph's nodes as they are executed.
This method returns a GraphRun
object which can be used to async-iterate over the nodes of this Graph
as
they are executed. This is the API to use if you want to record or interact with the nodes as the graph
execution unfolds.
The GraphRun
can also be used to manually drive the graph execution by calling
GraphRun.next
.
The GraphRun
provides access to the full run history, state, deps, and the final result of the run once
it has completed.
For more details, see the API documentation of GraphRun
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_node
|
BaseNode[StateT, DepsT, RunEndT]
|
the first node to run. Since the graph definition doesn't define the entry point in the graph, you need to provide the starting node. |
required |
state
|
StateT
|
The initial state of the graph. |
None
|
deps
|
DepsT
|
The dependencies of the graph. |
None
|
persistence
|
BaseStatePersistence[StateT, RunEndT] | None
|
State persistence interface, defaults to
|
None
|
span
|
AbstractContextManager[Any] | None
|
The span to use for the graph run. If not provided, a new span will be created. |
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
Returns: A GraphRun that can be async iterated over to drive the graph to completion.
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
iter_from_persistence
async
iter_from_persistence(
persistence: BaseStatePersistence[StateT, RunEndT],
*,
deps: DepsT = None,
span: AbstractContextManager[Any] | None = None,
infer_name: bool = True
) -> AsyncIterator[GraphRun[StateT, DepsT, RunEndT]]
A contextmanager to iterate over the graph's nodes as they are executed, created from a persistence object.
This method has similar functionality to iter
,
but instead of passing the node to run, it will restore the node and state from state persistence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
persistence
|
BaseStatePersistence[StateT, RunEndT]
|
The state persistence interface to use. |
required |
deps
|
DepsT
|
The dependencies of the graph. |
None
|
span
|
AbstractContextManager[Any] | None
|
The span to use for the graph run. If not provided, a new span will be created. |
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
Returns: A GraphRun that can be async iterated over to drive the graph to completion.
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
initialize
async
initialize(
node: BaseNode[StateT, DepsT, RunEndT],
persistence: BaseStatePersistence[StateT, RunEndT],
*,
state: StateT = None,
infer_name: bool = True
) -> None
Initialize a new graph run in persistence without running it.
This is useful if you want to set up a graph run to be run later, e.g. via
iter_from_persistence
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
BaseNode[StateT, DepsT, RunEndT]
|
The node to run first. |
required |
persistence
|
BaseStatePersistence[StateT, RunEndT]
|
State persistence interface. |
required |
state
|
StateT
|
The start state of the graph. |
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
Source code in pydantic_graph/pydantic_graph/graph.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
next
async
next(
node: BaseNode[StateT, DepsT, RunEndT],
persistence: BaseStatePersistence[StateT, RunEndT],
*,
state: StateT = None,
deps: DepsT = None,
infer_name: bool = True
) -> BaseNode[StateT, DepsT, Any] | End[RunEndT]
Run a node in the graph and return the next node to run.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
BaseNode[StateT, DepsT, RunEndT]
|
The node to run. |
required |
persistence
|
BaseStatePersistence[StateT, RunEndT]
|
State persistence interface, defaults to
|
required |
state
|
StateT
|
The current state of the graph. |
None
|
deps
|
DepsT
|
The dependencies of the graph. |
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
Returns:
Type | Description |
---|---|
BaseNode[StateT, DepsT, Any] | End[RunEndT]
|
The next node to run or |
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
mermaid_code
mermaid_code(
*,
start_node: (
Sequence[NodeIdent] | NodeIdent | None
) = None,
title: str | None | Literal[False] = None,
edge_labels: bool = True,
notes: bool = True,
highlighted_nodes: (
Sequence[NodeIdent] | NodeIdent | None
) = None,
highlight_css: str = DEFAULT_HIGHLIGHT_CSS,
infer_name: bool = True,
direction: StateDiagramDirection | None = None
) -> str
Generate a diagram representing the graph as mermaid diagram.
This method calls pydantic_graph.mermaid.generate_code
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_node
|
Sequence[NodeIdent] | NodeIdent | None
|
The node or nodes which can start the graph. |
None
|
title
|
str | None | Literal[False]
|
The title of the diagram, use |
None
|
edge_labels
|
bool
|
Whether to include edge labels. |
True
|
notes
|
bool
|
Whether to include notes on each node. |
True
|
highlighted_nodes
|
Sequence[NodeIdent] | NodeIdent | None
|
Optional node or nodes to highlight. |
None
|
highlight_css
|
str
|
The CSS to use for highlighting nodes. |
DEFAULT_HIGHLIGHT_CSS
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
direction
|
StateDiagramDirection | None
|
The direction of flow. |
None
|
Returns:
Type | Description |
---|---|
str
|
The mermaid code for the graph, which can then be rendered as a diagram. |
Here's an example of generating a diagram for the graph from above:
from never_42 import Increment, never_42_graph
print(never_42_graph.mermaid_code(start_node=Increment))
'''
---
title: never_42_graph
---
stateDiagram-v2
[*] --> Increment
Increment --> Check42
Check42 --> Increment
Check42 --> [*]
'''
The rendered diagram will look like this:
---
title: never_42_graph
---
stateDiagram-v2
[*] --> Increment
Increment --> Check42
Check42 --> Increment
Check42 --> [*]
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
mermaid_image
mermaid_image(
infer_name: bool = True, **kwargs: Unpack[MermaidConfig]
) -> bytes
Generate a diagram representing the graph as an image.
The format and diagram can be customized using kwargs
,
see pydantic_graph.mermaid.MermaidConfig
.
Uses external service
This method makes a request to mermaid.ink to render the image, mermaid.ink
is a free service not affiliated with Pydantic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
**kwargs
|
Unpack[MermaidConfig]
|
Additional arguments to pass to |
{}
|
Returns:
Type | Description |
---|---|
bytes
|
The image bytes. |
Source code in pydantic_graph/pydantic_graph/graph.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
|
mermaid_save
mermaid_save(
path: Path | str,
/,
*,
infer_name: bool = True,
**kwargs: Unpack[MermaidConfig],
) -> None
Generate a diagram representing the graph and save it as an image.
The format and diagram can be customized using kwargs
,
see pydantic_graph.mermaid.MermaidConfig
.
Uses external service
This method makes a request to mermaid.ink to render the image, mermaid.ink
is a free service not affiliated with Pydantic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path | str
|
The path to save the image to. |
required |
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
**kwargs
|
Unpack[MermaidConfig]
|
Additional arguments to pass to |
{}
|
Source code in pydantic_graph/pydantic_graph/graph.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
|
get_nodes
Get the nodes in the graph.
Source code in pydantic_graph/pydantic_graph/graph.py
499 500 501 |
|
GraphRun
Bases: Generic[StateT, DepsT, RunEndT]
A stateful, async-iterable run of a Graph
.
You typically get a GraphRun
instance from calling
async with [my_graph.iter(...)][pydantic_graph.graph.Graph.iter] as graph_run:
. That gives you the ability to iterate
through nodes as they run, either by async for
iteration or by repeatedly calling .next(...)
.
Here's an example of iterating over the graph from above:
from copy import deepcopy
from never_42 import Increment, MyState, never_42_graph
async def main():
state = MyState(1)
async with never_42_graph.iter(Increment(), state=state) as graph_run:
node_states = [(graph_run.next_node, deepcopy(graph_run.state))]
async for node in graph_run:
node_states.append((node, deepcopy(graph_run.state)))
print(node_states)
'''
[
(Increment(), MyState(number=1)),
(Check42(), MyState(number=2)),
(End(data=2), MyState(number=2)),
]
'''
state = MyState(41)
async with never_42_graph.iter(Increment(), state=state) as graph_run:
node_states = [(graph_run.next_node, deepcopy(graph_run.state))]
async for node in graph_run:
node_states.append((node, deepcopy(graph_run.state)))
print(node_states)
'''
[
(Increment(), MyState(number=41)),
(Check42(), MyState(number=42)),
(Increment(), MyState(number=42)),
(Check42(), MyState(number=43)),
(End(data=43), MyState(number=43)),
]
'''
See the GraphRun.next
documentation for an example of how to manually
drive the graph run.
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
__init__
__init__(
*,
graph: Graph[StateT, DepsT, RunEndT],
start_node: BaseNode[StateT, DepsT, RunEndT],
persistence: BaseStatePersistence[StateT, RunEndT],
state: StateT,
deps: DepsT,
snapshot_id: str | None = None
)
Create a new run for a given graph, starting at the specified node.
Typically, you'll use Graph.iter
rather than calling this directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph[StateT, DepsT, RunEndT]
|
The |
required |
start_node
|
BaseNode[StateT, DepsT, RunEndT]
|
The node where execution will begin. |
required |
persistence
|
BaseStatePersistence[StateT, RunEndT]
|
State persistence interface. |
required |
state
|
StateT
|
A shared state object or primitive (like a counter, dataclass, etc.) that is available
to all nodes via |
required |
deps
|
DepsT
|
Optional dependencies that each node can access via |
required |
snapshot_id
|
str | None
|
The ID of the snapshot the node came from. |
None
|
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
next_node
property
The next node that will be run in the graph.
This is the next node that will be used during async iteration, or if a node is not passed to self.next(...)
.
result
property
result: GraphRunResult[StateT, RunEndT] | None
The final result of the graph run if the run is completed, otherwise None
.
next
async
next(
node: BaseNode[StateT, DepsT, RunEndT] | None = None,
) -> BaseNode[StateT, DepsT, RunEndT] | End[RunEndT]
Manually drive the graph run by passing in the node you want to run next.
This lets you inspect or mutate the node before continuing execution, or skip certain nodes
under dynamic conditions. The graph run should stop when you return an End
node.
Here's an example of using next
to drive the graph from above:
from copy import deepcopy
from pydantic_graph import End
from never_42 import Increment, MyState, never_42_graph
async def main():
state = MyState(48)
async with never_42_graph.iter(Increment(), state=state) as graph_run:
next_node = graph_run.next_node # start with the first node
node_states = [(next_node, deepcopy(graph_run.state))]
while not isinstance(next_node, End):
if graph_run.state.number == 50:
graph_run.state.number = 42
next_node = await graph_run.next(next_node)
node_states.append((next_node, deepcopy(graph_run.state)))
print(node_states)
'''
[
(Increment(), MyState(number=48)),
(Check42(), MyState(number=49)),
(End(data=49), MyState(number=49)),
]
'''
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
BaseNode[StateT, DepsT, RunEndT] | None
|
The node to run next in the graph. If not specified, uses |
None
|
Returns:
Type | Description |
---|---|
BaseNode[StateT, DepsT, RunEndT] | End[RunEndT]
|
The next node returned by the graph logic, or an |
BaseNode[StateT, DepsT, RunEndT] | End[RunEndT]
|
the run has completed. |
Source code in pydantic_graph/pydantic_graph/graph.py
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 |
|
__anext__
async
Use the last returned node as the input to Graph.next
.
Source code in pydantic_graph/pydantic_graph/graph.py
778 779 780 781 782 |
|
GraphRunResult
dataclass
Bases: Generic[StateT, RunEndT]
The final result of running a graph.
Source code in pydantic_graph/pydantic_graph/graph.py
788 789 790 791 792 793 794 |
|