### Get, List, and Update Model Metadata Source: https://context7.com/googleapis/java-genai/llms.txt Retrieve metadata for a specific model, list available models with pagination, and update properties like display name and description for tuned models. Use 'get' for specific model details, 'list' for discovery, and 'update' for configuration changes. ```java import com.google.genai.Client; import com.google.genai.types.*; Client client = new Client(); // Get a specific model's metadata Model model = client.models.get("gemini-2.5-flash", null); System.out.println("Model: " + model.name().get()); // Update a tuned model's display name / description if (model.name().get().startsWith("tunedModels")) { Model updated = client.models.update( "tunedModels/my-tuned-model", UpdateModelConfig.builder() .displayName("My Production Model") .description("Fine-tuned on customer support data") .build()); System.out.println("Updated: " + updated); } // List all base models (paginated) for (Model m : client.models.list(ListModelsConfig.builder().pageSize(10).build())) { System.out.println(m.name().get()); } ``` -------------------------------- ### Create Model Interaction with Google Gen AI Java SDK Source: https://github.com/googleapis/java-genai/blob/main/README.md Demonstrates creating a model interaction using the experimental `interactions` service. This example shows how to set up parameters, send a request, and process the response, including printing text outputs. Note that the `interactions` service is experimental. ```java import com.google.genai.Client; import com.google.genai.interactions.models.interactions.Content; import com.google.genai.interactions.models.interactions.CreateModelInteractionParams; import com.google.genai.interactions.models.interactions.Interaction; import com.google.genai.interactions.models.interactions.Model; Client client = new Client(); CreateModelInteractionParams params = CreateModelInteractionParams.builder() .input("Why is the sky blue?") .model(Model.GEMINI_2_5_FLASH) .build(); Interaction interaction = client.interactions.create(params); System.out.println("Interaction ID: " + interaction.id()); System.out.println("Status: " + interaction.status()); // Print the text outputs from the interaction. interaction.outputs().ifPresent(outputs -> { for (Content output : outputs) { output.text().ifPresent(text -> System.out.println("Output: " + text.text())); } }); ``` -------------------------------- ### Fine-tune a Model and Use the Tuned Model Source: https://context7.com/googleapis/java-genai/llms.txt Initiates a supervised fine-tuning (SFT) job using a dataset from GCS and retrieves the ID of the newly tuned model. The example then demonstrates how to use the tuned model for content generation. Requires enterprise configuration. ```java import com.google.genai.Client; import com.google.genai.types.*; Client client = Client.builder().project("my-proj").location("us-central1").enterprise(true).build(); TuningDataset dataset = TuningDataset.builder() .gcsUri("gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl") .build(); TuningJob job = client.tunings.tune("gemini-2.5-flash", dataset, null); System.out.println("Created tuning job: " + job.name().get()); // Poll until the tuned model is ready String tunedModel = ""; while (tunedModel.isEmpty()) { Thread.sleep(10_000); TuningJob current = client.tunings.get(job.name().get(), null); if (current.tunedModel().isPresent() && current.tunedModel().get().model().isPresent()) { tunedModel = current.tunedModel().get().model().get(); } } System.out.println("Tuned model ID: " + tunedModel); // Use the tuned model GenerateContentResponse response = client.models.generateContent(tunedModel, "Hello!", null); System.out.println(response.text()); ``` -------------------------------- ### client.interactions.create — Interactions API (Experimental) Source: https://context7.com/googleapis/java-genai/llms.txt Provides a high-level, experimental API for creating and retrieving model interactions with structured outputs. The example demonstrates creating a basic interaction and retrieving an existing one by its ID. ```APIDOC ## `client.interactions.create` — Interactions API (Experimental) A high-level experimental API for creating and retrieving model interactions with structured outputs. ```java import com.google.genai.Client; import com.google.genai.interactions.models.interactions.*; Client client = new Client(); // Create a basic interaction CreateModelInteractionParams params = CreateModelInteractionParams.builder() .input("Why is the sky blue?") .model(Model.GEMINI_2_5_FLASH) .build(); Interaction interaction = client.interactions.create(params); System.out.println("ID: " + interaction.id()); System.out.println("Status: " + interaction.status()); interaction.outputs().ifPresent(outputs -> { for (Content output : outputs) { output.text().ifPresent(t -> System.out.println("Output: " + t.text())); } }); // Retrieve an existing interaction by ID Interaction fetched = client.interactions.get(interaction.id()); System.out.println("Fetched status: " + fetched.status()); ``` ``` -------------------------------- ### client.fileSearchStores — File Search Stores (Gemini API) Source: https://context7.com/googleapis/java-genai/llms.txt Enables the creation of document stores for semantic file search, integrated with the Files API. The example shows how to create a store, upload files, import them into the store, and manage documents within the store. ```APIDOC ## `client.fileSearchStores` — File Search Stores (Gemini API) Create document stores that enable semantic file search, backed by the Files API. ```java import com.google.genai.Client; import com.google.genai.errors.GenAiIOException; import com.google.genai.types.*; Client client = new Client(); // Gemini API only try { // Create a file search store FileSearchStore store = client.fileSearchStores.create(null); System.out.println("Store: " + store.name().get()); // Upload a file to the Files API, then import it into the store File file = client.files.upload("data.txt", UploadFileConfig.builder().mimeType("text/plain").build()); ImportFileOperation importOp = client.fileSearchStores.importFile(store.name().get(), file.name().get(), null); while (importOp.done().filter(Boolean::booleanValue).isEmpty()) { Thread.sleep(5_000); importOp = client.operations.get(importOp, null); } // Directly upload a file to the store UploadToFileSearchStoreOperation uploadOp = client.fileSearchStores.uploadToFileSearchStore(store.name().get(), "data.txt", null); while (uploadOp.done().filter(Boolean::booleanValue).isEmpty()) { Thread.sleep(5_000); uploadOp = client.operations.get(uploadOp, null); } String docName = uploadOp.response().get().documentName().get(); // List documents in the store for (Document doc : client.fileSearchStores.documents.list(store.name().get(), null)) { System.out.println("Document: " + doc.name().get()); } // Clean up client.fileSearchStores.documents.delete(docName, null); client.fileSearchStores.delete(store.name().get(), null); } catch (GenAiIOException e) { System.err.println("Error: " + e.getMessage()); } ``` ``` -------------------------------- ### Create, Get, Update, List, and Delete Cached Content Source: https://context7.com/googleapis/java-genai/llms.txt Demonstrates managing cached content for frequently used large documents. Use this to reduce latency and cost on repeated calls with identical content. Caches expire after a set time. ```java import com.google.genai.Client; import com.google.genai.types.*; import java.time.Duration; import java.time.Instant; Client client = new Client(); // Create cached content from a PDF Content pdfContent = Content.fromParts(Part.fromBytes(pdfBytes, "application/pdf")); CreateCachedContentConfig cacheConfig = CreateCachedContentConfig.builder() .systemInstruction(Content.fromParts(Part.fromText("Summarize the document."))) .expireTime(Instant.now().plus(Duration.ofHours(1))) .contents(pdfContent) .build(); CachedContent cache = client.caches.create("gemini-2.5-flash", cacheConfig); System.out.println("Cache name: " + cache.name().get()); // Get CachedContent fetched = client.caches.get(cache.name().get(), null); // Update TTL CachedContent updated = client.caches.update( cache.name().get(), UpdateCachedContentConfig.builder().ttl(Duration.ofMinutes(10)).build()); // List for (CachedContent c : client.caches.list(ListCachedContentsConfig.builder().pageSize(5).build())) { System.out.println(c.name().get()); } // Delete client.caches.delete(cache.name().get(), null); ``` -------------------------------- ### Instantiate Client Using Environment Variables Source: https://github.com/googleapis/java-genai/blob/main/README.md Instantiate a client without arguments after configuring the necessary environment variables (GOOGLE_API_KEY or GOOGLE_GENAI_USE_ENTERPRISE, GOOGLE_CLOUD_PROJECT, GOOGLE_CLOUD_LOCATION). ```java import com.google.genai.Client; Client client = new Client(); ``` -------------------------------- ### Generate Embeddings for Text and Multimodal Content Source: https://context7.com/googleapis/java-genai/llms.txt Generate embedding vectors for text or multimodal content. The multimodal example requires Vertex AI configuration. ```java import com.google.genai.Client; import com.google.genai.types.*; import java.util.Arrays; Client client = new Client(); // Text embedding (Gemini API) EmbedContentResponse textEmbed = client.models.embedContent("gemini-embedding-001", "Why is the sky blue?", null); System.out.println("Embedding: " + textEmbed); ``` ```java import com.google.genai.Client; import com.google.genai.types.*; import java.util.Arrays; // Multimodal embedding (Vertex AI only) Client vertexClient = Client.builder().project("my-proj").location("us-central1").enterprise(true).build(); Content multiContent = Content.builder() .parts(Arrays.asList( Part.builder().text("What is in this image?").build(), Part.builder().fileData(FileData.builder() .fileUri("gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png") .mimeType("image/png") .build()).build())) .build(); EmbedContentResponse mmEmbed = vertexClient.models.embedContent("multimodalembedding@001", multiContent, null); System.out.println("Multimodal embedding: " + mmEmbed); ``` -------------------------------- ### Client Initialization Source: https://context7.com/googleapis/java-genai/llms.txt Demonstrates various ways to initialize the `Client` class for connecting to Google's generative AI services, supporting both API key and Vertex AI authentication methods. ```APIDOC ## Client Initialization Construct a `Client` from environment variables, or explicitly via the builder. ```java import com.google.genai.Client; import com.google.genai.types.HttpOptions; import com.google.genai.types.ClientOptions; // --- Option 1: Gemini Developer API via env var GOOGLE_API_KEY --- Client client = new Client(); // --- Option 2: Explicit API key --- Client client = Client.builder() .apiKey("YOUR_API_KEY") .build(); // --- Option 3: Vertex AI / Gemini Enterprise Agent Platform via ADC --- // export GOOGLE_CLOUD_PROJECT=my-project // export GOOGLE_CLOUD_LOCATION=us-central1 // export GOOGLE_GENAI_USE_VERTEXAI=true Client client = Client.builder() .project("my-project") .location("us-central1") .enterprise(true) .build(); // --- Option 4: Custom HTTP options (API version, base URL, timeout, headers) --- Client client = Client.builder() .apiKey("YOUR_API_KEY") .httpOptions(HttpOptions.builder() .apiVersion("v1alpha") // default: beta .timeout(600) // seconds .build()) .build(); // --- Option 5: Connection pool & proxy --- Client client = Client.builder() .apiKey("YOUR_API_KEY") .clientOptions(ClientOptions.builder() .maxConnections(64) .maxConnectionsPerHost(16) .build()) .build(); // Always close when done to release HTTP connections client.close(); // Check backend in use System.out.println(client.vertexAI()); // true = Vertex AI ``` ``` -------------------------------- ### Instantiate Client for Gemini Enterprise Agent Platform (Project and Location) Source: https://github.com/googleapis/java-genai/blob/main/README.md Use the Client.builder(), set the project and location, and enable enterprise mode to instantiate a client for the Gemini Enterprise Agent Platform backend. ```java import com.google.genai.Client; // Use Builder class for instantiation. Explicitly set the project and location, // and set `enterprise(true)` to use Gemini Enterprise Agent Platform backend. Client client = Client.builder() .project("your-project") .location("your-location") .enterprise(true) .build(); ``` -------------------------------- ### Model Management Source: https://context7.com/googleapis/java-genai/llms.txt Manage models by retrieving metadata, listing available models, and updating properties of tuned models. Supports getting, listing, and updating model information. ```APIDOC ## `client.models.get` / `client.models.list` / `client.models.update` — Model Management Retrieve model metadata, list available models, and update tuned model properties. ```java import com.google.genai.Client; import com.google.genai.types.*; Client client = new Client(); // Get a specific model's metadata Model model = client.models.get("gemini-2.5-flash", null); System.out.println("Model: " + model.name().get()); // Update a tuned model's display name / description if (model.name().get().startsWith("tunedModels")) { Model updated = client.models.update( "tunedModels/my-tuned-model", UpdateModelConfig.builder() .displayName("My Production Model") .description("Fine-tuned on customer support data") .build()); System.out.println("Updated: " + updated); } // List all base models (paginated) for (Model m : client.models.list(ListModelsConfig.builder().pageSize(10).build())) { System.out.println(m.name().get()); } ``` ``` -------------------------------- ### Batch Inference Source: https://context7.com/googleapis/java-genai/llms.txt Submit asynchronous batch jobs against BigQuery or GCS JSONL sources for large-scale offline processing. Supports creating, getting status, listing, and cancelling batch jobs. ```APIDOC ## `client.batches` — Batch Inference Submit asynchronous batch jobs against BigQuery or GCS JSONL sources, suitable for large-scale offline processing. ```java import com.google.genai.Client; import com.google.genai.types.*; Client client = new Client(); // Vertex AI: GCS-based batch BatchJobSource source = BatchJobSource.builder() .gcsUri("gs://my-bucket/input/requests.jsonl") .format("jsonl") .build(); CreateBatchJobConfig config = CreateBatchJobConfig.builder() .displayName("nightly-summarization") .dest(BatchJobDestination.builder() .gcsUri("gs://my-bucket/output") .format("jsonl")) .build(); BatchJob job = client.batches.create("gemini-2.5-flash", source, config); System.out.println("Job: " + job.name().get() + " state: " + job.state().get()); // Get status BatchJob status = client.batches.get(job.name().get(), null); // List jobs for (BatchJob b : client.batches.list(ListBatchJobsConfig.builder().pageSize(5).build()).page()) { System.out.println(b.name().get() + " -> " + b.state().get()); } // Cancel client.batches.cancel(job.name().get(), null); ``` ``` -------------------------------- ### Configure Client Proxy Options Source: https://github.com/googleapis/java-genai/blob/main/README.md Set up proxy configurations for the HTTP client using ProxyOptions, supporting HTTP, SOCKS, and DIRECT connection types, along with authentication. Setting type to DIRECT enforces a direct connection, bypassing system proxies. ```java import com.google.genai.Client; import com.google.genai.types.ClientOptions; import com.google.genai.types.ProxyOptions; import com.google.genai.types.ProxyType; ClientOptions clientOptions = ClientOptions.builder() .proxyOptions( ProxyOptions.builder() .type(ProxyType.Known.HTTP) .host("your-proxy-host") .port(8080) .username("your-proxy-username") .password("your-proxy-password")) .build(); Client client = Client.builder().apiKey("your-api-key").clientOptions(clientOptions).build(); ``` -------------------------------- ### Initialize Google Gen AI Java SDK Client Source: https://context7.com/googleapis/java-genai/llms.txt Construct a Client instance using API keys or Vertex AI credentials. Ensure to close the client when done to release resources. ```java import com.google.genai.Client; import com.google.genai.types.HttpOptions; import com.google.genai.types.ClientOptions; // --- Option 1: Gemini Developer API via env var GOOGLE_API_KEY --- Client client = new Client(); // --- Option 2: Explicit API key --- Client client = Client.builder() .apiKey("YOUR_API_KEY") .build(); // --- Option 3: Vertex AI / Gemini Enterprise Agent Platform via ADC --- // export GOOGLE_CLOUD_PROJECT=my-project // export GOOGLE_CLOUD_LOCATION=us-central1 // export GOOGLE_GENAI_USE_VERTEXAI = true Client client = Client.builder() .project("my-project") .location("us-central1") .enterprise(true) .build(); // --- Option 4: Custom HTTP options (API version, base URL, timeout, headers) --- Client client = Client.builder() .apiKey("YOUR_API_KEY") .httpOptions(HttpOptions.builder() .apiVersion("v1alpha") // default: beta .timeout(600) // seconds .build()) .build(); // --- Option 5: Connection pool & proxy --- Client client = Client.builder() .apiKey("YOUR_API_KEY") .clientOptions(ClientOptions.builder() .maxConnections(64) .maxConnectionsPerHost(16) .build()) .build(); // Always close when done to release HTTP connections client.close(); // Check backend in use System.out.println(client.vertexAI()); // true = Vertex AI ``` -------------------------------- ### Edit Image with Java Source: https://github.com/googleapis/java-genai/blob/main/README.md Edit an image using the `editImage` method, supporting inpainting and style transfer. This requires the Gemini Enterprise Agent Platform and specific model. Reference images and masks can guide the editing process. ```java package ; import com.google.genai.Client; import com.google.genai.types.EditImageConfig; import com.google.genai.types.EditImageResponse; import com.google.genai.types.EditMode; import com.google.genai.types.Image; import com.google.genai.types.MaskReferenceConfig; import com.google.genai.types.MaskReferenceImage; import com.google.genai.types.MaskReferenceMode; import com.google.genai.types.RawReferenceImage; import com.google.genai.types.ReferenceImage; import java.util.ArrayList; public class EditImage { public static void main(String[] args) { Client client = Client.builder().enterprise(true).build(); Image image = Image.fromFile("path/to/your/image"); // Edit image with a mask. EditImageConfig config = EditImageConfig.builder() .editMode(EditMode.Known.EDIT_MODE_INPAINT_INSERTION) .numberOfImages(1) .outputMimeType("image/jpeg") .build(); ArrayList referenceImages = new ArrayList<>(); RawReferenceImage rawReferenceImage = RawReferenceImage.builder().referenceImage(image).referenceId(1).build(); referenceImages.add(rawReferenceImage); MaskReferenceImage maskReferenceImage = MaskReferenceImage.builder() .referenceId(2) .config( MaskReferenceConfig.builder() .maskMode(MaskReferenceMode.Known.MASK_MODE_BACKGROUND) .maskDilation(0.0f)) .build(); referenceImages.add(maskReferenceImage); EditImageResponse response = client.models.editImage( "imagen-3.0-capability-001", "Sunlight and clear sky", referenceImages, config); response.generatedImages().ifPresent( images -> { Image editedImage = images.get(0).image().orElse(null); // Do something with the edited image. } ); } } ``` -------------------------------- ### Instantiate Client for Gemini API Source: https://github.com/googleapis/java-genai/blob/main/README.md Use the Client.builder() and explicitly set the API key to instantiate a client for the Gemini Developer backend. ```java import com.google.genai.Client; // Use Builder class for instantiation. Explicitly set the API key to use Gemini // Developer backend. Client client = Client.builder().apiKey("your-api-key").build(); ``` -------------------------------- ### Generate Content with Extra Configurations (Java) Source: https://github.com/googleapis/java-genai/blob/main/README.md This snippet shows how to configure content generation with system instructions, safety settings, and tools like Google Search. Adjust `HarmBlockThreshold` and `ThinkingConfig` for specific use cases. ```java package ; import com.google.common.collect.ImmutableList; import com.google.genai.Client; import com.google.genai.types.Content; import com.google.genai.types.GenerateContentConfig; import com.google.genai.types.GenerateContentResponse; import com.google.genai.types.GoogleSearch; import com.google.genai.types.HarmBlockThreshold; import com.google.genai.types.HarmCategory; import com.google.genai.types.Part; import com.google.genai.types.SafetySetting; import com.google.genai.types.ThinkingConfig; import com.google.genai.types.Tool; public class GenerateContentWithConfigs { public static void main(String[] args) { Client client = new Client(); // Sets the safety settings in the config. ImmutableList safetySettings = ImmutableList.of( SafetySetting.builder() .category(HarmCategory.Known.HARM_CATEGORY_HATE_SPEECH) .threshold(HarmBlockThreshold.Known.BLOCK_ONLY_HIGH) .build(), SafetySetting.builder() .category(HarmCategory.Known.HARM_CATEGORY_DANGEROUS_CONTENT) .threshold(HarmBlockThreshold.Known.BLOCK_LOW_AND_ABOVE) .build()); // Sets the system instruction in the config. Content systemInstruction = Content.fromParts(Part.fromText("You are a history teacher.")); // Sets the Google Search tool in the config. Tool googleSearchTool = Tool.builder().googleSearch(GoogleSearch.builder()).build(); GenerateContentConfig config = GenerateContentConfig.builder() // Sets the thinking budget to 0 to disable thinking mode .thinkingConfig(ThinkingConfig.builder().thinkingBudget(0)) .candidateCount(1) .maxOutputTokens(1024) .safetySettings(safetySettings) .systemInstruction(systemInstruction) .tools(googleSearchTool) .build(); GenerateContentResponse response = client.models.generateContent("gemini-2.5-flash", "Tell me the history of LLM", config); System.out.println("Response: " + response.text()); } } ``` -------------------------------- ### Create, Get, List, and Cancel Batch Inference Jobs Source: https://context7.com/googleapis/java-genai/llms.txt Submit asynchronous batch inference jobs for large-scale offline processing. Supports GCS or BigQuery as input sources and GCS for output. Use this for processing large datasets without real-time constraints. ```java import com.google.genai.Client; import com.google.genai.types.*; Client client = new Client(); // Vertex AI: GCS-based batch BatchJobSource source = BatchJobSource.builder() .gcsUri("gs://my-bucket/input/requests.jsonl") .format("jsonl") .build(); CreateBatchJobConfig config = CreateBatchJobConfig.builder() .displayName("nightly-summarization") .dest(BatchJobDestination.builder() .gcsUri("gs://my-bucket/output") .format("jsonl")) .build(); BatchJob job = client.batches.create("gemini-2.5-flash", source, config); System.out.println("Job: " + job.name().get() + " state: " + job.state().get()); // Get status BatchJob status = client.batches.get(job.name().get(), null); // List jobs for (BatchJob b : client.batches.list(ListBatchJobsConfig.builder().pageSize(5).build()).page()) { System.out.println(b.name().get() + " -> " + b.state().get()); } // Cancel client.batches.cancel(job.name().get(), null); ``` -------------------------------- ### Connect to Live Real-time Streaming (WebSocket) Source: https://context7.com/googleapis/java-genai/llms.txt Opens a persistent WebSocket session for real-time bidirectional text, audio, and video interactions. Requires configuration for response modalities, topP, and seed. ```java import com.google.genai.AsyncSession; import com.google.genai.Client; import com.google.genai.types.*; import java.util.concurrent.CompletableFuture; Client client = new Client(); LiveConnectConfig config = LiveConnectConfig.builder() .responseModalities(Modality.Known.TEXT) .topP(0.8f) .seed(1234) .build(); // Connect and obtain an AsyncSession AsyncSession session = client.async.live.connect("gemini-live-2.5-flash-preview", config).get(); CompletableFuture allDone = new CompletableFuture<>(); // Receive incoming messages session.receive(message -> { message.serverContent() .flatMap(LiveServerContent::modelTurn) .flatMap(Content::parts) .ifPresent(parts -> parts.forEach(p -> p.text().ifPresent(System.out::print))); if (message.serverContent().flatMap(LiveServerContent::turnComplete).orElse(false)) { allDone.complete(null); } }); // Send a message LiveSendClientContentParameters payload = LiveSendClientContentParameters.builder() .turnComplete(true) .turns(Content.fromParts(Part.fromText("Tell me a joke."))) .build(); session.sendClientContent(payload).get(); allDone.get(); session.close().get(); ``` -------------------------------- ### Generate Videos from Text (Java) Source: https://github.com/googleapis/java-genai/blob/main/README.md Use this snippet to generate videos from a text prompt. Ensure the 'veo-2.0-generate-001' model is available and configure generation parameters like number of videos and duration. ```java package ; import com.google.genai.Client; import com.google.genai.types.GenerateVideosConfig; import com.google.genai.types.GenerateVideosOperation; import com.google.genai.types.Video; public class GenerateVideosWithText { public static void main(String[] args) { Client client = new Client(); GenerateVideosConfig config = GenerateVideosConfig.builder() .numberOfVideos(1) .enhancePrompt(true) .durationSeconds(5) .build(); // generateVideos returns an operation GenerateVideosOperation operation = client.models.generateVideos( "veo-2.0-generate-001", "A neon hologram of a cat driving at top speed", null, config); // When the operation hasn't been finished, operation.done() is empty while (!operation.done().isPresent()) { try { System.out.println("Waiting for operation to complete..."); Thread.sleep(10000); // Sleep for 10 seconds and check the operation again operation = client.operations.getVideosOperation(operation, null); } catch (InterruptedException e) { System.out.println("Thread was interrupted while sleeping."); Thread.currentThread().interrupt(); } } operation.response().ifPresent( response -> { response.generatedVideos().ifPresent( videos -> { System.out.println("Generated " + videos.size() + " videos."); Video video = videos.get(0).video().orElse(null); // Do something with the generated video } ); } ); } } ``` -------------------------------- ### Edit Image Source: https://github.com/googleapis/java-genai/blob/main/README.md The `editImage` method lets you edit an image. You can input reference images (ex. mask reference for inpainting, or style reference for style transfer) in addition to a text prompt to guide the editing. This feature uses a different model than `generateImages` and `upscaleImage` and is only supported in Gemini Enterprise Agent Platform. ```APIDOC ## Edit Image ### Description Edits an existing image based on a text prompt and optional reference images (e.g., masks for inpainting, style references). Requires Gemini Enterprise Agent Platform. ### Method Signature `EditImageResponse editImage(String modelName, String prompt, List referenceImages, EditImageConfig config)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None (Image, prompt, references, and configuration are passed as arguments) ### Request Example ```java Client client = Client.builder().enterprise(true).build(); Image image = Image.fromFile("path/to/your/image"); EditImageConfig config = EditImageConfig.builder() .editMode(EditMode.Known.EDIT_MODE_INPAINT_INSERTION) .numberOfImages(1) .outputMimeType("image/jpeg") .build(); ArrayList referenceImages = new ArrayList<>(); RawReferenceImage rawReferenceImage = RawReferenceImage.builder().referenceImage(image).referenceId(1).build(); referenceImages.add(rawReferenceImage); MaskReferenceImage maskReferenceImage = MaskReferenceImage.builder() .referenceId(2) .config(MaskReferenceConfig.builder() .maskMode(MaskReferenceMode.Known.MASK_MODE_BACKGROUND) .maskDilation(0.0f)) .build(); referenceImages.add(maskReferenceImage); EditImageResponse response = client.models.editImage("imagen-3.0-capability-001", "Sunlight and clear sky", referenceImages, config); ``` ### Response #### Success Response (200) - **generatedImages** (Optional>) - An optional list containing the edited images. #### Response Example ```java response.generatedImages().ifPresent( images -> { Image editedImage = images.get(0).image().orElse(null); // Do something with the edited image. } ); ``` ``` -------------------------------- ### Instantiate Client for Gemini Enterprise Agent Platform (API Key - GCP Express Mode) Source: https://github.com/googleapis/java-genai/blob/main/README.md Use the Client.builder(), set the API key, and enable enterprise mode to instantiate a client for the Gemini Enterprise Agent Platform backend in express mode. ```java import com.google.genai.Client; // Explicitly set the `apiKey` and `enterprise(true)` to use Gemini Enterprise Agent Platform backend // in express mode. Client client = Client.builder() .apiKey("your-api-key") .enterprise(true) .build(); ``` -------------------------------- ### client.async.live.connect — Live Real-time Streaming (WebSocket) Source: https://context7.com/googleapis/java-genai/llms.txt Opens a persistent WebSocket session for real-time bidirectional text, audio, and video interactions using the `connect` method. It demonstrates how to configure the connection, receive messages, send client content, and manage the session lifecycle. ```APIDOC ## `client.async.live.connect` — Live Real-time Streaming (WebSocket) Open a persistent WebSocket session for real-time bidirectional text, audio, and video interactions. ```java import com.google.genai.AsyncSession; import com.google.genai.Client; import com.google.genai.types.*; import java.util.concurrent.CompletableFuture; Client client = new Client(); LiveConnectConfig config = LiveConnectConfig.builder() .responseModalities(Modality.Known.TEXT) .topP(0.8f) .seed(1234) .build(); // Connect and obtain an AsyncSession AsyncSession session = client.async.live.connect("gemini-live-2.5-flash-preview", config).get(); CompletableFuture allDone = new CompletableFuture<>(); // Receive incoming messages session.receive(message -> { message.serverContent() .flatMap(LiveServerContent::modelTurn) .flatMap(Content::parts) .ifPresent(parts -> parts.forEach(p -> p.text().ifPresent(System.out::print))); if (message.serverContent().flatMap(LiveServerContent::turnComplete).orElse(false)) { allDone.complete(null); } }); // Send a message LiveSendClientContentParameters payload = LiveSendClientContentParameters.builder() .turnComplete(true) .turns(Content.fromParts(Part.fromText("Tell me a joke."))) .build(); session.sendClientContent(payload).get(); allDone.get(); session.close().get(); ``` ``` -------------------------------- ### Generate Videos from Image (Java) Source: https://github.com/googleapis/java-genai/blob/main/README.md This snippet demonstrates generating videos from an image input. Provide the path to your image file and configure generation parameters. The operation polling mechanism is crucial for handling asynchronous video generation. ```java package ; import com.google.genai.Client; import com.google.genai.types.GenerateVideosConfig; import com.google.genai.types.GenerateVideosOperation; import com.google.genai.types.Image; import com.google.genai.types.Video; public class GenerateVideosWithImage { public static void main(String[] args) { Client client = new Client(); Image image = Image.fromFile("path/to/your/image"); GenerateVideosConfig config = GenerateVideosConfig.builder() .numberOfVideos(1) .enhancePrompt(true) .durationSeconds(5) .build(); // generateVideos returns an operation GenerateVideosOperation operation = client.models.generateVideos( "veo-2.0-generate-001", "Night sky", image, config); // When the operation hasn't been finished, operation.done() is empty while (!operation.done().isPresent()) { try { System.out.println("Waiting for operation to complete..."); Thread.sleep(10000); // Sleep for 10 seconds and check the operation again operation = client.operations.getVideosOperation(operation, null); } catch (InterruptedException e) { System.out.println("Thread was interrupted while sleeping."); Thread.currentThread().interrupt(); } } operation.response().ifPresent( response -> { response.generatedVideos().ifPresent( videos -> { System.out.println("Generated " + videos.size() + " videos."); Video video = videos.get(0).video().orElse(null); // Do something with the generated video } ); } ); } } ``` -------------------------------- ### Set Environment Variables for Gemini Enterprise Agent Platform Source: https://github.com/googleapis/java-genai/blob/main/README.md Configure environment variables for Gemini Enterprise Agent Platform. Set GOOGLE_GENAI_USE_ENTERPRISE along with either project and location, or an API key for express mode. Project and location take precedence over API key. ```bash export GOOGLE_GENAI_USE_ENTERPRISE=true // Set project and location for Gemini Enterprise Agent Platform authentication export GOOGLE_CLOUD_PROJECT='your-project-id' export GOOGLE_CLOUD_LOCATION='us-central1' // or API key for express mode export GOOGLE_API_KEY='your-api-key' ``` -------------------------------- ### Generate Content with Text Input Source: https://github.com/googleapis/java-genai/blob/main/README.md Perform basic content generation using the `generateContent` method with a text prompt. The client automatically uses the Gemini API and retrieves the API key from the GOOGLE_API_KEY environment variable. Response headers can be accessed via `sdkHttpResponse()`. ```java package ; import com.google.genai.Client; import com.google.genai.types.GenerateContentResponse; public class GenerateContentWithTextInput { public static void main(String[] args) { // Instantiate the client. The client by default uses the Gemini API. It // gets the API key from the environment variable `GOOGLE_API_KEY`. Client client = new Client(); GenerateContentResponse response = client.models.generateContent("gemini-2.5-flash", "What is your name?", null); // Gets the text string from the response by the quick accessor method `text()`. System.out.println("Unary response: " + response.text()); // Gets the http headers from the response. response .sdkHttpResponse() .ifPresent( httpResponse -> System.out.println("Response headers: " + httpResponse.headers().orElse(null))); } } ``` -------------------------------- ### Count Tokens for Text and Content with Configuration Source: https://context7.com/googleapis/java-genai/llms.txt Count tokens for a text prompt or a Content object with system instructions. Local tokenization is also demonstrated, which does not require an API call. ```java import com.google.genai.Client; import com.google.genai.types.*; Client client = new Client(); // Count tokens for a text prompt CountTokensResponse countResponse = client.models.countTokens("gemini-2.5-flash", "What is your name?", null); System.out.println("Token count: " + countResponse); // e.g. CountTokensResponse{totalTokens=5} // Count tokens for a Content with config CountTokensConfig config = CountTokensConfig.builder() .systemInstruction(Content.fromParts(Part.fromText("You are a helpful assistant."))) .build(); CountTokensResponse countWithConfig = client.models.countTokens("gemini-2.5-flash", "Explain quantum computing.", config); System.out.println("Tokens with config: " + countWithConfig); ``` ```java import com.google.genai.LocalTokenizer; LocalTokenizer tokenizer = new LocalTokenizer("gemini-2.5-flash"); System.out.println("Local count: " + tokenizer.countTokens("Hello world").totalTokens()); // 2 ``` -------------------------------- ### File Operations with Google Gen AI Java SDK Source: https://github.com/googleapis/java-genai/blob/main/README.md Demonstrates uploading a PDF file, using it in a content generation request, retrieving the file, listing all files, and finally deleting the uploaded file. Ensure you have the correct file path and necessary permissions. ```java package ; import com.google.genai.Client; import com.google.genai.errors.GenAiIOException; import com.google.genai.types.Content; import com.google.genai.types.DeleteFileResponse; import com.google.genai.types.File; import com.google.genai.types.GenerateContentResponse; import com.google.genai.types.ListFilesConfig; import com.google.genai.types.Part; import com.google.genai.types.UploadFileConfig; /** An example of how to use the Files module to upload, retrieve, list, and delete files. */ public final class FileOperations { public static void main(String[] args) { Client client = new Client(); // Upload a file to the API. try { File file = client.files.upload( "path/to/your/file.pdf", UploadFileConfig.builder().mimeType("application/pdf").build()); // Use the uploaded file in the generateContent Content content = Content.fromParts( Part.fromText("Summary this pdf."), Part.fromUri(file.name().get(), file.mimeType().get())); GenerateContentResponse response = client.models.generateContent("gemini-2.5-flash", content, null); // Get the uploaded file. File retrievedFile = client.files.get(file.name().get(), null); // List all files. System.out.println("List files: "); for (File f : client.files.list(ListFilesConfig.builder().pageSize(10).build())) { System.out.println("File name: " + f.name().get()); } // Delete the uploaded file. client.files.delete(file.name().get(), null); } catch (GenAiIOException e) { System.out.println("An error occurred while uploading the file: " + e.getMessage()); } } } ``` -------------------------------- ### Create and Manage File Search Stores Source: https://context7.com/googleapis/java-genai/llms.txt Enables semantic file search using document stores backed by the Files API. This involves creating a store, uploading files, importing them into the store, and managing documents. Error handling for GenAiIOException is included. ```java import com.google.genai.Client; import com.google.genai.errors.GenAiIOException; import com.google.genai.types.*; Client client = new Client(); // Gemini API only try { // Create a file search store FileSearchStore store = client.fileSearchStores.create(null); System.out.println("Store: " + store.name().get()); // Upload a file to the Files API, then import it into the store File file = client.files.upload("data.txt", UploadFileConfig.builder().mimeType("text/plain").build()); ImportFileOperation importOp = client.fileSearchStores.importFile(store.name().get(), file.name().get(), null); while (importOp.done().filter(Boolean::booleanValue).isEmpty()) { Thread.sleep(5_000); importOp = client.operations.get(importOp, null); } // Directly upload a file to the store UploadToFileSearchStoreOperation uploadOp = client.fileSearchStores.uploadToFileSearchStore(store.name().get(), "data.txt", null); while (uploadOp.done().filter(Boolean::booleanValue).isEmpty()) { Thread.sleep(5_000); uploadOp = client.operations.get(uploadOp, null); } String docName = uploadOp.response().get().documentName().get(); // List documents in the store for (Document doc : client.fileSearchStores.documents.list(store.name().get(), null)) { System.out.println("Document: " + doc.name().get()); } // Clean up client.fileSearchStores.documents.delete(docName, null); client.fileSearchStores.delete(store.name().get(), null); } catch (GenAiIOException e) { System.err.println("Error: " + e.getMessage()); } ``` -------------------------------- ### Compute Tokens Info for a Prompt Source: https://github.com/googleapis/java-genai/blob/main/README.md This snippet computes detailed token information, including token IDs, for a given prompt. It requires the Gemini Enterprise Agent Platform and is useful for advanced token analysis. Initialize the client with `enterprise(true)` to enable this functionality. ```java package ; import com.google.genai.Client; import com.google.genai.types.ComputeTokensResponse; public class ComputeTokens { public static void main(String[] args) { Client client = Client.builder().enterprise(true).build(); ComputeTokensResponse response = client.models.computeTokens("gemini-2.5-flash", "What is your name?", null); System.out.println("Compute tokens response: " + response); } } ``` -------------------------------- ### Generate Content with Text and Image Input (Java) Source: https://github.com/googleapis/java-genai/blob/main/README.md Use this snippet to generate content using a multimodal model that accepts both text and image inputs. Ensure the client is instantiated correctly and the image URI is valid. ```java package ; import com.google.common.collect.ImmutableList; import com.google.genai.Client; import com.google.genai.types.Content; import com.google.genai.types.GenerateContentResponse; import com.google.genai.types.Part; public class GenerateContentWithImageInput { public static void main(String[] args) { // Instantiate the client using Gemini Enterprise Agent Platform API. The client gets the project and // location from the environment variables `GOOGLE_CLOUD_PROJECT` and // `GOOGLE_CLOUD_LOCATION`. Client client = Client.builder().enterprise(true).build(); // Construct a multimodal content with quick constructors Content content = Content.fromParts( Part.fromText("describe the image"), Part.fromUri("gs://path/to/image.jpg", "image/jpeg")); GenerateContentResponse response = client.models.generateContent("gemini-2.5-flash", content, null); System.out.println("Response: " + response.text()); } } ``` -------------------------------- ### Automatic Function Calling with Generate Content Source: https://github.com/googleapis/java-genai/blob/main/README.md Demonstrates how to enable and use automatic function calling by providing reflected Method objects in the GenerateContentConfig. Ensure the compiler argument '-parameters' is set. ```java import com.google.common.collect.ImmutableList; import com.google.genai.Client; import com.google.genai.types.GenerateContentConfig; import com.google.genai.types.GenerateContentResponse; import com.google.genai.types.Tool; import java.lang.reflect.Method; public class GenerateContentWithFunctionCall { public static String getCurrentWeather(String location, String unit) { return "The weather in " + location + " is " + "very nice."; } public static void main(String[] args) throws NoSuchMethodException { Client client = new Client(); // Load the method as a reflected Method object so that that it can be // automatically executed on the client side. Method method = GenerateContentWithFunctionCall.class.getMethod( "getCurrentWeather", String.class, String.class); GenerateContentConfig config = GenerateContentConfig.builder() .tools(Tool.builder().functions(method)) .build(); GenerateContentResponse response = client.models.generateContent( "gemini-2.5-flash", "What is the weather in Vancouver?", config); System.out.println("The response is: " + response.text()); System.out.println( "The automatic function calling history is: " + response.automaticFunctionCallingHistory().get()); } } ``` -------------------------------- ### Set Environment Variable for Gemini Developer API Key Source: https://github.com/googleapis/java-genai/blob/main/README.md Set the GOOGLE_API_KEY environment variable for automatic client configuration when using the Gemini Developer API. GOOGLE_API_KEY takes precedence over the legacy GEMINI_API_KEY. ```bash export GOOGLE_API_KEY='your-api-key' ``` -------------------------------- ### client.models.generateContent Source: https://context7.com/googleapis/java-genai/llms.txt Sends a prompt to a model and returns a single `GenerateContentResponse`. Supports plain text, `Content`, or a `List` for generation. ```APIDOC ## `client.models.generateContent` — Unary Text Generation Sends a prompt to a model and returns a single `GenerateContentResponse`. Accepts plain text, `Content`, or a `List`. ```java import com.google.genai.Client; import com.google.genai.types.*; Client client = new Client(); // uses GOOGLE_API_KEY // Simple text prompt GenerateContentResponse response = client.models.generateContent("gemini-2.5-flash", "What is your name?", null); System.out.println(response.text()); // quick accessor // With full config: system instruction, safety settings, Google Search, token limit ImmutableList safetySettings = ImmutableList.of( SafetySetting.builder() .category(HarmCategory.Known.HARM_CATEGORY_HATE_SPEECH) .threshold(HarmBlockThreshold.Known.BLOCK_ONLY_HIGH) .build()); GenerateContentConfig config = GenerateContentConfig.builder() .systemInstruction(Content.fromParts(Part.fromText("You are a history teacher."))) .maxOutputTokens(1024) .candidateCount(1) .safetySettings(safetySettings) .tools(Tool.builder().googleSearch(GoogleSearch.builder()).build()) .build(); GenerateContentResponse response2 = client.models.generateContent("gemini-2.5-flash", "Tell me the history of LLMs", config); System.out.println(response2.text()); // Multimodal: text + image Content multimodal = Content.fromParts( Part.fromText("Describe this image"), Part.fromUri("gs://my-bucket/photo.jpg", "image/jpeg")); GenerateContentResponse response3 = client.models.generateContent("gemini-2.5-flash", multimodal, null); System.out.println(response3.text()); ``` ```