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
impl StateGraph
Sourcepub fn new(channels: HashMap<String, Box<dyn Channel>>) -> Self
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().
Sourcepub fn add_node(
&mut self,
name: impl Into<String>,
action: impl IntoNodeFunction,
) -> Result<&mut Self, GraphError>
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))
}))?;Sourcepub fn add_edge(
&mut self,
start: impl Into<String>,
end: impl Into<String>,
) -> Result<&mut Self, GraphError>
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.
Sourcepub fn add_join_edge(
&mut self,
starts: Vec<String>,
end: impl Into<String>,
) -> Result<&mut Self, GraphError>
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.
Sourcepub fn add_conditional_edges(
&mut self,
source: impl Into<String>,
path: impl IntoNodeFunction,
path_map: Option<HashMap<String, String>>,
) -> Result<&mut Self, GraphError>
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.
Sourcepub fn set_entry_point(
&mut self,
key: impl Into<String>,
) -> Result<&mut Self, GraphError>
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)).
Sourcepub fn set_finish_point(
&mut self,
key: impl Into<String>,
) -> Result<&mut Self, GraphError>
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)).
Sourcepub fn compile(&mut self) -> Result<CompiledStateGraph, GraphError>
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().