LangChain Rust OpenRouter
A fork of langchain-rust with first-class support for OpenRouter, providing unified access to multiple AI model providers through a single API.
Overview
This library extends langchain-rust with native OpenRouter integration, allowing you to:
- Access 200+ LLM models from OpenAI, Anthropic, Google, Meta, Mistral, and more through a single API
- Use OpenRouter's unified embeddings API with multiple embedding models
- Leverage automatic fallback routing between providers
- Configure provider preferences for cost optimization and data privacy
Installation
Add the dependency to your Cargo.toml:
[]
= { = "https://github.com/coolcmyk/langchain-rust-openrouter" }
= "1.0"
= { = "1", = ["full"] }
Environment Variables
Set your OpenRouter API key:
Usage
LLM Client
The Openrouter client provides access to all models available on OpenRouter.
Basic Usage
use ;
use LLM;
async
Using Different Models
use ;
// Using predefined model enum
let client = default
.with_model;
// Using custom model string
let client = default
.with_model;
// Available predefined models:
// - OpenrouterModel::Gpt4o
// - OpenrouterModel::Gpt4oMini
// - OpenrouterModel::Claude35Sonnet
// - OpenrouterModel::Claude3Opus
// - OpenrouterModel::Claude3Haiku
// - OpenrouterModel::Claude4_5Haiku
// - OpenrouterModel::Gemini2Flash
// - OpenrouterModel::Llama31405b
// - OpenrouterModel::DeepseekV3
// - OpenrouterModel::MistralLarge
Fallback Models
Configure automatic fallback to alternative models if the primary model fails:
use Openrouter;
let client = default
.with_model
.with_fallback_models;
Provider Preferences
Control which providers serve your requests:
use ;
let preferences = ProviderPreferences ;
let client = default
.with_model
.with_provider_preferences;
Streaming Responses
use Openrouter;
use LLM;
use ;
use StreamExt;
async
Using with Chains
use ;
async
Embeddings
The OpenrouterEmbedder provides access to embedding models through OpenRouter.
Basic Usage
use ;
async
Batch Document Embedding
use ;
async
Available Embedding Models
| Model | Dimensions | Description |
|---|---|---|
openai/text-embedding-3-small |
1536 | Fast and cost-effective |
openai/text-embedding-3-large |
3072 | Higher quality embeddings |
openai/text-embedding-ada-002 |
1536 | Legacy model |
qwen/qwen3-embedding-0.6b |
Variable | Lightweight option |
qwen/qwen3-embedding-4b |
Variable | Balanced performance |
Using with Vector Stores
use ;
use Qdrant;
async
Configuration Options
LLM Client Options
| Method | Description |
|---|---|
with_model() |
Set the model to use |
with_api_key() |
Set the API key |
with_base_url() |
Set custom base URL |
with_json_mode() |
Enable JSON output mode |
with_http_referer() |
Set app identification header |
with_x_title() |
Set app title header |
with_fallback_models() |
Set fallback model list |
with_provider_preferences() |
Configure provider routing |
with_top_k() |
Set top-k sampling |
with_repetition_penalty() |
Set repetition penalty |
with_min_p() |
Set min-p sampling |
with_top_a() |
Set top-a sampling |
with_seed() |
Set random seed |
Embedder Options
| Method | Description |
|---|---|
with_model() |
Set the embedding model |
with_api_key() |
Set the API key |
with_base_url() |
Set custom base URL |
with_http_referer() |
Set app identification header |
with_x_title() |
Set app title header |
Error Handling
The library provides specific error types for OpenRouter API responses:
use OpenrouterError;
// Error variants:
// - BadRequestError (400)
// - UnauthorizedError (401)
// - PaymentRequiredError (402)
// - RateLimitError (429)
// - BadGatewayError (502)
// - ServiceUnavailableError (503)
// - ProviderOverloadedError (529)
Additional Features
This fork includes all features from the original langchain-rust:
- Vector Stores: Qdrant, Postgres, SQLite, SurrealDB, OpenSearch
- Document Loaders: PDF, HTML, CSV, Pandoc, Git commits, Source code
- Chains: LLM Chain, Conversational Chain, Q&A Chain, SQL Chain
- Agents: Chat Agent with Tools, OpenAI Tools Agent
- Semantic Routing: Static and Dynamic routing
Credits
This project is a fork of langchain-rust by Abraxas-365.
License
MIT License - see the original repository for details.