Skip to main content

StateGraph

Struct StateGraph 

Source
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: String

The 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: String

The entry point node ID (where execution starts).

§terminal_nodes: Vec<String>

Terminal node IDs (where execution ends).

Implementations§

Source§

impl StateGraph

Source

pub fn new(name: impl Into<String>, entry_point: impl Into<String>) -> Self

Create a new StateGraph with a name and entry point.

Source

pub fn add_node(&mut self, node: GraphNode)

Add a node to the graph.

Source

pub fn add_edge(&mut self, edge: GraphEdge)

Add an edge to the graph.

Source

pub fn add_terminal(&mut self, node_id: impl Into<String>)

Add a terminal node (execution ends here).

Source

pub fn get_node(&self, id: &str) -> Option<&GraphNode>

Get a node by ID.

Source

pub fn get_edges_from(&self, node_id: &str) -> Vec<&GraphEdge>

Get outgoing edges from a node.

Source

pub fn node_count(&self) -> usize

Get the number of nodes.

Source

pub fn edge_count(&self) -> usize

Get the number of edges.

Source

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.

Trait Implementations§

Source§

impl Clone for StateGraph

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StateGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for StateGraph

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for StateGraph

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more