Compiled graph
Classes:
Name | Description |
---|---|
CompiledGraph |
A fully compiled and executable graph ready for workflow execution. |
Attributes:
Name | Type | Description |
---|---|---|
StateT |
|
|
logger |
|
Attributes¶
Classes¶
CompiledGraph
¶
A fully compiled and executable graph ready for workflow execution.
CompiledGraph represents the final executable form of a StateGraph after compilation. It encapsulates all the execution logic, handlers, and services needed to run agent workflows. The graph supports both synchronous and asynchronous execution with comprehensive state management, checkpointing, event publishing, and streaming capabilities.
This class is generic over state types to support custom AgentState subclasses, ensuring type safety throughout the execution process.
Key Features: - Synchronous and asynchronous execution methods - Real-time streaming with incremental results - State persistence and checkpointing - Interrupt and resume capabilities - Event publishing for monitoring and debugging - Background task management - Graceful error handling and recovery
Attributes:
Name | Type | Description |
---|---|---|
_state |
The initial/template state for graph executions. |
|
_invoke_handler |
InvokeHandler[StateT]
|
Handler for non-streaming graph execution. |
_stream_handler |
StreamHandler[StateT]
|
Handler for streaming graph execution. |
_checkpointer |
BaseCheckpointer[StateT] | None
|
Optional state persistence backend. |
_publisher |
BasePublisher | None
|
Optional event publishing backend. |
_store |
BaseStore | None
|
Optional data storage backend. |
_state_graph |
StateGraph[StateT]
|
Reference to the source StateGraph. |
_interrupt_before |
list[str]
|
Nodes where execution should pause before execution. |
_interrupt_after |
list[str]
|
Nodes where execution should pause after execution. |
_task_manager |
Manager for background async tasks. |
Example
# After building and compiling a StateGraph
compiled = graph.compile()
# Synchronous execution
result = compiled.invoke({"messages": [Message.text_message("Hello")]})
# Asynchronous execution with streaming
async for chunk in compiled.astream({"messages": [message]}):
print(f"Streamed: {chunk.content}")
# Graceful cleanup
await compiled.aclose()
Note
CompiledGraph instances should be properly closed using aclose() to release resources like database connections, background tasks, and event publishers.
Methods:
Name | Description |
---|---|
__init__ |
|
aclose |
Close the graph and release any resources. |
ainvoke |
Execute the graph asynchronously. |
astop |
Request the current graph execution to stop (async). |
astream |
Execute the graph asynchronously with streaming support. |
generate_graph |
Generate the graph representation. |
invoke |
Execute the graph synchronously and return the final results. |
stop |
Request the current graph execution to stop (sync helper). |
stream |
Execute the graph synchronously with streaming support. |
Source code in pyagenity/graph/compiled_graph.py
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 |
|
Functions¶
__init__
¶
__init__(state, checkpointer, publisher, store, state_graph, interrupt_before, interrupt_after, task_manager)
Source code in pyagenity/graph/compiled_graph.py
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 |
|
aclose
async
¶
aclose()
Close the graph and release any resources.
Source code in pyagenity/graph/compiled_graph.py
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 |
|
ainvoke
async
¶
ainvoke(input_data, config=None, response_granularity=ResponseGranularity.LOW)
Execute the graph asynchronously.
Auto-detects whether to start fresh execution or resume from interrupted state based on the AgentState's execution metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict[str, Any]
|
Input dict with 'messages' key (for new execution) or additional data for resuming |
required |
|
dict[str, Any] | None
|
Configuration dictionary |
None
|
|
ResponseGranularity
|
Response parsing granularity |
LOW
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Response dict based on granularity |
Source code in pyagenity/graph/compiled_graph.py
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 |
|
astop
async
¶
astop(config)
Request the current graph execution to stop (async).
Contract: - Requires a valid thread_id in config - If no active thread or no checkpointer, returns not-running - If state exists and is running, set stop_requested flag in thread info
Source code in pyagenity/graph/compiled_graph.py
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 |
|
astream
async
¶
astream(input_data, config=None, response_granularity=ResponseGranularity.LOW)
Execute the graph asynchronously with streaming support.
Yields Message objects containing incremental responses. If nodes return streaming responses, yields them directly. If nodes return complete responses, simulates streaming by chunking.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict[str, Any]
|
Input dict |
required |
|
dict[str, Any] | None
|
Configuration dictionary |
None
|
|
ResponseGranularity
|
Response parsing granularity |
LOW
|
Yields:
Type | Description |
---|---|
AsyncIterator[Message]
|
Message objects with incremental content |
Source code in pyagenity/graph/compiled_graph.py
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 |
|
generate_graph
¶
generate_graph()
Generate the graph representation.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary representing the graph structure. |
Source code in pyagenity/graph/compiled_graph.py
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 |
|
invoke
¶
invoke(input_data, config=None, response_granularity=ResponseGranularity.LOW)
Execute the graph synchronously and return the final results.
Runs the complete graph workflow from start to finish, handling state management, node execution, and result formatting. This method automatically detects whether to start a fresh execution or resume from an interrupted state.
The execution is synchronous but internally uses async operations, making it suitable for use in non-async contexts while still benefiting from async capabilities for I/O operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict[str, Any]
|
Input dictionary for graph execution. For new executions, should contain 'messages' key with list of initial messages. For resumed executions, can contain additional data to merge. |
required |
|
dict[str, Any] | None
|
Optional configuration dictionary containing execution settings: - user_id: Identifier for the user/session - thread_id: Unique identifier for this execution thread - run_id: Unique identifier for this specific run - recursion_limit: Maximum steps before stopping (default: 25) |
None
|
|
ResponseGranularity
|
Level of detail in the response: - LOW: Returns only messages (default) - PARTIAL: Returns context, summary, and messages - FULL: Returns complete state and messages |
LOW
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary containing execution results formatted according to the |
dict[str, Any]
|
specified granularity level. Always includes execution messages |
dict[str, Any]
|
and may include additional state information. |
Raises:
Type | Description |
---|---|
ValueError
|
If input_data is invalid for new execution. |
GraphRecursionError
|
If execution exceeds recursion limit. |
Various exceptions
|
Depending on node execution failures. |
Example
# Basic execution
result = compiled.invoke({"messages": [Message.text_message("Process this data")]})
print(result["messages"]) # Final execution messages
# With configuration and full details
result = compiled.invoke(
input_data={"messages": [message]},
config={"user_id": "user123", "thread_id": "session456", "recursion_limit": 50},
response_granularity=ResponseGranularity.FULL,
)
print(result["state"]) # Complete final state
Note
This method uses asyncio.run() internally, so it should not be called from within an async context. Use ainvoke() instead for async execution.
Source code in pyagenity/graph/compiled_graph.py
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 |
|
stop
¶
stop(config)
Request the current graph execution to stop (sync helper).
This sets a stop flag in the checkpointer's thread store keyed by thread_id. Handlers periodically check this flag and interrupt execution. Returns a small status dict.
Source code in pyagenity/graph/compiled_graph.py
251 252 253 254 255 256 257 258 |
|
stream
¶
stream(input_data, config=None, response_granularity=ResponseGranularity.LOW)
Execute the graph synchronously with streaming support.
Yields Message objects containing incremental responses. If nodes return streaming responses, yields them directly. If nodes return complete responses, simulates streaming by chunking.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict[str, Any]
|
Input dict |
required |
|
dict[str, Any] | None
|
Configuration dictionary |
None
|
|
ResponseGranularity
|
Response parsing granularity |
LOW
|
Yields:
Type | Description |
---|---|
Generator[Message]
|
Message objects with incremental content |
Source code in pyagenity/graph/compiled_graph.py
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 |
|