adk-gemini
Rust client for the Google Gemini API: content generation, streaming, function calling, built-in tools, thinking, embeddings, image and speech generation, batch processing, caching, files, the Interactions API (Beta), and Vertex AI.
Stability: Stable — follows semantic versioning, so breaking changes wait for the next major release. See STABILITY.md.
Overview
adk-gemini is the Gemini client behind adk_model::GeminiModel and is maintained as part of
ADK-Rust. It covers:
- Content generation (text, images, audio) and streaming
- Function calling and built-in tools: Google Search, URL context, code execution, Google Maps, File Search, and computer use
- Thinking levels and thought signatures for multi-turn function calling
- Structured JSON output
- Embeddings (
gemini-embedding-2,gemini-embedding-001) - Image generation and editing, single- and multi-speaker text-to-speech
- Batch processing, context caching, and the Files API
- Model discovery (
list_models,get_model) - Vertex AI with API keys, Application Default Credentials, service accounts, and Workload
Identity Federation (
vertexfeature) - The Interactions API (Beta): server-side history, a typed step timeline, streaming step events,
managed agents, and environments (
interactionsfeature)
Installation
[]
= "3.0.0"
| Feature | Enables |
|---|---|
vertex |
Vertex AI constructors and VertexBackend |
interactions |
The Interactions API module and client methods |
backtrace |
snafu's backtraces-impl-backtrace-crate feature |
The AI Studio backend has no feature flag and is always compiled; no feature is on by default.
adk-model uses this crate for GeminiModel:
[]
= { = "3.0.0", = ["gemini"] }
Quick Start
use Gemini;
async
Gemini::new uses the default model, gemini-3.8-flash, on the v1beta API.
Client constructors
| Constructor | Backend and model |
|---|---|
Gemini::new(api_key) |
AI Studio, gemini-3.8-flash, v1beta |
Gemini::pro(api_key) |
AI Studio, gemini-3.1-pro-preview |
Gemini::with_model(api_key, model) |
AI Studio, any model |
Gemini::with_v1(api_key), Gemini::with_model_v1(api_key, model) |
AI Studio on the stable v1 API |
Gemini::with_base_url(api_key, url), Gemini::with_model_and_base_url(api_key, model, url) |
A custom endpoint |
Gemini::with_google_cloud(api_key, project, location) |
Vertex AI with an API key (vertex) |
Gemini::with_google_cloud_adc(project, location) |
Vertex AI with Application Default Credentials (vertex) |
Gemini::with_google_cloud_service_account_json(json, project, location, model) |
Vertex AI with a service account key (vertex) |
Gemini::with_service_account_json(json) |
Vertex AI, project read from the service account key (vertex) |
Gemini::with_google_cloud_wif_json(json, project, location, model) |
Vertex AI with Workload Identity Federation (vertex) |
GeminiBuilder::new(api_key) |
Builder: model, HTTP client, base URL, and Google Cloud options |
model accepts a Model variant, a factory such as Model::gemini_3_5_flash_lite(), or a model
ID String; a bare ID gets the models/ prefix.
Examples
Streaming
use Gemini;
use TryStreamExt;
async
Function calling
The parameter type derives schemars::JsonSchema, so the example needs schemars and serde
as dependencies.
use ;
use JsonSchema;
use ;
async
Answer the call with with_function_response(name, value) on the next request.
Google Search grounding
use ;
async
GroundingMetadata::search_entry_point carries the search suggestions to display with grounded
answers, and GroundingSegment::part_index the part a segment belongs to (None for the first
part). Tool::url_context(), Tool::code_execution(), Tool::google_maps(config),
Tool::file_search(config), and Tool::computer_use(config) declare the other built-in tools.
Thinking
use ;
async
ThinkingLevel has Minimal, Low, Medium, High, and XHigh; the Gemini 4 models,
Gemini 3.8 Flash and Gemini 3.1 Pro do not support Minimal, and only gemini-4-neon and
gemini-4-hydrogen support XHigh. with_thinking_budget(tokens) is the budget-based control of
Gemini 2.5 models, and a config that sets both a budget and a level is rejected. For
gemini-3.6-flash and gemini-3.8-flash, a request that sets temperature, top_p, top_k,
candidate_count, or a thinking budget fails with ClientError::InvalidGenerationConfig before
it is sent. For the Gemini 4 models (gemini-4-carbon, gemini-4-argon, gemini-4-neon,
gemini-4-hydrogen), sampling parameters, candidate_count, Minimal, and XHigh on Carbon or
Argon fail the same way; a thinking budget maps to the nearest level.
Thought signatures
With thinking and function calling, Gemini returns a thoughtSignature with each function call.
Send it back with the call in the next request to keep the model's reasoning context.
use ;
use json;
async
thoughtSignature is serialized both on the part and inside the FunctionCall object, because
Gemini 3 models require it in both places for multi-turn function calling. It is omitted when
None.
Structured JSON output
use Gemini;
use json;
async
Embeddings
use ;
async
Image generation
use ;
async
Text-to-speech
use Gemini;
async
with_multi_speaker_config(vec![SpeakerVoiceConfig::new(speaker, voice), ...]) assigns a voice
to each speaker.
Context caching
use Gemini;
use Duration;
async
Batch processing
use Gemini;
async
Multi-turn conversation
use Gemini;
async
Interactions API (Beta)
Needs the interactions feature. The server keeps the history; continue a conversation with
previous_interaction_id.
use ;
async
stream() returns the step events, and get_interaction, delete_interaction, and
cancel_interaction manage stored interactions. A step can carry content on its step.start:
a function call's name and id arrive there and its arguments as StepDelta::ArgumentsDelta
fragments. InteractionStatus::RequiresContinuation marks a long-decode hop; pass the
interaction's continuation_token back with continuation_token() to continue it. antigravity()
targets the antigravity-preview-09-2026 agent. The stream fails when it ends before
interaction.completed or reports a failed or budget_exceeded status. The
interactions_basic example covers streaming and multi-turn
continuation, and the gemini_managed_agents example in
adk-playground covers managed agents and environments.
Vertex AI
Needs the vertex feature.
use Gemini;
async
A location of "global" resolves to https://aiplatform.googleapis.com; any other location
uses https://{location}-aiplatform.googleapis.com.
Backends
| Backend | Transport | Auth | Used by |
|---|---|---|---|
StudioBackend |
REST and SSE | API key | Gemini::new, Gemini::with_model, and the other AI Studio constructors |
VertexBackend |
gRPC for non-streaming requests, REST SSE for streaming | ADC, service account, WIF, or API key | The with_google_cloud* and service-account constructors |
The GeminiBackend trait defines generate_content, generate_content_stream, and the optional
operations. VertexBackend supports generation, streaming, embeddings, and cached content
(create, get, update, list, delete). The Files API, batch operations, and the Interactions API
are AI Studio only and return ClientError::GoogleCloudUnsupported on Vertex AI. A failed model
request is never replayed through another transport.
Errors
ClientError reports an error object inside a successful generateContent body or
streamGenerateContent event as ErrorInResponse, with its code, status, and message,
instead of decoding it as an empty response. Through adk_model::GeminiModel, a stream that ends
before any chunk carries finishReason fails with model.gemini.unfinished_stream, and a blocked
prompt (promptFeedback.blockReason) ends the turn with FinishReason::Safety.
FinishReason is #[non_exhaustive], so a match on it needs a wildcard arm; a value this
version does not know deserializes as FinishReason::Other.
API modules
| Module | Contents |
|---|---|
generation |
Content generation requests and responses |
embedding |
Embedding requests |
batch |
Batch processing |
files |
File upload and management |
cache |
Context caching |
safety |
Safety settings and ratings |
tools |
Function declarations and built-in tools |
backend |
GeminiBackend, StudioBackend, VertexBackend |
model_info |
Model discovery |
pricing |
Token cost estimation |
schema_adapter |
Function-declaration schema normalization (GeminiSchemaAdapter) |
interactions |
Interactions API (Beta), interactions feature |
prelude |
Re-exports of commonly used types |
Environment variables
The examples read GEMINI_API_KEY. For Vertex AI, Application Default Credentials follow the
Google Cloud conventions, such as GOOGLE_APPLICATION_CREDENTIALS.
Running examples
Related crates
- adk-rust — umbrella crate
- adk-model — multi-provider
Llmclients, includingGeminiModelover this crate - adk-core — the
Llmtrait - adk-agent — agent implementations
License
MIT AND Apache-2.0 — both licences apply to the crate:
| Licence | File | Covers |
|---|---|---|
| MIT | LICENSE-MIT |
The code forked from gemini-rust, under its original notice (Copyright (c) 2025 Michael Yang) |
| Apache-2.0 | LICENSE-APACHE |
Changes and additions made in ADK-Rust, licensed like the rest of the workspace |
The fork keeps the MIT notice because the MIT licence requires it in every copy; ADK-Rust's own work is Apache-2.0. Redistributing the crate means keeping both files.
Original work Copyright (c) 2025 Michael Yang, as stated in LICENSE-MIT
Modifications Copyright (c) 2024 Zavora AI
Part of ADK-Rust
This crate is part of the ADK-Rust framework for building AI agents in Rust.
Attribution
This crate is a fork of the gemini-rust library by @flachesis, and keeps its MIT notice (see License).
| Upstream | |
|---|---|
| Repository | github.com/flachesis/gemini-rust |
| Crates.io | crates.io/crates/gemini-rust |
| Original author | @flachesis |
ADK-Rust forked the client to export additional types (such as GroundingMetadata and
GroundingChunk) for grounding support, to add ADK-specific extensions, and to version it with
the workspace.
Acknowledgments
- @flachesis — creator and maintainer of the original gemini-rust library
- @npatsakula — major contributions to the upstream project