Generate text using the Vertex AI Gemini API

In this guide, you send a text prompt request and a multimodal prompt and image request to the Vertex AI Gemini API and view the responses.

Prerequisites

To complete this guide, you must have a Google Cloud project with the Vertex AI API enabled. Follow the Vertex AI setup guide to complete these steps.

Add the Vertex AI client library as a dependency

The Vertex AI client library includes many features. To speed up compilation, enable only the features that you need:

cargo add google-cloud-aiplatform-v1 --no-default-features --features prediction-service

Send a prompt to the Vertex AI Gemini API

  1. Initialize the client using the default settings:
let client = PredictionService::builder().build().await?;
  1. Build the model name. This example receives the project ID as an argument, uses a fixed location (global), and sets the model to a specific version of Gemini:
const MODEL: &str = "gemini-3.8-flash";
let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");

If you run this function in your own code, pass the project ID (without any projects/ prefix) of the project that you selected in the prerequisites.

  1. Send the request:
let response = client
    .generate_content()
    .set_model(&model)
    .set_contents([Content::new()
        .set_role("user")
        .set_parts([Part::new().set_text(
            "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?",
        )])])
    .send()
    .await;
  1. Print the response. You can use the :#? format specifier to format the nested response objects:
println!("RESPONSE = {response:#?}");

Text prompt: complete code

use google_cloud_aiplatform_v1::client::PredictionService;
use google_cloud_aiplatform_v1::model::{Content, Part};

pub async fn sample(project_id: &str) -> anyhow::Result<()> {
    let client = PredictionService::builder().build().await?;

    const MODEL: &str = "gemini-3.8-flash";
    let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");

    let response = client
        .generate_content()
        .set_model(&model)
        .set_contents([Content::new()
            .set_role("user")
            .set_parts([Part::new().set_text(
                "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?",
            )])])
        .send()
        .await;
    println!("RESPONSE = {response:#?}");

    Ok(())
}

Send a prompt and an image to the Vertex AI Gemini API

  1. Initialize the client using the default settings:
let client = PredictionService::builder().build().await?;
  1. Build the model name:
const MODEL: &str = "gemini-3.8-flash";
let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");
  1. Create the image part of the request:
Part::new().set_file_data(
    FileData::new()
        .set_mime_type("image/jpeg")
        .set_file_uri("gs://generativeai-downloads/images/scones.jpg"),
),
  1. Create the text prompt part of the request:
Part::new().set_text("Describe this picture."),
  1. Send the full request:
let response = client
    .generate_content()
    .set_model(&model)
    .set_contents([Content::new().set_role("user").set_parts([
        Part::new().set_file_data(
            FileData::new()
                .set_mime_type("image/jpeg")
                .set_file_uri("gs://generativeai-downloads/images/scones.jpg"),
        ),
        Part::new().set_text("Describe this picture."),
    ])])
    .send()
    .await;
  1. Print the full response:
println!("RESPONSE = {response:#?}");

Prompt and image: complete code

use google_cloud_aiplatform_v1::client::PredictionService;
use google_cloud_aiplatform_v1::model::{Content, FileData, Part};

pub async fn sample(project_id: &str) -> anyhow::Result<()> {
    let client = PredictionService::builder().build().await?;

    const MODEL: &str = "gemini-3.8-flash";
    let model = format!("projects/{project_id}/locations/global/publishers/google/models/{MODEL}");

    let response = client
        .generate_content()
        .set_model(&model)
        .set_contents([Content::new().set_role("user").set_parts([
            Part::new().set_file_data(
                FileData::new()
                    .set_mime_type("image/jpeg")
                    .set_file_uri("gs://generativeai-downloads/images/scones.jpg"),
            ),
            Part::new().set_text("Describe this picture."),
        ])])
        .send()
        .await;
    println!("RESPONSE = {response:#?}");

    Ok(())
}