| Crates.io | gemini-rust |
| lib.rs | gemini-rust |
| version | 1.7.1 |
| created_at | 2025-03-25 01:10:15.892531+00 |
| updated_at | 2026-01-17 11:53:41.00819+00 |
| description | Rust client for Google Gemini API |
| homepage | |
| repository | https://github.com/flachesis/gemini-rust |
| max_upload_size | |
| id | 1604614 |
| size | 2,256,127 |
A comprehensive Rust client library for Google's Gemini API.
serde supporttokio for high-performance async operationstracing for observabilityAdd this to your Cargo.toml:
[dependencies]
gemini-rust = "1.7.1"
Get started with simple text generation, system prompts, and conversations. See basic_generation.rs for complete examples including simple messages, system prompts, and multi-turn conversations.
Enable real-time content streaming for interactive applications. See basic_streaming.rs for examples of processing content as it's generated with immediate display.
Add location-aware capabilities to your applications with Google Maps integration. See simple_maps_example.rs for basic usage and google_maps_grounding.rs for comprehensive examples.
Calculate token usage before making generation requests for cost estimation and optimization. See count_tokens.rs.
Customize content moderation with granular control over different harm categories and block thresholds. See safety_settings.rs.
Access the latest model features including code execution and advanced thinking levels. See gemini_3_code_execution.rs for code execution and gemini_3_thinking_and_media.rs for thinking levels.
The library provides comprehensive access to all Gemini 2.5 capabilities through an intuitive Rust API:
Advanced reasoning capabilities with thought process visibility and custom thinking budgets. See thinking_basic.rs and thinking_advanced.rs.
schemars)tools.rs, complex_function.rs, and google_maps_grounding.rssimple_maps_example.rs and advanced_maps_configuration.rsimage_generation.rs and multi_speaker_tts.rsEfficient processing of multiple requests with automatic file handling for large jobs. See batch_generate.rs.
Cache system instructions and conversation history to reduce costs and improve performance. See cache_basic.rs.
Advanced embedding generation with multiple task types for document retrieval and semantic search. See embedding.rs.
category = "api-docs")file_search_basic.rs, file_search_metadata.rs, and file_search_import.rsReal-time streaming of generated content for interactive applications. See streaming.rs.
generation_config.rs and custom_base_url.rsBuilt-in structured logging and telemetry with tracing for comprehensive monitoring and debugging.
Pre-calculate token usage for cost estimation and optimization. Calculate tokens for your requests before executing them. See count_tokens.rs.
Customize content moderation with granular control over different harm categories (Hate Speech, Dangerous Content, etc.) and block thresholds (Block None, Low, Medium, High). See safety_settings.rs.
gemini_3_code_execution.rs and gemini_3_thinking_and_media.rsEfficiently reference previously uploaded files without re-encoding. Upload files once and reference them multiple times, reducing data transfer. Supports PDFs, images, and other binary formats. See file_input.rs and files_usage.rs.
Configure different Gemini models including Flash, Pro, Lite, and custom models. See custom_models.rs for examples of all model configuration options including convenience methods, enum variants, and custom model strings.
Use custom API endpoints and configurations. See custom_base_url.rs for examples of configuring custom endpoints with different models.
For advanced HTTP configuration (timeouts, proxies, custom headers), use the builder pattern. See http_client_builder.rs for a complete example with custom timeouts, user agents, connection pooling, and proxy configuration.
The library is instrumented with the tracing crate to provide detailed telemetry data for monitoring and debugging. This allows you to gain deep insights into the library's performance and behavior.
Key tracing features include:
To use these features, you will need to integrate a tracing subscriber into your application. See tracing_telemetry.rs for comprehensive examples including basic console logging, structured logging for production, and environment-based log level filtering.
The repository includes 30+ comprehensive examples demonstrating all features. See examples/README.md for detailed information.
basic_generation.rs - Simple content generation for beginnersbasic_streaming.rs - Real-time streaming responsessimple.rs - Comprehensive example with function callingthinking_basic.rs - Gemini 2.5 thinking modecount_tokens.rs - Pre-calculate token usagesafety_settings.rs - Configure safety filtersgemini_3_code_execution.rs - Code execution with Pythonfile_input.rs - Upload and reference filesbatch_generate.rs - Batch content generationimage_generation.rs - Text-to-image generationgoogle_search.rs - Google Search integrationurl_context.rs - URL Context tool for web content analysisRun any example:
GEMINI_API_KEY="your-api-key" cargo run --example basic_generation
Get your API key from Google AI Studio and set it as an environment variable:
export GEMINI_API_KEY="your-api-key-here"
Model::Gemini25FlashModel::Gemini25FlashLiteModel::Gemini25ProModel::Gemini3Pro (Preview)Model::Gemini3Flash (Preview)Model::TextEmbedding004Model::Custom(String) or string literals for other modelsContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
For guidelines on developing agents and applications, see the Agent Development Guide.
This project is licensed under the MIT License - see the LICENSE file for details.