André Cipriani Bandarra a8fbe658bb Add consistent error handling to text_embeddings and count_tokens
- Check HTTP status before parsing response body, matching the
  pattern used by generate_content and predict_image
- Unwrap TextEmbeddingResponse enum, returning TextEmbeddingResponseOk
- Extract CountTokensResponseResult struct and add into_result(),
  returning the unwrapped result instead of the raw enum
- All endpoints now consistently return the success type directly
  and surface API errors as GeminiError or GenericApiError
2026-01-30 20:32:40 +00:00
2026-01-30 19:01:00 +00:00
2026-01-29 20:14:49 +00:00
2026-01-29 20:14:49 +00:00
2026-01-30 18:59:27 +00:00

google-genai

A Rust client library for the Google Generative AI API.

Building

To build the library, run the following command:

cargo build --release

Running Examples

The project includes several examples in the examples directory. To run an example, you'll first need to set up your environment with your Gemini API key.

  1. Create a .env file in the root of the project:

    GEMINI_API_KEY=your_api_key_here
    
  2. Run an example using cargo run:

    cargo run --example generate-content
    

Usage

To use this library in your own Rust application, add it as a dependency in your Cargo.toml file:

[dependencies]
google-genai = "0.1.0" # Or the latest version

Here is a basic example of how to use the GeminiClient to generate content:

use std::env;
use google_genai::prelude::{Content, GeminiClient, GenerateContentRequest, Role};

async fn run() -> Result<(), Box<dyn std::error::Error>> {
    // Get the API key from the environment.
    let api_key = env::var("GEMINI_API_KEY")?;

    // Create a new GeminiClient.
    let gemini_client = GeminiClient::new(api_key);

    // Build a prompt with a single user message.
    let prompt = vec![
        Content::builder()
            .role(Role::User)
            .add_text_part("What is the airspeed of an unladen swallow?")
            .build(),
    ];

    // Create the content generation request.
    let request = GenerateContentRequest::builder().contents(prompt).build();

    // Call the API to generate content.
    let response = gemini_client
        .generate_content(&request, "gemini-3-pro-preview")
        .await?;

    // Print the response.
    println!("Response: {:?}", response.candidates[0].get_text().unwrap());
    
    Ok(())
}
Description
No description provided
Readme 268 KiB
Languages
Rust 100%