Skip to main content

StateGraph

Struct StateGraph 

Source
pub struct StateGraph<S: State> { /* private fields */ }
Expand description

Builder for creating state-based graphs.

StateGraph provides a declarative API for building graphs where nodes communicate through shared state. It compiles into a CompiledGraph which can be executed.

§Example

use rust_langgraph::{StateGraph, Config};

let mut graph = StateGraph::new();

graph.add_node("process", |mut state: MyState, _config: &Config| async move {
    state.count += 1;
    Ok(state)
});

graph.set_entry_point("process");
graph.set_finish_point("process");

let app = graph.compile(None).unwrap();

Implementations§

Source§

impl<S: State> StateGraph<S>

Source

pub fn new() -> Self

Create a new StateGraph

Source

pub fn add_node( &mut self, name: impl Into<String>, node: impl Node<S> + 'static, ) -> &mut Self

Add a node to the graph

§Arguments
  • name - Unique identifier for this node
  • node - The node implementation (function or struct implementing Node)
§Example
let mut graph = StateGraph::new();

graph.add_node("increment", |mut state: MyState, _config: &Config| async move {
    state.count += 1;
    Ok(state)
});
Source

pub fn add_edge( &mut self, from: impl Into<String>, to: impl Into<String>, ) -> &mut Self

Add a static edge from one node to another

§Arguments
  • from - Source node name
  • to - Target node name
Source

pub fn add_conditional_edges( &mut self, source: impl Into<String>, branch: impl Branch<S> + 'static, ) -> &mut Self

Add conditional edges from a source node

The branch function examines state and returns which node(s) to route to next.

§Arguments
  • source - Source node name
  • branch - Branch logic that determines routing
§Example
let mut graph = StateGraph::new();

graph.add_conditional_edges(
    "check",
    |state: &MyState| async move {
        if state.value > 0 {
            Ok(BranchResult::single("positive"))
        } else {
            Ok(BranchResult::single("negative"))
        }
    }
);
Source

pub fn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self

Set the entry point for the graph

This is the first node to execute when the graph is invoked.

Source

pub fn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self

Set a finish point for the graph

When execution reaches a finish point, the graph completes.

Source

pub fn add_finish_points(&mut self, nodes: Vec<impl Into<String>>) -> &mut Self

Add multiple finish points

Source

pub fn compile( self, checkpointer: Option<Arc<dyn BaseCheckpointSaver>>, ) -> Result<CompiledGraph<S>>

Compile the graph into an executable CompiledGraph

§Arguments
  • checkpointer - Optional checkpoint saver for persistence
§Returns

A CompiledGraph ready to execute

§Errors

Returns an error if the graph configuration is invalid (e.g., no entry point)

Trait Implementations§

Source§

impl<S: State> Default for StateGraph<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S> Freeze for StateGraph<S>

§

impl<S> !RefUnwindSafe for StateGraph<S>

§

impl<S> Send for StateGraph<S>

§

impl<S> Sync for StateGraph<S>

§

impl<S> Unpin for StateGraph<S>

§

impl<S> UnsafeUnpin for StateGraph<S>

§

impl<S> !UnwindSafe for StateGraph<S>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.