pub struct StateGraph {
pub name: String,
pub nodes: BTreeMap<String, GraphNode>,
pub edges: BTreeMap<String, Vec<GraphEdge>>,
pub entry_point: String,
pub terminal_nodes: Vec<String>,
}Expand description
A directed graph that supports cyclic edges for agent workflows.
Unlike DAGs, StateGraphs can contain cycles, enabling ReAct loops and other iterative patterns to be modeled as explicit graph structures. This makes the state machine visible, inspectable, and interruptable.
Example: ReAct as a StateGraph (diagram):
think_node (LLMInfer) → [HasToolCalls] → tool_node (ToolCall) → think_node (cycle!) think_node (LLMInfer) → [IsFinalAnswer] → end_node
Fields§
§name: StringThe graph name/identifier.
nodes: BTreeMap<String, GraphNode>All nodes in the graph, keyed by node ID.
BTreeMap (not HashMap) makes iteration order deterministic across
runs — important for reproducible rendering, snapshot tests, and
stable edge-fan-out ordering.
edges: BTreeMap<String, Vec<GraphEdge>>All edges in the graph, keyed by source node ID.
Each source node can have multiple outgoing edges (with conditions).
BTreeMap for deterministic source-node iteration order.
entry_point: StringThe entry point node ID (where execution starts).
terminal_nodes: Vec<String>Terminal node IDs (where execution ends).
Implementations§
Source§impl StateGraph
impl StateGraph
Sourcepub fn new(name: impl Into<String>, entry_point: impl Into<String>) -> Self
pub fn new(name: impl Into<String>, entry_point: impl Into<String>) -> Self
Create a new StateGraph with a name and entry point.
Sourcepub fn add_terminal(&mut self, node_id: impl Into<String>)
pub fn add_terminal(&mut self, node_id: impl Into<String>)
Add a terminal node (execution ends here).
Sourcepub fn get_edges_from(&self, node_id: &str) -> Vec<&GraphEdge>
pub fn get_edges_from(&self, node_id: &str) -> Vec<&GraphEdge>
Get outgoing edges from a node.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Get the number of nodes.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Get the number of edges.
Sourcepub fn has_cycles(&self) -> bool
pub fn has_cycles(&self) -> bool
Check if the graph contains any cycles.
Unlike DAGs, cycles are ALLOWED in StateGraphs (they’re the mechanism for ReAct loops). This method is for diagnostics, not validation.