rust-langgraph 0.1.0

Stateful graph runtime for LLM workflows in Rust (community project; not affiliated with LangChain). Pregel-style execution: nodes, conditional edges, checkpoints, streaming. Optional adapters for Ollama, OpenAI, OpenRouter (OpenAI-compatible), and Anthropic; optional ReAct agent + tools. Crate import: rust_langgraph (underscore). Default features include in-memory checkpoints. Enable Cargo features explicitly for LLM modules (e.g. ollama, openai, openrouter, anthropic, prebuilt). See README.md on crates.io for copy-paste Cargo.toml, env vars, and common mistakes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! StateGraph builder and CompiledGraph.
//!
//! StateGraph provides an ergonomic builder API for creating graphs,
//! which then compile into executable CompiledGraph instances.

use crate::channels::{BaseChannel, LastValue};
use crate::checkpoint::{BaseCheckpointSaver, CheckpointMetadata, StateSnapshot};
use crate::config::Config;
use crate::errors::{Error, Result};
use crate::graph::{START, END};
use crate::nodes::{Node, PregelNode, ChannelWrite, NodeArc};
use crate::pregel::{Branch, Pregel};
use crate::state::State;
use crate::types::{StreamEvent, StreamMode};
use futures::stream::Stream;
use std::collections::{HashMap, HashSet};
use std::pin::Pin;
use std::sync::Arc;

/// 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
///
/// ```rust
/// use rust_langgraph::{StateGraph, Config};
/// # use rust_langgraph::{State, Error};
/// # #[derive(Clone, serde::Serialize, serde::Deserialize)]
/// # struct MyState { count: i32 }
/// # impl State for MyState {
/// #     fn merge(&mut self, other: Self) -> Result<(), Error> {
/// #         self.count += other.count;
/// #         Ok(())
/// #     }
/// # }
///
/// 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();
/// ```
pub struct StateGraph<S: State> {
    nodes: HashMap<String, Box<dyn Node<S>>>,
    edges: HashMap<String, Vec<String>>,
    conditional_edges: HashMap<String, Box<dyn Branch<S>>>,
    entry_point: Option<String>,
    finish_points: HashSet<String>,
}

impl<S: State> StateGraph<S> {
    /// Create a new StateGraph
    pub fn new() -> Self {
        Self {
            nodes: HashMap::new(),
            edges: HashMap::new(),
            conditional_edges: HashMap::new(),
            entry_point: None,
            finish_points: HashSet::new(),
        }
    }

    /// Add a node to the graph
    ///
    /// # Arguments
    ///
    /// * `name` - Unique identifier for this node
    /// * `node` - The node implementation (function or struct implementing Node)
    ///
    /// # Example
    ///
    /// ```rust
    /// # use rust_langgraph::{StateGraph, Config, State, Error};
    /// # #[derive(Clone, serde::Serialize, serde::Deserialize)]
    /// # struct MyState { count: i32 }
    /// # impl State for MyState {
    /// #     fn merge(&mut self, other: Self) -> Result<(), Error> { Ok(()) }
    /// # }
    /// let mut graph = StateGraph::new();
    ///
    /// graph.add_node("increment", |mut state: MyState, _config: &Config| async move {
    ///     state.count += 1;
    ///     Ok(state)
    /// });
    /// ```
    pub fn add_node(&mut self, name: impl Into<String>, node: impl Node<S> + 'static) -> &mut Self {
        self.nodes.insert(name.into(), Box::new(node));
        self
    }

    /// Add a static edge from one node to another
    ///
    /// # Arguments
    ///
    /// * `from` - Source node name
    /// * `to` - Target node name
    pub fn add_edge(&mut self, from: impl Into<String>, to: impl Into<String>) -> &mut Self {
        let from = from.into();
        let to = to.into();

        self.edges.entry(from).or_default().push(to);
        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
    ///
    /// ```rust
    /// # use rust_langgraph::{StateGraph, Config, State, Error};
    /// # use rust_langgraph::pregel::BranchResult;
    /// # #[derive(Clone, serde::Serialize, serde::Deserialize)]
    /// # struct MyState { value: i32 }
    /// # impl State for MyState {
    /// #     fn merge(&mut self, other: Self) -> Result<(), Error> { Ok(()) }
    /// # }
    /// 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"))
    ///         }
    ///     }
    /// );
    /// ```
    pub fn add_conditional_edges(
        &mut self,
        source: impl Into<String>,
        branch: impl Branch<S> + 'static,
    ) -> &mut Self {
        self.conditional_edges.insert(source.into(), Box::new(branch));
        self
    }

    /// Set the entry point for the graph
    ///
    /// This is the first node to execute when the graph is invoked.
    pub fn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self {
        self.entry_point = Some(node.into());
        self
    }

    /// Set a finish point for the graph
    ///
    /// When execution reaches a finish point, the graph completes.
    pub fn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self {
        self.finish_points.insert(node.into());
        self
    }

    /// Add multiple finish points
    pub fn add_finish_points(&mut self, nodes: Vec<impl Into<String>>) -> &mut Self {
        for node in nodes {
            self.finish_points.insert(node.into());
        }
        self
    }

    /// 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)
    pub fn compile(
        self,
        checkpointer: Option<Arc<dyn BaseCheckpointSaver>>,
    ) -> Result<CompiledGraph<S>> {
        // Validate graph
        if self.entry_point.is_none() {
            return Err(Error::invalid_graph("No entry point set"));
        }

        let entry_point = self.entry_point.unwrap();

        if !self.nodes.contains_key(&entry_point) {
            return Err(Error::invalid_graph(format!(
                "Entry point '{}' is not a valid node",
                entry_point
            )));
        }

        // Build PregelNodes from our nodes
        let mut pregel_nodes = HashMap::new();

        for (name, node) in self.nodes {
            // Determine triggers: nodes triggered by their dependencies
            let mut triggers = vec![];

            // If this is the entry point, trigger on START
            if name == entry_point {
                triggers.push(START.to_string());
            }

            // Add triggers from incoming edges
            for (source, targets) in &self.edges {
                if targets.contains(&name) {
                    triggers.push(format!("{}_output", source));
                }
            }

            // If no triggers, add a default
            if triggers.is_empty() {
                triggers.push(format!("{}_input", name));
            }

            let pregel_node = PregelNode::new(
                name.clone(),
                vec![format!("{}_input", name)],
                triggers,
                Arc::from(node) as NodeArc<S>,
                vec![ChannelWrite::new(format!("{}_output", name))],
            );

            pregel_nodes.insert(name, pregel_node);
        }

        // Create channels
        let mut channels: HashMap<String, Box<dyn BaseChannel>> = HashMap::new();

        // Add START and END channels
        channels.insert(START.to_string(), Box::new(LastValue::<S>::new()));
        channels.insert(END.to_string(), Box::new(LastValue::<S>::new()));

        // Add channels for each node
        for node_name in pregel_nodes.keys() {
            channels.insert(
                format!("{}_input", node_name),
                Box::new(LastValue::<S>::new()),
            );
            channels.insert(
                format!("{}_output", node_name),
                Box::new(LastValue::<S>::new()),
            );
        }

        // Create the Pregel engine
        let pregel = Pregel::new(
            pregel_nodes,
            channels,
            checkpointer.clone(),
            entry_point.clone(),
            self.finish_points.clone(),
            self.edges.clone(),
        );

        Ok(CompiledGraph {
            pregel,
            entry_point,
            finish_points: self.finish_points,
            checkpointer,
        })
    }
}

impl<S: State> Default for StateGraph<S> {
    fn default() -> Self {
        Self::new()
    }
}

/// A compiled, executable graph.
///
/// CompiledGraph is the result of compiling a StateGraph. It provides
/// methods to execute the graph with different invocation patterns.
pub struct CompiledGraph<S: State> {
    pregel: Pregel<S>,
    entry_point: String,
    finish_points: HashSet<String>,
    checkpointer: Option<Arc<dyn BaseCheckpointSaver>>,
}

impl<S: State> CompiledGraph<S> {
    /// Execute the graph with the given input
    ///
    /// This runs the graph to completion and returns the final state.
    ///
    /// # Arguments
    ///
    /// * `input` - Initial state
    /// * `config` - Execution configuration
    ///
    /// # Returns
    ///
    /// The final state after graph execution
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use rust_langgraph::{StateGraph, Config, State, Error};
    /// # #[derive(Clone, serde::Serialize, serde::Deserialize)]
    /// # struct MyState { count: i32 }
    /// # impl State for MyState {
    /// #     fn merge(&mut self, other: Self) -> Result<(), Error> { Ok(()) }
    /// # }
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let mut graph = StateGraph::new();
    /// # graph.add_node("test", |s: MyState, _| async move { Ok(s) });
    /// # graph.set_entry_point("test");
    /// # graph.set_finish_point("test");
    /// let app = graph.compile(None)?;
    ///
    /// let result = app.invoke(
    ///     MyState { count: 0 },
    ///     Config::default()
    /// ).await?;
    ///
    /// println!("Final count: {}", result.count);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn invoke(&mut self, input: S, config: Config) -> Result<S> {
        self.pregel.invoke(input, config).await
    }

    /// Stream execution events
    ///
    /// Returns a stream of events as the graph executes, allowing
    /// real-time observation of progress.
    ///
    /// # Arguments
    ///
    /// * `input` - Initial state
    /// * `config` - Execution configuration
    /// * `mode` - Type of events to stream
    pub async fn stream(
        &mut self,
        input: S,
        config: Config,
        mode: StreamMode,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamEvent>> + Send>>> {
        self.pregel.stream(input, config, mode).await
    }

    /// Get the current state for a given configuration
    ///
    /// This retrieves the most recent checkpoint for the thread.
    pub async fn get_state(&self, config: &Config) -> Result<Option<StateSnapshot<S>>> {
        self.pregel.get_state(config).await
    }

    /// Get the state history for a thread
    ///
    /// Returns past checkpoints in reverse chronological order.
    pub async fn get_state_history(
        &self,
        config: &Config,
        limit: Option<usize>,
    ) -> Result<Vec<StateSnapshot<S>>> {
        self.pregel.get_state_history(config, limit).await
    }

    /// Update the state for a thread
    ///
    /// This allows modifying the checkpoint state, useful for
    /// human-in-the-loop patterns.
    pub async fn update_state(&mut self, config: Config, values: S) -> Result<Config> {
        if let Some(checkpointer) = &self.checkpointer {
            // Get the current checkpoint
            let mut tuple = checkpointer
                .get_tuple(&config)
                .await?
                .ok_or_else(|| Error::checkpoint("No checkpoint found for config"))?;

            // Update the state
            let mut current_state = S::from_value(
                tuple
                    .checkpoint
                    .get_channel("__start__")
                    .ok_or_else(|| Error::checkpoint("No state in checkpoint"))?
                    .clone(),
            )?;

            current_state.merge(values)?;

            // Create new checkpoint
            tuple.checkpoint.set_channel("__start__", current_state.to_value()?);

            // Save the updated checkpoint
            let metadata = CheckpointMetadata {
                step: tuple.metadata.step + 1,
                source: "update_state".to_string(),
                created_at: chrono::Utc::now(),
                extra: HashMap::new(),
            };

            checkpointer.put(&tuple.checkpoint, &metadata, &config).await
        } else {
            Err(Error::checkpoint("No checkpointer configured"))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::checkpoint_backends::memory::MemorySaver;
    use serde::{Deserialize, Serialize};

    #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
    struct TestState {
        count: i32,
    }

    impl crate::state::State for TestState {
        fn merge(&mut self, other: Self) -> Result<()> {
            self.count += other.count;
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_state_graph_basic() {
        let mut graph = StateGraph::new();

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

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

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

        let result = app.invoke(TestState { count: 0 }, Config::default()).await.unwrap();
        assert_eq!(result.count, 1);
    }

    #[tokio::test]
    async fn test_state_graph_chain() {
        let mut graph = StateGraph::new();

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

        graph.add_node("multiply_two", |mut state: TestState, _config: &Config| async move {
            state.count *= 2;
            Ok(state)
        });

        graph.set_entry_point("add_one");
        graph.add_edge("add_one", "multiply_two");
        graph.set_finish_point("multiply_two");

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

        let result = app.invoke(TestState { count: 5 }, Config::default()).await.unwrap();
        assert_eq!(result.count, 12); // (5 + 1) * 2
    }

    #[tokio::test]
    async fn test_state_graph_with_checkpointer() {
        let mut graph = StateGraph::new();

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

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

        let checkpointer = Arc::new(MemorySaver::new());
        let mut app = graph.compile(Some(checkpointer)).unwrap();

        let config = Config::new().with_thread_id("test-123");
        let result = app.invoke(TestState { count: 0 }, config.clone()).await.unwrap();
        assert_eq!(result.count, 1);

        // Check that checkpoint was saved
        let snapshot = app.get_state(&config).await.unwrap();
        assert!(snapshot.is_some());
    }

    #[test]
    fn test_state_graph_no_entry_point() {
        let mut graph = StateGraph::<TestState>::new();
        graph.add_node("test", |s: TestState, _config: &Config| async move { Ok(s) });

        let result = graph.compile(None);
        assert!(result.is_err());
        if let Err(e) = result {
            assert!(e.to_string().contains("entry point"));
        }
    }
}