Skip to main content
The 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 loops
  • initial_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:
Nodes can be:
  • 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:
For linear workflows, you simply connect nodes in sequence:

Working with Routers

Routers are special nodes that determine the next node to execute based on their output:
The router must return a dictionary containing a "choice" key with the name of the next node to execute:

Graph Execution

To execute a graph:

Execution Flow

  1. The graph starts execution at the designated start node
  2. Each node processes the input and returns an output
  3. The output is merged with the current state and passed to the next node
  4. If a router node is encountered, its "choice" output determines the next node
  5. 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:
Each node receives the current state as an optional parameter:

Graph Validation

Before execution, you can validate that your graph is properly configured:
The compile method checks for:
  • Presence of a start node
  • Presence of at least one end node
  • Graph validity (e.g., no disconnected nodes)

Visualization

GraphAI provides a method to visualize your graph (requires matplotlib and networkx):
This generates a visual representation of your graph, making it easier to understand complex workflows.

Next Steps

  • Learn about Nodes to understand how to build processing units
  • Explore Parallel Execution for concurrent branch processing
  • Explore State management for maintaining context
  • Check out Callbacks for implementing streaming