Graph Engine
The core workflow engine for Maticlib.
MaticGraph
maticlib.graph.graph.MaticGraph
A fast, pure-Python graph workflow engine with optional state management. Supports dict, TypedDict, dataclass, and Pydantic BaseModel states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stateful
|
bool
|
If True, maintains and merges state across nodes. |
True
|
state_schema
|
Optional[type]
|
Optional Pydantic BaseModel, dataclass, or TypedDict class |
None
|
max_workers
|
int
|
Maximum number of parallel workers (default: 4) |
4
|
Initializes the MaticGraph engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stateful
|
bool
|
If True, state is preserved and merged between nodes. |
True
|
state_schema
|
type
|
A Pydantic model, dataclass, or TypedDict class to use as the state container. |
None
|
max_workers
|
int
|
Maximum number of parallel threads for |
4
|
Source code in maticlib/graph/graph.py
add_conditional_edge
Adds a conditional edge that routes execution based on a condition function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_node
|
str
|
The node from which to route. |
required |
condition
|
Callable
|
A function that takes the current state and returns a string key matching one of the routes. |
required |
routes
|
Dict[str, str]
|
A mapping of condition keys to target node names. |
required |
readable_names
|
dict
|
Mapping of keys to human-friendly names for documentation/visualization. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
MaticGraph |
MaticGraph
|
The graph instance (for chaining). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If from_node or route targets are not found. |
Source code in maticlib/graph/graph.py
add_edge
Adds a directed edge between two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_node
|
str
|
The name of the starting node. |
required |
to_node
|
str
|
The name of the target node, or 'END' to exit. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MaticGraph |
MaticGraph
|
The graph instance (for chaining). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either node name is not found in the graph. |
Source code in maticlib/graph/graph.py
add_node
Adds a processing node to the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name for the node. |
required |
function
|
Callable
|
Function to execute. Should accept the current state and return a dict or model update. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MaticGraph |
MaticGraph
|
The graph instance (for chaining). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a node with the same name already exists. |
Source code in maticlib/graph/graph.py
get_execution_log
parallel_group
Execute multiple nodes in parallel after a specific node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_node
|
str
|
Node that triggers parallel execution |
required |
parallel_nodes
|
List[str]
|
List of nodes to execute in parallel |
required |
join_node
|
Optional[str]
|
Optional node to execute after all parallel nodes complete |
None
|
condition
|
Optional[Callable[[Any], bool]]
|
Optional function to decide whether to parallelize If returns False, executes first parallel_node only |
None
|
Example
Always parallel
graph.parallel_group( "analyze", ["sentiment", "entities", "summary"], join_node="combine_results" )
Conditional parallel
graph.parallel_group( "check_size", ["process_large_a", "process_large_b"], join_node="merge", condition=lambda state: state.get("size") > 1000 )
Source code in maticlib/graph/graph.py
run
Executes the graph workflow dynamically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_state
|
dict | BaseModel
|
Starting data for the workflow. |
None
|
max_iterations
|
int
|
Safety limit on total node executions to prevent infinite loops. Default is 1000. |
1000
|
verbose
|
bool
|
If True, prints execution trace to stdout. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Union[Dict[str, Any], BaseModel]
|
The final accumulated state of the workflow. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no entry node is set, or if an execution error occurs in a node. |
Source code in maticlib/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 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 | |
set_entry
Sets the starting node for the graph execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_name
|
str
|
The name of the entry node. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MaticGraph |
MaticGraph
|
The graph instance (for chaining). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the node_name does not exist in the graph. |
Source code in maticlib/graph/graph.py
set_exit
Marks a node as an explicit exit point for the workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_name
|
str
|
The name of the node. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MaticGraph |
MaticGraph
|
The graph instance (for chaining). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the node_name does not exist in the graph. |
Source code in maticlib/graph/graph.py
visualize
Generate a text-based visualization of the graph.
Source code in maticlib/graph/graph.py
when
Simplified conditional routing using state['next'] or state.next Works with both dict and Pydantic models.
Source code in maticlib/graph/graph.py
Node
maticlib.graph.node.Node
dataclass
Node(
name,
function,
next_nodes=list(),
condition_func=None,
condition_map=None,
readable_names=None,
parallel_group=None,
parallel_join=None,
parallel_condition=None,
)
Represents a processing node within a MaticGraph workflow.
A node encapsulates a function to be executed and metadata for routing the workflow after execution, including support for conditional branching and parallel execution.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Unique identifier for the node. |
function |
Callable
|
The Python function to execute. Receives the current state and returns an update (dict or model). |
next_nodes |
List[str]
|
List of possible next nodes for sequential or parallel flow. |
condition_func |
Optional[Callable]
|
A function that determines which route to take next based on the returned key. |
condition_map |
Optional[Dict[str, str]]
|
Maps keys from |
readable_names |
Optional[Dict[str, str]]
|
Human-readable names for routes (useful for visualization). |
parallel_group |
Optional[List[str]]
|
List of nodes to execute in parallel after this node. |
parallel_join |
Optional[str]
|
A node where parallel execution groups re-converge. |
parallel_condition |
Optional[Callable]
|
A condition to decide whether to trigger parallel execution. |