Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
π¦οΈπLangChain AI Rust
β‘ Building applications with LLMs through composability, with Rust! β‘
π€ What is this?
This is the Rust language implementation of LangChain, providing a powerful and type-safe way to build LLM applications in Rust.
β¨ Key Features
- π Multiple LLM Providers: Support for OpenAI, Azure OpenAI, Anthropic Claude, MistralAI, Google Gemini, AWS Bedrock, HuggingFace, Alibaba Qwen, DeepSeek, and Ollama
- π Chains: LLM chains, conversational chains, sequential chains, Q&A chains, SQL chains, and more
- π€ Agents: Chat agents with tools, multi-agent systems (router, subagents, skills, handoffs)
- π RAG: Agentic RAG, Hybrid RAG, and two-step RAG implementations
- π§ Memory: Simple memory, conversational memory, and long-term memory with metadata
- π οΈ Tools: Search tools, command line, Wolfram Alpha, text-to-speech, and more
- π Document Loaders: PDF, HTML, CSV, Git commits, source code, and more
- ποΈ Vector Stores: PostgreSQL (pgvector), Qdrant, SQLite (VSS/Vec), SurrealDB, OpenSearch, In-Memory, Chroma, FAISS (hnsw_rs), MongoDB Atlas, Pinecone, Weaviate
- π― Embeddings: OpenAI, Azure OpenAI, Ollama, FastEmbed, MistralAI
- π§ Middleware: Logging, PII detection, content filtering, rate limiting, retry, and custom middleware
- π¨ Structured Output: JSON schema validation and structured response generation
- βοΈ Runtime Context: Dynamic prompts, typed context, and runtime-aware middleware
- π LangGraph: State graphs, streaming, persistence (SQLite/memory), interrupts, subgraphs, and time-travel debugging
- π€ Deep Agent: Planning (write_todos), filesystem tools (ls, read_file, write_file, edit_file), skills, long-term memory, and human-in-the-loop
π¦ Installation
This library heavily relies on serde_json for its operation.
Step 1: Add serde_json
First, ensure serde_json is added to your Rust project.
Step 2: Add langchain-ai-rust
Then, you can add langchain-ai-rust to your Rust project.
Simple install
With Vector Stores
PostgreSQL (pgvector)
Qdrant
SQLite (VSS)
Download additional sqlite_vss libraries from https://github.com/asg017/sqlite-vss
SQLite (Vec)
Download additional sqlite_vec libraries from https://github.com/asg017/sqlite-vec
SurrealDB
OpenSearch
In-Memory
Chroma
FAISS (hnsw_rs)
MongoDB Atlas Vector Search
Pinecone
Weaviate
With LLM Providers
Ollama
MistralAI
Google Gemini
AWS Bedrock
With Document Loaders
PDF (pdf-extract)
PDF (lopdf)
HTML to Markdown
With Code Parsing
Tree-sitter (for source code parsing, requires 0.26+)
With FastEmbed (Local Embeddings)
π Quick Start
Simple LLM Invocation
use ;
async
Using init_chat_model (Recommended)
The init_chat_model function provides a unified interface to initialize any supported LLM:
use init_chat_model;
async
Supported model formats:
gpt-4o-mini,gpt-4o,gpt-4-turbo(OpenAI)claude-3-5-sonnet-20241022(Anthropic)mistralai/mistral-large-latest(MistralAI)gemini-1.5-pro(Google Gemini)anthropic.claude-3-5-sonnet-20241022-v2:0(AWS Bedrock)meta-llama/Llama-3.1-8B-Instruct(HuggingFace)qwen-plus(Alibaba Qwen)deepseek-chat(DeepSeek)llama3(Ollama)
Conversational Chain
use ;
async
Creating an Agent with Tools
use Arc;
use ;
async
LangGraph (Hello World)
Build a state graph with MessagesState, add a node with function_node, connect START to the node and the node to END, then compile and invoke:
use ;
use Message;
let mock_llm = function_node;
let mut graph = new;
graph.add_node?;
graph.add_edge;
graph.add_edge;
let compiled = graph.compile?;
let initial_state = with_messages;
let final_state = compiled.invoke.await?;
See LangGraph Hello World and LangGraph Streaming for more.
Deep Agent (Basic)
Use create_deep_agent with planning and filesystem enabled; the agent gets a workspace and built-in tools (write_todos, ls, read_file, write_file, edit_file):
use ;
let workspace = temp_dir.join;
create_dir_all?;
let config = new
.with_planning
.with_filesystem
.with_workspace_root;
let agent = create_deep_agent?;
let result = agent
.invoke
.await?;
See Deep Agent Basic and Deep Agent Customization for more.
π Current Features
LLMs
- OpenAI
- Azure OpenAI
- Anthropic Claude
- MistralAI
- Google Gemini
- AWS Bedrock
- HuggingFace
- Alibaba Qwen
- DeepSeek
- Ollama
- Unified Model Initialization
Embeddings
Vector Stores
- PostgreSQL (pgvector)
- Qdrant
- SQLite VSS
- SQLite Vec
- SurrealDB
- OpenSearch
- In-Memory
- Chroma
- FAISS
- MongoDB Atlas
- Pinecone
- Weaviate
Chains
- LLM Chain
- Conversational Chain
- Conversational Retriever Simple
- Conversational Retriever With Vector Store
- Sequential Chain
- Q&A Chain
- SQL Chain
- Streaming Chain
Agents
- Simple Agent
- Chat Agent with Tools
- OpenAI Compatible Tools Agent
- Multi-Agent Router
- Multi-Agent Subagents
- Multi-Agent Skills
- Multi-Agent Handoffs
LangGraph
- Hello World
- Streaming
- Persistence Basic, Persistence SQLite, Persistence Replay
- Interrupts, Interrupts Approval, Interrupts Review
- Subgraph Shared State, Subgraph Streaming
- Memory Store, Memory Basic
- Agent Workflow, Parallel Execution, Time Travel, Task Example
Deep Agent
- Basic (planning + filesystem)
- Customization
- Skills
- Planning
- Filesystem
- Human-in-the-Loop
- Long-term Memory
- With Task Tool
Text Splitters
Text Structure-Based
- RecursiveCharacterTextSplitter - Recommended default, splits recursively by separators
- CharacterTextSplitter - Simple character-based splitting with single separator
- PlainTextSplitter - Basic text splitting
- TokenSplitter - Token-based splitting (Tiktoken)
Document Structure-Based
- MarkdownSplitter - Split Markdown by structure
- HTMLSplitter - Split HTML by tags
- JsonSplitter - Split JSON by objects/arrays
- CodeSplitter - Split code by syntax tree (tree-sitter 0.26+, requires
tree-sitterfeature)
RAG (Retrieval-Augmented Generation)
- Agentic RAG - Agent decides when to retrieve
- Hybrid RAG - Combines multiple retrieval strategies
- Two-Step RAG - Two-stage retrieval process
Retrievers
External Index Retrievers
- Wikipedia Retriever - Retrieve Wikipedia articles
- Arxiv Retriever - Retrieve academic papers from arXiv
- Tavily Search API Retriever - Real-time web search
Algorithm-Based Retrievers
- BM25 Retriever - BM25 algorithm for text retrieval
- TF-IDF Retriever - TF-IDF based retrieval
- SVM Retriever - Support Vector Machine based retrieval
Rerankers
- Cohere Reranker - Rerank using Cohere API
- FlashRank Reranker - Local ONNX model reranking
- Contextual AI Reranker - Contextual AI API reranking
Hybrid Retrievers
- Merger Retriever - Combine multiple retrievers
- Ensemble Retriever - Voting mechanism from multiple retrievers
Query Enhancement Retrievers
- RePhrase Query Retriever - LLM-based query rephrasing
- Multi Query Retriever - Generate multiple query variations
Document Compression Retrievers
- Embeddings Redundant Filter - Filter redundant documents by similarity
Tools
- Serpapi/Google Search
- DuckDuckGo Search
- Wolfram Alpha
- Command Line Executor
- Text-to-Speech
- Speech-to-Text
- Advanced Tools
Middleware
- Logging Middleware
- PII Detection
- Content Filtering
- Custom Middleware
- Runtime-Aware Middleware
- Dynamic Prompt Middleware
Memory
- Simple Memory
- Conversational Memory
- Long-Term Memory (Basic)
- Long-Term Memory (Search)
- Long-Term Memory (Tool)
Runtime & Context
Structured Output
Advanced Features
- Configurable Models
- Invocation Config
- Semantic Routing
- Dynamic Semantic Routing
- Vision LLM Chain
- Tool Runtime
Document Loaders
Common File Types
- PDF (pdf-extract or lopdf)
- HTML
- HTML to Markdown
- CSV
- TSV (Tab-Separated Values)
- JSON (including JSONL)
- Markdown
- TOML (with
tomlfeature) - YAML (with
yamlfeature) - XML (with
xmlfeature)
Office Documents
- Excel (.xlsx, .xls) (with
excelfeature) - Word, PowerPoint, and more via PandocLoader
Web Loaders
- WebBaseLoader - Load content from URLs
- RecursiveURLLoader - Recursively crawl websites
- SitemapLoader - Load all URLs from sitemap.xml (with
xmlfeature)
Cloud Storage
- AWS S3 (with
aws-s3feature)
Productivity Tools
- GitHub (with
githubfeature) - Git Commits (with
gitfeature)
Other
- Source Code (with tree-sitter feature)
- Pandoc (various formats: docx, epub, html, ipynb, markdown, etc.)
See the examples directory for complete examples of each feature.
π§ Configuration
Environment Variables
For OpenAI:
For Anthropic:
For MistralAI:
For Google Gemini:
For AWS Bedrock:
π Documentation
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
- LangChain - The original Python implementation
- All contributors and users of this library