DEV Community

Victor Ayo
Victor Ayo

Posted on

Announcing `google-ai-rs`: Type-Safe Google Generative AI Client for Rust

Hi Rustaceans! I'm excited to share my new crate for working with Google's AI APIs: google-ai-rs

Why this exists:

  • Provides strict type safety for Google's Gemini API responses
  • Handles JSON schema validation at compile time
  • Supports both gRPC and REST with async/await
  • Implements full API surface (chat, embeddings, model management)

Key Features:

// Schema validation example

#[derive(AsSchema)]
#[schema(description = "Validated customer purchase order")]
#[schema(rename_all = "camelCase")]
pub struct Order {
    #[schema(
        description = "Order items with quantity and pricing"
    )]
    pub items: Vec<OrderItem>,

    #[schema(
        description = "Customer contact information"
    )]
    pub customer: Customer,

    #[schema(description = "Total amount in USD")]
    pub total: f64,

    pub status: OrderStatus
}

#[derive(AsSchema)]
#[schema(rename_all = "camelCase")]
enum OrderStatus {
    Pending,
    Processing,
    Shipped,
    Canceled,
}

let result = model.as_schema::<Vec<Order>>()
        .generate_content("Return top 5 orders").await?;
Enter fullscreen mode Exit fullscreen mode

Flexible Input Handling:

// Mix text and binary inputs
model.generate_content((
    "Explain this diagram",
    Part::blob("image/png", diagram_bytes),
    "Include technical specs"
)).await?;
Enter fullscreen mode Exit fullscreen mode

Production Ready:

  • Connection pooling via tonic/hyper
  • Configurable retry policies
  • Proper error handling variants
  • Benchmark: 2-3x faster than pure REST implementations

Getting Started:

[dependencies]
google-ai-rs = "0.1.0"
Enter fullscreen mode Exit fullscreen mode
use google_ai_rs::{Client, generative::GenerativeModel};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new(env!("API_KEY").into())?;
    let response = client.generative_model("gemini-pro")
        .generate_content("Explain borrow checker using cooking analogies")
        .await?;

    println!("{}", response.text());
    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

Links:

Would love feedback from anyone working with AI APIs in Rust! Particularly interested in:

  • Schema derivation patterns
  • Error handling ergonomics
  • gRPC vs REST tradeoffs

Top comments (0)