genai-rs
A Rust client library for Google's Generative AI (Gemini) API using the Interactions API.
Quick Start
use Client;
async
Features
Core Capabilities
| Feature | Description |
|---|---|
| Streaming | Real-time token streaming with resume capability |
| Stateful Conversations | Multi-turn context via previous_interaction_id |
| Function Calling | Auto-discovery with #[tool] macro or manual control |
| Structured Output | JSON schema enforcement with with_response_format() |
| Thinking Mode | Access model reasoning with configurable depth |
Built-in Tools
| Tool | Method | Use Case |
|---|---|---|
| Google Search | with_google_search() |
Real-time web grounding |
| Code Execution | with_code_execution() |
Python sandbox |
| URL Context | with_url_context() |
Web page analysis |
Multimodal I/O
| Input | Output |
|---|---|
| Images, Audio, Video, PDFs | Text, Images, Audio (TTS) |
Installation
[]
= "0.7"
= { = "1.0", = ["full"] }
# Optional
= "0.7" # For #[tool] macro
= "0.3" # For streaming
Requirements: Rust 1.88+ (edition 2024), Gemini API key
Examples
Runnable examples covering all features:
Quick Reference:
| I want to... | Example |
|---|---|
| Make my first API call | simple_interaction |
| Stream responses | streaming |
| Use function calling | auto_function_calling |
| Multi-turn conversations | stateful_interaction |
| Generate images | image_generation |
| Text to speech | text_to_speech |
| Get structured JSON | structured_output |
| Implement retry logic | retry_with_backoff |
See Examples Index for the complete categorized list.
Usage Highlights
Streaming
use StreamExt;
use StreamChunk;
let mut stream = client.interaction
.with_text
.create_stream;
while let Some = stream.next.await
Function Calling with #[tool]
use tool;
let result = client.interaction
.with_text
.add_function
.create_with_auto_functions
.await?;
Stateful Conversations
// First turn (enable storage for multi-turn)
let r1 = client.interaction
.with_system_instruction
.with_text
.with_store_enabled
.create.await?;
// Continue conversation (r1.id is Option<String>)
let r2 = client.interaction
.with_previous_interaction
.with_text // Remembers: Alice
.create.await?;
Thinking Mode
use ThinkingLevel;
let response = client.interaction
.with_thinking_level
.with_text
.create.await?;
// Check if model used reasoning (thoughts contain cryptographic signatures)
if response.has_thoughts
Build & Execute (for Retries)
use InteractionRequest;
// Build request without executing (Clone + Serialize)
let request: InteractionRequest = client.interaction
.with_model
.with_text
.build?;
// Execute separately - enables retry loops
let response = client.execute.await?;
// On error, check if retryable: error.is_retryable()
See retry_with_backoff for a complete retry example using the backon crate.
Documentation
Guides
| Guide | Description |
|---|---|
| Examples Index | All examples, categorized |
| Function Calling | #[tool] macro, ToolService, manual execution |
| Multi-Turn Patterns | Stateful/stateless, inheritance rules |
| Streaming API | Stream types, resume, auto-functions |
| Multimodal | Images, audio, video, PDFs |
| Output Modalities | Image generation, text-to-speech |
| Thinking Mode | Reasoning depth, thought signatures |
| Built-in Tools | Google Search, code execution, URL context |
| Configuration | Client options, generation config |
| Conversation Patterns | Multi-turn, context management |
Reference
| Document | Description |
|---|---|
| Error Handling | Error types, recovery patterns |
| Reliability Patterns | Retries, timeouts, resilience |
| Logging Strategy | Log levels, LOUD_WIRE debugging |
| Testing Guide | Test strategies, assertions |
| Agents & Background | Long-running tasks, polling |
| API Reference | Generated API documentation |
External Resources
| Resource | Description |
|---|---|
| Interactions API Reference | Official API specification |
| Interactions API Guide | Usage patterns |
| Function Calling Guide | Google's function calling docs |
Debugging
# Wire-level request/response logging
LOUD_WIRE=1
# Library debug logs
RUST_LOG=genai_rs=debug
See Logging Strategy for details.
Forward Compatibility
This library follows the Evergreen philosophy: unknown API types deserialize into Unknown variants instead of failing. Always include wildcard arms:
match content
Testing
Project Structure
genai-rs/ # Main crate: Client, InteractionBuilder, types
genai-rs-macros/ # Procedural macro for #[tool]
docs/ # Comprehensive guides
examples/ # Runnable examples
Contributing
Contributions welcome! Please read:
- CLAUDE.md - Development guidelines and architecture
- CHANGELOG.md - Version history and migration guides
- SECURITY.md - Security policy and reporting
Troubleshooting
Common issues and solutions are documented in TROUBLESHOOTING.md.
Quick fixes:
- "API key not valid" - Check
GEMINI_API_KEYis set - "Model not found" - Use
gemini-3-flash-preview - Functions not executing - Use
create_with_auto_functions()