Graph
is the central orchestration component in GraphAI. It connects Nodes
and Routers
into a coherent workflow and manages the execution flow.
Graph Basics
A graph consists of:- Nodes: Processing units that perform specific tasks
- Edges: Connections between nodes that define the flow of data
- State: Shared context that persists throughout the execution
Creating a Graph
Parameters
max_steps
(int, default=10): Maximum number of steps to prevent infinite loopsinitial_state
(Dict[str, Any], optional): Initial state for the graph execution
Adding Nodes
Nodes are the building blocks of your graph. Each node represents a discrete processing step:- Start nodes: Entry points to the graph (only one allowed)
- End nodes: Exit points from the graph (multiple allowed)
- Regular nodes: Intermediate processing steps
- Router nodes: Decision points that determine execution flow
Connecting Nodes with Edges
Edges define how data flows between nodes:Working with Routers
Routers are special nodes that determine the next node to execute based on their output:"choice"
key with the name of the next node to execute:
Graph Execution
To execute a graph:Execution Flow
- The graph starts execution at the designated start node
- Each node processes the input and returns an output
- The output is merged with the current state and passed to the next node
- If a router node is encountered, its
"choice"
output determines the next node - Execution continues until an end node is reached or max_steps is exceeded
State Management
The graph maintains a state dictionary that persists throughout execution:Graph Validation
Before execution, you can validate that your graph is properly configured:- Presence of a start node
- Presence of at least one end node
- Graph validity (e.g., no disconnected nodes)