pydantic_graph.beta.graph
Core graph execution engine for the next version of the pydantic-graph library.
This module provides the main Graph class and GraphRun execution engine that
handles the orchestration of nodes, edges, and parallel execution paths in
the graph-based workflow system.
StateT
module-attribute
StateT = TypeVar('StateT', infer_variance=True)
Type variable for graph state.
DepsT
module-attribute
DepsT = TypeVar('DepsT', infer_variance=True)
Type variable for graph dependencies.
InputT
module-attribute
InputT = TypeVar('InputT', infer_variance=True)
Type variable for graph inputs.
OutputT
module-attribute
OutputT = TypeVar('OutputT', infer_variance=True)
Type variable for graph outputs.
EndMarker
dataclass
A marker indicating the end of graph execution with a final value.
EndMarker is used internally to signal that the graph has completed execution and carries the final output value.
Type Parameters
OutputT: The type of the final output value
Source code in pydantic_graph/pydantic_graph/beta/graph.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
JoinItem
dataclass
An item representing data flowing into a join operation.
JoinItem carries input data from a parallel execution path to a join node, along with metadata about which execution 'fork' it originated from.
Source code in pydantic_graph/pydantic_graph/beta/graph.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
join_id
instance-attribute
join_id: JoinID
The ID of the join node this item is targeting.
fork_stack
instance-attribute
fork_stack: ForkStack
The stack of ForkStackItems that led to producing this join item.
Graph
dataclass
Bases: Generic[StateT, DepsT, InputT, OutputT]
A complete graph definition ready for execution.
The Graph class represents a complete workflow graph with typed inputs, outputs, state, and dependencies. It contains all nodes, edges, and metadata needed for execution.
Type Parameters
StateT: The type of the graph state DepsT: The type of the dependencies InputT: The type of the input data OutputT: The type of the output data
Source code in pydantic_graph/pydantic_graph/beta/graph.py
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 | |
name
instance-attribute
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.
auto_instrument
instance-attribute
auto_instrument: bool
Whether to automatically create instrumentation spans.
edges_by_source
instance-attribute
Outgoing paths from each source node.
parent_forks
instance-attribute
parent_forks: dict[JoinID, ParentFork[NodeID]]
Parent fork information for each join node.
get_parent_fork
get_parent_fork(join_id: JoinID) -> ParentFork[NodeID]
Get the parent fork information for a join node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join_id
|
JoinID
|
The ID of the join node |
required |
Returns:
| Type | Description |
|---|---|
ParentFork[NodeID]
|
The parent fork information for the join |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the join ID is not found or has no parent fork |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
run
async
run(
*,
state: StateT = None,
deps: DepsT = None,
inputs: InputT = None,
span: (
AbstractContextManager[AbstractSpan] | None
) = None,
infer_name: bool = True
) -> OutputT
Execute the graph and return the final output.
This is the main entry point for graph execution. It runs the graph to completion and returns the final output value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
StateT
|
The graph state instance |
None
|
deps
|
DepsT
|
The dependencies instance |
None
|
inputs
|
InputT
|
The input data for the graph |
None
|
span
|
AbstractContextManager[AbstractSpan] | None
|
Optional span for tracing/instrumentation |
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
Returns:
| Type | Description |
|---|---|
OutputT
|
The final output from the graph execution |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
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 | |
iter
async
iter(
*,
state: StateT = None,
deps: DepsT = None,
inputs: InputT = None,
span: (
AbstractContextManager[AbstractSpan] | None
) = None,
infer_name: bool = True
) -> AsyncIterator[GraphRun[StateT, DepsT, OutputT]]
Create an iterator for step-by-step graph execution.
This method allows for more fine-grained control over graph execution, enabling inspection of intermediate states and results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
StateT
|
The graph state instance |
None
|
deps
|
DepsT
|
The dependencies instance |
None
|
inputs
|
InputT
|
The input data for the graph |
None
|
span
|
AbstractContextManager[AbstractSpan] | None
|
Optional span for tracing/instrumentation |
None
|
infer_name
|
bool
|
Whether to infer the graph name from the calling frame. |
True
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[GraphRun[StateT, DepsT, OutputT]]
|
A GraphRun instance that can be iterated for step-by-step execution |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
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 | |
render
Render the graph as a Mermaid diagram string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str | None
|
Optional title for the diagram |
None
|
direction
|
StateDiagramDirection | None
|
Optional direction for the diagram layout |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string containing the Mermaid diagram representation |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
251 252 253 254 255 256 257 258 259 260 261 262 263 | |
__str__
__str__() -> str
Return a Mermaid diagram representation of the graph.
Returns:
| Type | Description |
|---|---|
str
|
A string containing the Mermaid diagram of the graph |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
270 271 272 273 274 275 276 | |
GraphTask
dataclass
A single task representing the execution of a node in the graph.
GraphTask encapsulates all the information needed to execute a specific node, including its inputs and the fork context it's executing within.
Source code in pydantic_graph/pydantic_graph/beta/graph.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
node_id
instance-attribute
node_id: NodeID
The ID of the node to execute.
fork_stack
class-attribute
instance-attribute
fork_stack: ForkStack = field(repr=False)
Stack of forks that have been entered.
Used by the GraphRun to decide when to proceed through joins.
GraphRun
Bases: Generic[StateT, DepsT, OutputT]
A single execution instance of a graph.
GraphRun manages the execution state for a single run of a graph, including task scheduling, fork/join coordination, and result tracking.
Type Parameters
StateT: The type of the graph state DepsT: The type of the dependencies OutputT: The type of the output data
Source code in pydantic_graph/pydantic_graph/beta/graph.py
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 | |
__init__
__init__(
graph: Graph[StateT, DepsT, InputT, OutputT],
*,
state: StateT,
deps: DepsT,
inputs: InputT,
traceparent: str | None
)
Initialize a graph run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph[StateT, DepsT, InputT, OutputT]
|
The graph to execute |
required |
state
|
StateT
|
The graph state instance |
required |
deps
|
DepsT
|
The dependencies instance |
required |
inputs
|
InputT
|
The input data for the graph |
required |
traceparent
|
str | None
|
Optional trace parent for instrumentation |
required |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
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 | |
graph
instance-attribute
graph = graph
The graph being executed.
state
instance-attribute
state = state
The graph state instance.
deps
instance-attribute
deps = deps
The dependencies instance.
inputs
instance-attribute
inputs = inputs
The initial input data.
__aiter__
__aiter__() -> (
AsyncIterator[EndMarker[OutputT] | Sequence[GraphTask]]
)
Return self as an async iterator.
Returns:
| Type | Description |
|---|---|
AsyncIterator[EndMarker[OutputT] | Sequence[GraphTask]]
|
Self for async iteration |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
388 389 390 391 392 393 394 | |
__anext__
async
Get the next item in the async iteration.
Returns:
| Type | Description |
|---|---|
EndMarker[OutputT] | Sequence[GraphTask]
|
The next execution result from the graph |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
396 397 398 399 400 401 402 403 404 405 406 | |
next
async
next(
value: (
EndMarker[OutputT] | Sequence[GraphTask] | None
) = None,
) -> EndMarker[OutputT] | Sequence[GraphTask]
Advance the graph execution by one step.
This method allows for sending a value to the iterator, which is useful for resuming iteration or overriding intermediate results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
EndMarker[OutputT] | Sequence[GraphTask] | None
|
Optional value to send to the iterator |
None
|
Returns:
| Type | Description |
|---|---|
EndMarker[OutputT] | Sequence[GraphTask]
|
The next execution result: either an EndMarker, or sequence of GraphTasks |
Source code in pydantic_graph/pydantic_graph/beta/graph.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |