Crate genai_rs

Crate genai_rs 

Source
Expand description

§genai-rs

A Rust client library for Google’s Generative AI (Gemini) API using the Interactions API.

§Quick Start

use genai_rs::Client;

#[tokio::main]
async fn main() -> Result<(), genai_rs::GenaiError> {
    let client = Client::new(
        std::env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY not set")
    );

    let response = client
        .interaction()
        .with_model("gemini-3-flash-preview")
        .with_text("Hello, Gemini!")
        .create()
        .await?;

    println!("{}", response.as_text().unwrap_or("No response"));
    Ok(())
}

§Features

  • Fluent Builder API: Chain methods for readable request construction
  • Streaming: Real-time response streaming with create_stream()
  • Function Calling: Automatic function discovery and execution via macros
  • Built-in Tools: Google Search, Code Execution, URL Context
  • Multimodal: Images, audio, video, and document inputs
  • Thinking Mode: Access model reasoning with configurable levels

§API Stability & Forward Compatibility

This library is designed for forward compatibility with evolving APIs:

  • #[non_exhaustive] enums: Match statements require wildcard arms (_ => ...)
  • Unknown variants: Unrecognized API types are captured, not rejected
  • Graceful degradation: New API features won’t break existing code

When Google adds new features, your code continues to work. Unknown content types and tools are preserved for inspection via helper methods like has_unknown().

§Module Organization

Re-exports§

pub use errors::GenaiError;
pub use content::Annotation;
pub use content::CodeExecutionLanguage;
pub use content::Content;
pub use content::FileSearchResultItem;
pub use content::GoogleSearchResultItem;
pub use content::Resolution;
pub use content::UrlContextResultItem;
pub use request::AgentConfig;
pub use request::DeepResearchConfig;
pub use request::DynamicConfig;
pub use request::GenerationConfig;
pub use request::InteractionInput;
pub use request::InteractionRequest;
pub use request::Role;
pub use request::SpeechConfig;
pub use request::ThinkingLevel;
pub use request::ThinkingSummaries;
pub use request::Turn;
pub use request::TurnContent;
pub use response::AudioInfo;
pub use response::CodeExecutionCallInfo;
pub use response::CodeExecutionResultInfo;
pub use response::ContentSummary;
pub use response::FunctionCallInfo;
pub use response::FunctionResultInfo;
pub use response::GroundingChunk;
pub use response::GroundingMetadata;
pub use response::ImageInfo;
pub use response::InteractionResponse;
pub use response::InteractionStatus;
pub use response::ModalityTokens;
pub use response::OwnedFunctionCallInfo;
pub use response::UrlContextMetadata;
pub use response::UrlContextResultInfo;
pub use response::UrlMetadataEntry;
pub use response::UrlRetrievalStatus;
pub use response::UsageMetadata;
pub use response::WebSource;
pub use tools::FunctionCallingMode;
pub use tools::FunctionDeclaration;
pub use tools::FunctionDeclarationBuilder;
pub use tools::FunctionParameters;
pub use tools::Tool;
pub use wire_streaming::InteractionStreamEvent;
pub use wire_streaming::StreamChunk;
pub use wire_streaming::StreamEvent;
pub use client::Client;
pub use client::ClientBuilder;
pub use request_builder::ConversationBuilder;
pub use request_builder::InteractionBuilder;
pub use function_calling::CallableFunction;
pub use function_calling::FunctionError;
pub use function_calling::ToolService;
pub use streaming::AutoFunctionResult;
pub use streaming::AutoFunctionResultAccumulator;
pub use streaming::AutoFunctionStreamChunk;
pub use streaming::AutoFunctionStreamEvent;
pub use streaming::FunctionExecutionResult;
pub use streaming::PendingFunctionCall;
pub use multimodal::audio_from_file;
pub use multimodal::audio_from_file_with_mime;
pub use multimodal::detect_mime_type;
pub use multimodal::document_from_file;
pub use multimodal::document_from_file_with_mime;
pub use multimodal::image_from_file;
pub use multimodal::image_from_file_with_mime;
pub use multimodal::video_from_file;
pub use multimodal::video_from_file_with_mime;

Modules§

client
content
Core content types for the Interactions API.
errors
function_calling
interactions_api
multimodal
Multimodal content utilities for file loading and MIME type detection.
request
Request types for creating interactions.
request_builder
response
Response types for the Interactions API.
streaming
Streaming types for automatic function calling.
tools
wire_streaming
Streaming types for SSE responses.

Structs§

FileError
Error information for failed file operations.
FileMetadata
Represents an uploaded file in the Files API.
ListFilesResponse
Response from listing files.
ResumableUpload
Handle to a resumable upload session.
VideoMetadata
Metadata for video files.

Enums§

FileState
Processing state of an uploaded file.

Constants§

DEFAULT_CHUNK_SIZE
Default chunk size for chunked uploads (8 MB).