gemini-rust
A comprehensive Rust client library for Google's Gemini API.
β¨ Features
- π€ Interactions API (Recommended) - Unified interface for models and agents with server-side state, observable execution steps, and background execution
- π€ Managed Agents - Built-in Deep Research and Antigravity agents with sandbox environments
- π Background Execution - Long-running interactions with polling and webhook callbacks
- π Complete Gemini API Implementation - Full support for all Gemini API endpoints
- π οΈ Function Calling & Tools - Custom functions, Google Search, and Google Maps integration with OpenAPI schema support
- πΊοΈ Google Maps Grounding - Location-aware responses with Google Maps data and widget support
- π¦ Batch Processing - Efficient batch content generation and embedding
- πΎ Content Caching - Cache system instructions and conversation history for cost optimization
- π Streaming Responses - Real-time streaming of generated content via SSE step lifecycle events
- π§ Thinking Mode - Support for Gemini 2.5+ thinking capabilities
- π Gemini 3 Pro - Code execution, advanced thinking levels, and media resolution control
- π’ Token Count API - Pre-calculate token usage for cost optimization
- π― Safety Settings - Customize content moderation and safety filters
- π File Handles - Efficient file reference without re-encoding large files
- π Image Generation - Text-to-image generation and image editing capabilities
- π€ Speech Generation - Text-to-speech with single and multi-speaker support
- πΌοΈ Multimodal Support - Images and binary data processing
- π Text Embeddings - Advanced embedding generation with multiple task types
- π File Search - Retrieval Augmented Generation (RAG) with semantic document search
- βοΈ Highly Configurable - Custom models, endpoints, and generation parameters with HTTP client builder
- π Type Safe - Comprehensive type definitions with full
serdesupport - β‘ Async/Await - Built on
tokiofor high-performance async operations - π Comprehensive Tracing - Built-in structured logging and telemetry with
tracingfor observability
π¦ Installation
Add this to your Cargo.toml:
[]
= "2.0.0"
π Quick Start
Interactions API (Recommended)
The Interactions API is the simplest and best way to use Gemini models and agents. It provides server-side state management, observable execution steps, background execution, and unified support for models and agents.
use *;
async
Key features:
- Server-side state β
previous_interaction_idfor multi-turn conversations - Background execution β
.with_background()+ polling - Managed agents β Deep Research, Antigravity
- Observable steps β thoughts, function calls, tool usage as typed steps
- SSE streaming β step lifecycle events (start/delta/stop)
- Structured output β JSON schema via
.with_json_schema()
See the interaction_*.rs examples for complete coverage of every feature.
Legacy generateContent API
The original generateContent API is still available but deprecated. New code should use the Interactions API.
π Migration Guide: generateContent β Interactions API
Paradigm Shift
The Interactions API introduces a fundamentally different request/response model:
generateContent (Legacy) Interactions API (Recommended)
Contents (messages) β Candidates Interaction (input) β Steps (typed actions)
| Aspect | generateContent (Legacy) | Interactions API (Recommended) |
|---|---|---|
| Request | contents: Vec<Content> (role + parts) |
input: string | Content[] | Step[] |
| Response | candidates: Vec<Candidate> (parts) |
steps: Vec<Step> (typed enum) |
| Multi-turn state | Client manages full history | Server-side previous_interaction_id |
| Streaming | SSE chunks of GenerationResponse |
SSE step lifecycle events (step.start/delta/stop) |
| Function calling | Part::FunctionCall / Part::FunctionResponse |
Step::FunctionCall / Step::FunctionResult |
| Background execution | Not supported | background=true + polling or webhook |
| Managed agents | Not supported | Deep Research, Antigravity |
| Sandbox environments | Not supported | environment: "remote" |
| Service tiers | Not supported | flex, standard, priority |
Side-by-Side Code Comparison
Basic Text Generation
// ββ Legacy generateContent ββ
let response = client.generate_content.await?;
println!;
// ββ Interactions API ββ
let interaction = client.create_interaction
.with_model
.with_text
.execute
.await?;
println!;
Multi-Turn Conversation
// ββ Legacy: manually resend entire history ββ
let response2 = client.generate_content.await?;
// ββ Interactions: server-side state via previous_interaction_id ββ
let interaction2 = client.create_interaction
.with_model
.with_text
.with_previous_interaction
.execute
.await?;
Streaming
// ββ Legacy: SSE chunks of GenerationResponse ββ
use *;
let mut stream = client.generate_content_stream.await?;
while let Some = stream.next.await
// ββ Interactions: SSE step lifecycle events ββ
let mut stream = client.create_interaction_stream.await?;
while let Some = stream.next.await
Feature Availability
New in Interactions API (not in generateContent):
- Server-side conversation state (
previous_interaction_id) - Background execution with polling or webhooks
- Managed agents (Deep Research, Antigravity)
- Remote sandbox environments
- Observable execution steps (thoughts, tool calls, code execution)
- Service tiers (
flex,standard,priority) - Webhook callbacks
Only in generateContent (not yet in Interactions API):
- Batch API
- Explicit content caching
- Custom safety settings
- Video metadata (clipping, frame rate)
- Automatic function calling
Type Mapping
| Legacy Type | Interactions Type | Notes |
|---|---|---|
Content / Part |
InteractionContent |
Type-tagged polymorphic |
Candidate |
Step::ModelOutput |
Filter steps for ModelOutput variant |
Part::Text |
InteractionContent::Text |
|
Part::InlineData |
InteractionContent::Image / Audio / Video |
Typed per media |
Part::FunctionCall |
Step::FunctionCall |
Now a top-level step, not a Part |
Part::FunctionResponse |
Step::FunctionResult |
Provide tool results back |
GenerationConfig |
InteractionGenerationConfig |
Shared subset of fields |
Tool |
InteractionTool |
Type-tagged polymorphic |
Migration Checklist
- Start with simple text generation β swap
generate_content()forcreate_interaction().execute() - Replace multi-turn logic β use
with_previous_interaction()instead of rebuilding history - Update response parsing β use
interaction.output_text()instead ofresponse.candidates[0]... - Switch streaming β handle
InteractionEventvariants instead of raw chunks - Map tools β convert
TooltoInteractionTool(function, google_search, code_execution, etc.) - Explore new features β try
with_background(),with_agent(), orwith_environment()
Basic Content Generation
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.
Streaming Responses
Enable real-time content streaming for interactive applications. See basic_streaming.rs for examples of processing content as it's generated with immediate display.
Google Maps Grounding
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.
Token Count API
Calculate token usage before making generation requests for cost estimation and optimization. See count_tokens.rs.
Safety Settings
Customize content moderation with granular control over different harm categories and block thresholds. See safety_settings.rs.
Gemini 3 Pro
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.
π οΈ Key Features
The library provides comprehensive access to all Gemini 2.5 capabilities through an intuitive Rust API:
π§ Thinking Mode (Gemini 2.5)
Advanced reasoning capabilities with thought process visibility and custom thinking budgets. See thinking_basic.rs and thinking_advanced.rs.
π οΈ Function Calling & Tools
- Custom function declarations with OpenAPI schema support (using
schemars) - Google Search integration for real-time information
- Google Maps grounding for location-aware responses
- Type-safe function definitions with automatic schema generation
- See
tools.rs,complex_function.rs, andgoogle_maps_grounding.rs
πΊοΈ Google Maps Grounding
- Location-Aware Responses: Access Google Maps data for geographically specific queries
- Widget Support: Generate context tokens for interactive Google Maps widgets
- Grounding Sources: Access citation information for all Maps data used in responses
- Easy Integration: Simple API with location context configuration
- See
simple_maps_example.rsandadvanced_maps_configuration.rs
π¨ Multimodal Generation
- Image Generation: Nano Banana (Flash) and Pro (Gemini 3) Text-to-image with detailed thinking and follow up editing capabilities
- Speech Generation: Text-to-speech with single and multi-speaker support
- Image Processing: Analyze images, videos, and binary data
- See
image_generation.rsandmulti_speaker_tts.rs
π¦ Batch Processing
Efficient processing of multiple requests with automatic file handling for large jobs. See batch_generate.rs.
πΎ Content Caching
Cache system instructions and conversation history to reduce costs and improve performance. See cache_basic.rs.
π Text Embeddings
Advanced embedding generation with multiple task types for document retrieval and semantic search. See embedding.rs.
π File Search (RAG)
- Semantic Document Search: Upload documents and query them with natural language
- Automatic Chunking: Documents are automatically split, embedded, and indexed
- Custom Metadata: Filter searches using metadata tags (e.g.,
category = "api-docs") - Grounding Citations: Get source references for model responses
- Multiple Upload Methods: Direct upload or import from Files API
- Persistent Storage: Documents persist indefinitely until deleted
- See
file_search_basic.rs,file_search_metadata.rs, andfile_search_import.rs
π Streaming Responses
Real-time streaming of generated content for interactive applications. See streaming.rs.
βοΈ Highly Configurable
- Custom models and endpoints
- Detailed generation parameters (temperature, tokens, etc.)
- HTTP client customization with timeouts and proxies
- See
generation_config.rsandcustom_base_url.rs
π Observability
Built-in structured logging and telemetry with tracing for comprehensive monitoring and debugging.
π’ Token Count API
Pre-calculate token usage for cost estimation and optimization. Calculate tokens for your requests before executing them. See count_tokens.rs.
π― Safety Settings
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 Pro
- Code Execution: Generate and execute Python code for mathematical calculations, data analysis, and computational tasks
- Thinking Levels: Choose Low for faster responses or High for deeper analysis
- Media Resolution: Fine-grained control over image and PDF processing quality
- See
gemini_3_code_execution.rsandgemini_3_thinking_and_media.rs
π File Handles
Efficiently 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.
π§ Configuration
Custom Models
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.
Custom Base URL
Use custom API endpoints and configurations. See custom_base_url.rs for examples of configuring custom endpoints with different models.
Configurable HTTP Client Builder
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.
π Tracing and Telemetry
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:
- HTTP Request Tracing: Captures detailed information about every API call, including HTTP method, URL, and response status, to help diagnose network-related issues
- Token Usage Monitoring: Records the number of prompt, candidate, and total tokens for each generation request, enabling cost analysis and optimization
- Structured Logging: Emits traces as structured events, compatible with modern log aggregation platforms like Elasticsearch, Datadog, and Honeycomb, allowing for powerful querying and visualization
- Performance Metrics: Provides timing information for each API request, allowing you to identify and address performance bottlenecks
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.
π Examples
The repository includes 30+ comprehensive examples demonstrating all features. See examples/README.md for detailed information.
Quick Start Examples
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 analysis
Interactions API Examples
interaction_basic.rs- Basic text generation (simplest starting point)interaction_multi_turn.rs- Multi-turn withprevious_interaction_idinteraction_streaming.rs- SSE step lifecycle eventsinteraction_advanced.rs- Advanced configuration (tools, thinking, system prompt)interaction_function_calling.rs- Function calling withStep::FunctionCallinteraction_google_search.rs- Google Search groundinginteraction_google_maps.rs- Google Maps groundinginteraction_code_execution.rs- Python code executioninteraction_thinking.rs- Thinking levels (low/medium/high)interaction_structured.rs- JSON schema structured outputinteraction_multimodal.rs- Image, audio, and video inputinteraction_image_gen.rs- Image generationinteraction_tts.rs- Text-to-speech outputinteraction_background.rs- Background execution with pollinginteraction_deep_research.rs- Deep Research managed agentinteraction_antigravity.rs- Antigravity agent with sandbox environmentinteraction_url_context.rs- URL context toolinteraction_file_search.rs- RAG file searchinteraction_error_handling.rs- Error handling patternsinteraction_tracing.rs- Tracing and telemetryinteraction_custom_client.rs- Custom HTTP client configuration
Run any example:
GEMINI_API_KEY="your-api-key"
π API Key Setup
Get your API key from Google AI Studio and set it as an environment variable:
π¦ Supported Models
Standard Models (Both APIs)
- Gemini 2.5 Flash - Fast, efficient model (default) -
Model::Gemini25Flash - Gemini 2.5 Flash Lite - Lightweight model -
Model::Gemini25FlashLite - Gemini 2.5 Pro - Advanced model with thinking capabilities -
Model::Gemini25Pro - Gemini 3 Pro - Latest model with code execution and advanced thinking -
Model::Gemini3Pro(Preview) - Gemini 3 Flash - Fast model with thinking levels (Minimal, Low, Medium, High) -
Model::Gemini3Flash(Preview) - Text Embedding 004 - Latest embedding model -
Model::TextEmbedding004 - Custom models - Use
Model::Custom(String)or string literals for other models
Managed Agents (Interactions API only)
- Deep Research - Multi-step research agent with report generation -
AgentConfig::DeepResearchor.with_agent("deepresearch-2.5") - Antigravity - Code-writing agent with remote sandbox -
.with_agent("antigravity-preview-05-2026")
π€ Contributing
Contributions 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.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
- Google for providing the Gemini API
- The Rust community for excellent async and HTTP libraries
- Special thanks to @npatsakula for major contributions that made this project more complete
- All contributors who have helped improve this library