Expand description
§langgraph4rust
A Rust implementation of a workflow engine inspired by Python’s LangGraph library. This crate provides a flexible and powerful way to build, execute, and manage stateful workflow graphs with support for parallel execution and conditional routing.
§Features
- Declarative Graph Building: Define workflows using a builder pattern
- Parallel Execution: Multiple nodes can execute simultaneously when possible
- Conditional Routing: Dynamic path selection based on state conditions
- State Management: Built-in JSON-based state persistence with type safety
- Extensible Architecture: Custom node implementations via traits
- Streaming Execution: Real-time push-based event stream via
StateGraph::stream - Validation: Comprehensive graph validation before execution
§Quick Start
use langgraph4rust::*;
use std::collections::HashSet;
use std::sync::Arc;
use async_trait::async_trait;
// Define a custom node
#[derive(Clone)]
struct GreetingNode;
#[async_trait]
impl AgentNode<DefaultMemoryState> for GreetingNode {
async fn apply(&self, state: Arc<DefaultMemoryState>) -> Result<(), LangGraphError> {
state.set("message", "Hello from langgraph4rust!").await?;
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), LangGraphError> {
let mut builder = StateGraphBuilder::new();
builder.add_node("greet", Box::new(GreetingNode));
builder.add_edge(START_NODE, HashSet::from(["greet".to_string()]));
builder.add_edge("greet", HashSet::from([END_NODE.to_string()]));
let graph = builder.compile()?;
let state = Arc::new(DefaultMemoryState::new());
graph.invoke(state).await?;
Ok(())
}§Core Concepts
§Nodes
Nodes are the basic building blocks of your workflow. Each node implements the
AgentNode trait and contains the logic to process and modify the state.
§Edges
Edges define the flow between nodes. There are two types:
- Static edges: Always connect to the same target nodes
- Conditional edges: Dynamically choose targets based on current state
§State
The state is shared across all nodes and persists throughout the workflow execution.
By default, DefaultMemoryState provides JSON-based storage.
§Streaming
Besides StateGraph::invoke, a compiled graph can be executed as a push-based
event stream via StateGraph::stream, yielding StreamEvent values in real
time. The stream always ends with either WorkflowFinished (success) or
WorkflowError (failure):
use langgraph4rust::*;
use std::collections::HashSet;
use std::sync::Arc;
use async_trait::async_trait;
#[derive(Clone)]
struct Noop;
#[async_trait]
impl AgentNode<DefaultMemoryState> for Noop {
async fn apply(&self, _state: Arc<DefaultMemoryState>) -> Result<(), LangGraphError> {
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), LangGraphError> {
let mut builder = StateGraphBuilder::new();
builder.add_node("noop", Box::new(Noop));
builder.add_edge(START_NODE, HashSet::from(["noop".to_string()]));
builder.add_edge("noop", HashSet::from([END_NODE.to_string()]));
let graph = Arc::new(builder.compile()?);
let mut events = graph.stream(Arc::new(DefaultMemoryState::new()));
let mut finished = false;
while let Some(event) = events.next().await {
if matches!(event, StreamEvent::WorkflowFinished { .. }) {
finished = true;
}
}
assert!(finished);
Ok(())
}§Examples
See the examples/ directory for complete working examples including:
- Simple linear workflows
- Parallel execution patterns
- Conditional routing
- Custom state implementations
§License
Apache License 2.0
Re-exports§
pub use core::state_graph::StateGraph;pub use core::state_graph_stream::StreamEvent;
Modules§
- core
- Core engine internals of the
langgraph4rustworkflow engine.
Structs§
- Default
Memory State - Default in-memory implementation of
AgentStateusing JSON storage. - State
Graph Builder - Builder for constructing and validating workflow graphs.
Enums§
- Lang
Graph Error - Comprehensive error type for the langgraph4rust workflow engine.
Constants§
- END_
NODE - Special node name representing the exit point of the workflow.
- START_
NODE - Special node name representing the entry point of the workflow.
Traits§
- Agent
Node - Trait for defining executable nodes in a workflow graph.
- Agent
State - Trait defining the interface for workflow state management.
- Stream
- A stream of values produced asynchronously.
- Stream
Ext - An extension trait for
Streams that provides a variety of convenient combinator functions.