QAI SDK
A modular, type-safe Rust SDK for AI providers. One unified API across OpenAI, Anthropic Claude, Google Gemini, DeepSeek, xAI Grok, GroqCloud, and any OpenAI-compatible endpoint.
Features
| Capability | OpenAI | Anthropic | DeepSeek | xAI | GroqCloud | Ollama | Compatible | |
|---|---|---|---|---|---|---|---|---|
| Chat / Language Model | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Streaming | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Tool Calling | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Structured Output (generate_object) |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Provider Registry | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Middleware Layer | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Universal Agent | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Reasoning / Thinking | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Prompt KV Caching | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | — | ✅ |
| Vision / Multimodal | ✅ | ✅ | ✅ | — | ✅ | ✅ | ✅ | — |
| Embeddings | ✅ | — | ✅ | — | — | — | ✅ | — |
| Image Generation | ✅ | — | ✅ | — | ✅ | — | — | — |
| Speech (TTS) | ✅ | — | — | — | — | ✅ | — | — |
| Transcription (STT) | ✅ | — | — | — | — | ✅ | — | — |
| Text Completion | ✅ | — | — | — | — | — | — | — |
| Responses API | ✅ | — | — | — | ✅ | ✅ | — | — |
| Content Moderation | — | — | — | — | — | ✅ | — | — |
| Built-in Web Search | — | — | ✅ | — | — | ✅ | — | — |
| Remote MCP Tools | — | — | — | — | — | ✅ | — | — |
| Model Context Protocol (MCP) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Custom HTTP Headers | ✅ | — | — | — | ✅ | — | ✅ | ✅ |
| Native Management APIs | — | — | — | — | — | — | ✅ | — |
Unified API Demo
The playground.html showcase demonstrates the lightning-fast API flexibility. Open it locally to interact with it directly.
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
By default, all providers are enabled. To optimize compile times, disable default features and select only the providers you need:
[]
= { = "0.1", = false, = ["openai", "anthropic"] }
Basic Usage
use *;
async
Streaming
use *;
use StreamExt;
let model = provider.chat;
let mut stream = model.generate_stream.await?;
while let Some = stream.next.await
Switch Providers in One Line
// OpenAI
let provider = create_openai;
// Anthropic
let provider = create_anthropic;
// Google Gemini
let provider = create_google;
// DeepSeek
let provider = create_deepseek;
// xAI Grok
let provider = create_xai;
// GroqCloud
let provider = create_groqcloud;
// Ollama
let provider = create_ollama;
// Any OpenAI-compatible API
let provider = create_openai_compatible;
Reasoning / Thinking
// Works across Gemini, Claude, Grok, DeepSeek, OpenAI
let result = model.generate.await?;
if let Some = &result.reasoning
println!;
Prompt Caching (xAI with Sticky Routing)
use HashMap;
let mut headers = new;
headers.insert;
let result = model.generate.await?;
// Cache metrics are exposed in usage
if let Some = result.usage.cache_hit_tokens
Provider Registry — Resolve Models by String
use ProviderRegistry;
let registry = new
.register
.register;
let model = registry.language_model?;
let result = model.generate.await?;
Structured Output — Force JSON Schema Conformance
use *;
let result = generate_object.await?;
println!; // {"name": "John Doe", "age": 30}
Middleware — Composable Model Wrappers
use *;
let wrapped = wrap_language_model;
// Every call now uses temperature=0.7 if not explicitly set
Universal Agent — Multi-Step Tool Loop
use Agent;
let agent = builder
.model
.tools
.tool_handler
.max_steps
.system
.build
.expect;
let result = agent.run.await?;
println!;
Documentation
Dive deep into specific provider features and initialization parameters in our comprehensive module docs:
- Core Interoperability
qai_sdk::core - OpenAI Provider
qai_sdk::openai - Anthropic Provider
qai_sdk::anthropic - Google Gemini Provider
qai_sdk::google - DeepSeek Provider
qai_sdk::deepseek - xAI Grok Provider
qai_sdk::xai - GroqCloud Provider
qai_sdk::groqcloud - Ollama Provider
qai_sdk::ollama - OpenAI Compatible Provider
qai_sdk::openai_compatible - Model Context Protocol
qai_sdk::mcp - Structured Output
qai_sdk::core::structured - Provider Registry
qai_sdk::core::registry - Middleware
qai_sdk::core::middleware - Universal Agent
qai_sdk::core::agent
Architecture
qai-sdk is a single, monolithic crate designed with zero-cost abstractions. Providers are organically separated via modular architecture and gated by Cargo features, keeping compile times fast when you only need specific integrations:
qai-sdk
├── core
│ ├── traits — LanguageModel, EmbeddingModel, ImageModel, SpeechModel, TranscriptionModel
│ ├── structured — generate_object() / stream_object() with JSON Schema validation
│ ├── registry — ProviderRegistry for "provider:model" string resolution
│ ├── middleware — Composable LanguageModelMiddleware (DefaultSettings, ExtractReasoning)
│ └── agent — Universal Agent with builder pattern & max_steps tool loop
├── openai — OpenAI API (GPT, DALL-E, Whisper, TTS, Responses)
├── anthropic — Anthropic API (Claude, Extended/Adaptive Thinking)
├── google — Google API (Gemini, Thinking/Reasoning)
├── deepseek — DeepSeek API (via OpenAI-compatible pipeline)
├── xai — xAI API (Grok, Reasoning, Prompt Caching, Image Gen, Responses)
├── groqcloud — GroqCloud API (Chat, Vision, STT, TTS, Reasoning, Moderation, Web Search, MCP)
├── ollama — Ollama API (Cloud and Local, Management APIs, via OpenAI-compatible pipeline)
├── openai_compatible — Any OpenAI-compatible endpoint (LM Studio)
└── mcp — Model Context Protocol (JSON-RPC, Stdio/SSE, resources, prompts)
Examples
See the examples/ directory for 42 comprehensive examples covering:
- Basic chat, streaming, and multimodal conversations
- Tool calling / function calling with built-in and remote tools
- Reasoning / thinking across Gemini, Claude, Grok, and DeepSeek
- Prompt caching with xAI's sticky routing
- GroqCloud: vision, speech (TTS/STT), moderation, web search, MCP
- Embeddings, image generation, speech, and transcription
- OpenAI Responses API and xAI Responses API
- Structured output with JSON Schema
- Error handling patterns
- Provider factory pattern
- OpenAI-compatible endpoints (Ollama, LM Studio, etc.)
Run an example:
# Fill in your API keys
Environment Variables
| Variable | Provider |
|---|---|
OPENAI_API_KEY |
OpenAI |
ANTHROPIC_API_KEY |
Anthropic |
GOOGLE_API_KEY |
Google Gemini |
DEEPSEEK_API_KEY |
DeepSeek |
XAI_API_KEY |
xAI |
GROQ_API_KEY |
GroqCloud |
OLLAMA_API_KEY |
Ollama (for Cloud) |
Contributing
See CONTRIBUTING.md for guidelines.
License
Licensed under either of:
at your option.
Author
Keyvan Arasteh — @keyvanarasteh