Skip to main content

StateGraph

Struct StateGraph 

Source
pub struct StateGraph { /* private fields */ }
Expand description

Builder for constructing a state graph.

S is the state type (typically a struct with #[derive(StateGraph)]). Channels are derived from S::create_channels() (the derive macro generates this).

§Example

use langgraph::prelude::*;

let mut graph = StateGraph::new(channels);
graph.add_node("agent", agent_fn);
graph.add_edge(START, "agent");
graph.add_edge("agent", END);
let compiled = graph.compile(checkpointer, None, None, None, None, false, None);

Implementations§

Source§

impl StateGraph

Source

pub fn new(channels: HashMap<String, Box<dyn Channel>>) -> Self

Create a new StateGraph with the given channels.

Typically called via the derive macro: MyState::create_channels().

Source

pub fn add_node( &mut self, name: impl Into<String>, action: impl IntoNodeFunction, ) -> Result<&mut Self, GraphError>

Add a node to the graph.

Accepts async closures (the default), sync closures via node_fn!() or SyncNodeFn, or pre-built Arc<dyn Runnable>.

§Examples
// Async closure (default)
graph.add_node("agent", |input, _config| async move {
    Ok(json!({"result": "done"}))
})?;

// Sync closure via node_fn! macro
graph.add_node("doubler", node_fn!(|input, _config| {
    let n = input.as_i64().unwrap_or(0);
    Ok(json!(n * 2))
}))?;
Source

pub fn add_edge( &mut self, start: impl Into<String>, end: impl Into<String>, ) -> Result<&mut Self, GraphError>

Add a direct edge from start to end.

start can be a node name or START. end can be a node name or END.

Source

pub fn add_join_edge( &mut self, starts: Vec<String>, end: impl Into<String>, ) -> Result<&mut Self, GraphError>

Add a multi-source join edge.

The graph waits for ALL starts to complete before routing to end.

Source

pub fn add_conditional_edges( &mut self, source: impl Into<String>, path: impl IntoNodeFunction, path_map: Option<HashMap<String, String>>, ) -> Result<&mut Self, GraphError>

Add conditional edges from source.

The path function evaluates the state and returns a routing key. The path_map maps routing keys to destination node names. If path_map is None, the routing key is used directly as the node name.

Source

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

Set the entry point (equivalent to add_edge(START, key)).

Source

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

Set the finish point (equivalent to add_edge(key, END)).

Source

pub fn compile(&mut self) -> Result<CompiledStateGraph, GraphError>

Compile the graph into an executable CompiledStateGraph.

The compiled graph implements Runnable and can be invoked with state. Uses all defaults (no checkpointer, no cache, no store, etc.).

For custom configuration, use compile_builder().

Source

pub fn compile_builder(&mut self) -> CompileBuilder<'_>

Start building compile options with a builder pattern.

§Example
let compiled = graph.compile_builder()
    .debug(true)
    .name("my_graph")
    .build()?;

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> 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.