Gemini Crate
A robust Rust client library for Google's Gemini AI API with built-in error handling, retry logic, and comprehensive model support.
Features
- 🚀 Simple API - Easy-to-use client for Gemini AI models
- 🔄 Automatic Retries - Built-in exponential backoff for network reliability
- 🌐 Starlink Optimized - Designed for satellite internet connections with dropout handling
- 📦 Model Discovery - List and discover available Gemini models
- 🛡️ Comprehensive Error Handling - Detailed error types for robust applications
- ⚡ Async/Await Support - Fully asynchronous with Tokio
- 🔧 Configurable - Flexible configuration options
Quick Start
1. Add to your project
[]
= "0.1.0"
= { = "1.0", = ["full"] }
= "0.15"
2. Set up your API key
Create a .env file in your project root:
GEMINI_API_KEY=your_gemini_api_key_here
Get your API key from Google AI Studio.
3. Basic usage
use GeminiClient;
async
Usage Examples
List Available Models
use GeminiClient;
async
Error Handling
use ;
async
Batch Processing
use GeminiClient;
use try_join_all;
async
Available Models
The library supports all current Gemini models:
| Model | Best For | Speed | Context |
|---|---|---|---|
gemini-2.5-flash |
General tasks | Fast | 1M tokens |
gemini-2.5-pro |
Complex reasoning | Medium | 2M tokens |
gemini-flash-latest |
Latest features | Fast | Variable |
gemini-pro-latest |
Latest pro features | Medium | Variable |
Use client.list_models() to see all available models and their capabilities.
Examples
Run the included examples:
# Interactive chat
# List all models
# Batch processing demo
Error Types
The library provides comprehensive error handling:
Error::Network- Network connectivity issuesError::Api- Gemini API errors (rate limits, invalid requests)Error::Json- Response parsing errorsError::Config- Configuration issues (missing API key)
Best Practices
1. Environment Setup
# .env file
GEMINI_API_KEY=your_api_key_here
RUST_LOG=info # Optional: for debugging
2. Rate Limiting
use Duration;
use sleep;
// Add delays between requests
for prompt in prompts
3. Model Selection
// For quick responses
let model = "gemini-2.5-flash";
// For complex reasoning
let model = "gemini-2.5-pro";
// For latest features
let model = "gemini-flash-latest";
Network Reliability
The library is designed for unreliable connections (like Starlink):
- ✅ Automatic retry with exponential backoff
- ✅ Transient error detection
- ✅ Timeout handling
- ✅ Network dropout recovery
Configuration
Environment Variables
GEMINI_API_KEY(required) - Your Gemini API key
Custom Configuration
use ;
let config = from_api_key;
let client = with_config;
Documentation
- Full Usage Guide - Comprehensive examples and patterns
- API Documentation - Complete API reference
- Examples - Ready-to-run example applications
Requirements
- Rust 2024 edition
- Tokio async runtime
- Valid Google Gemini API key
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass:
cargo test - Run clippy:
cargo clippy - Submit a pull request
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
Troubleshooting
Common Issues
"GEMINI_API_KEY must be set"
- Ensure your
.envfile is in the project root - Verify the API key is correct
- Call
dotenvy::dotenv().ok()before creating the client
"Model not found"
- Use
client.list_models()to see available models - Update to current model names (avoid deprecated ones like
gemini-pro)
Network timeouts
- The library has built-in retry logic
- For Starlink connections, consider application-level timeouts
- Check internet connectivity
For more help, see the full troubleshooting guide.