Skip to main content

Crate gemini_genai_rs

Crate gemini_genai_rs 

Source
Expand description

§gemini-genai-rs

The wire layer for Google’s Gemini Live API in Rust: a WebSocket session with typed events, the setup/realtime message vocabulary, Google AI and Vertex AI authentication, and the audio primitives a realtime client needs. It is the L0 crate of the gemini-rs workspace, with no agent abstractions. Applications usually want the L2 crate, gemini-adk-fluent-rs; this crate is for anyone who needs the protocol itself.

§Quick start

use gemini_genai_rs::prelude::*;

#[tokio::main]
async fn main() -> Result<(), SessionError> {
    // Unset model → the platform's current native-audio Live model
    // (`GEMINI_LIVE_MODEL` overrides). Output transcription makes the answer readable.
    let config = SessionConfig::new(std::env::var("GEMINI_API_KEY").unwrap())
        .output_transcription(true);
    let session = connect(config).await?;

    let mut events = session.subscribe();
    session.send_text("What is the speed of light?").await?;
    while let Some(event) = recv_event(&mut events).await {
        match event {
            SessionEvent::OutputTranscription(text) => print!("{text}"),
            SessionEvent::TurnComplete => break,
            SessionEvent::Error(e) => eprintln!("{e}"),
            _ => {}
        }
    }
    session.disconnect().await
}

Vertex AI is the same session with a different endpoint. Tokens live about an hour, so give a long-lived session a refreshing source:

use gemini_genai_rs::prelude::*;

let config = SessionConfig::from_endpoint(ApiEndpoint::vertex_refreshing(
    "my-project",
    "us-central1",
    fetch_token,
));

Timeouts, reconnection policy, a custom transport or codec, and wire recording go through ConnectBuilder; connect(config) is the same path with none of the options.

§What is in the box

  • Protocol types mapping one-to-one to the Live API wire format, with builders for the parts you write (Content::user(..), Part::text(..), Tool::function(..)).
  • A session (SessionHandle): send_audio/send_text/send_video/ tool responses in, a broadcast of SessionEvent out, reconnection with backoff and session-resumption handles, GoAway as a typed event.
  • Authentication for Google AI (API key or OAuth token) and Vertex AI (bearer token, static or refreshing), and platform differences handled on the wire (Vertex strips async-tool and thinking fields it does not accept).
  • Audio primitives: a lock-free SPSC ring, an adaptive jitter buffer, client-side voice activity detection, barge-in and turn detection.
  • REST surfaces behind feature flags: generateContent, embeddings, token counting, files, caches, tunings, batches, chats.

§Feature flags

The default build is the Live protocol plus a TLS backend. Everything heavier is opt-in.

FeatureEnablesDefault
liveLive WebSocket session types and transportyes
tls-nativeTLS via the platform’s native library (enable exactly one TLS backend)yes
tls-rustlsTLS via rustls with native root certificatesno
vadEnergy-based client-side voice activity detectionno
vad-wavekatVAD backed by the wavekat-vad model (implies vad)no
httpHTTP client (reqwest), required by every REST featureno
generate, embed, tokens, models, files, caches, tunings, batchesThe corresponding REST endpoint (each implies http)no
chatsMulti-turn chat sessions over generateno
all-apisEvery REST feature aboveno
tracing-subscriberThe fmt/EnvFilter subscriber behind TelemetryConfig::initno
metricsPrometheus metrics exporterno
otel-otlp / otel-gcpOpenTelemetry export over OTLP, or to Google Cloud Trace and Monitoringno

The tracing facade itself is always compiled; spans are no-ops until a subscriber is installed.

[dependencies]
gemini-genai-rs = { version = "2", features = ["generate", "tokens"] }

§Voice activity detection

VoiceActivityDetector (feature vad) runs client-side, alongside or instead of the server’s detection: an adaptive noise floor over incoming PCM frames that emits speech start and end events. vad-wavekat swaps in the wavekat-vad model for tighter speech boundaries. The L1 runtime uses it for soft-turn detection and client-authority interruption.

§Documentation

API reference on docs.rs · the gemini-rs book for the full stack.

§License

MIT

Re-exports§

pub use client::Client;
pub use transport::ConnectBuilder;
pub use transport::connect;

Modules§

buffer
Lock-free audio buffers for the hot path.
client
Unified Gemini API client — wraps both Live (WebSocket) and REST API access.
prelude
Convenient re-exports for wire-level usage.
primitives
The L0 contract — frames on a wire
protocol
Wire-format types mapping one-to-one to the Gemini Multimodal Live API.
session
Session orchestration — the central coordination layer.
telemetry
Observability layer — OpenTelemetry tracing, structured logging, Prometheus metrics.
transport
WebSocket transport layer — connection, full-duplex messaging, flow control.
turn
Turn-taking — barge-in handling and turn detection (VAD-driven).