Class GoogleAiGeminiBatchImageModel

java.lang.Object
dev.langchain4j.model.googleai.GoogleAiGeminiBatchImageModel

@Experimental public final class GoogleAiGeminiBatchImageModel extends Object
Provides an interface for batch image generation using the Gemini Batch API.

This is an asynchronous service designed for processing large volumes of image generation requests at a reduced cost (50% of standard pricing). It is ideal for non-urgent, large-scale tasks with a Service Level Objective (SLO) of 24-hour turnaround, though completion is often much quicker.

Key Features

  • Cost Savings: 50% reduction compared to real-time image generation
  • High Throughput: Process many image generation requests in a single batch
  • Flexible Input: Submit requests inline (up to 20MB) or via uploaded files (up to 2GB) using the GeminiFiles api
  • Configurable: Supports aspect ratio, image size, and safety settings

Workflow

  1. Create a batch using createBatchInline(String, Long, List) or createBatchFromFile(String, GeminiFiles.GeminiFile)
  2. Poll for completion using retrieveBatchResults(BatchRequestResponse.BatchName)
  3. Process the generated images from the BatchRequestResponse.BatchSuccess response
  4. Optionally cancel or delete the batch job

Example Usage

GoogleAiGeminiBatchImageModel model = GoogleAiGeminiBatchImageModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_GEMINI_API_KEY"))
    .modelName("gemini-2.5-flash-image")
    .aspectRatio("16:9")
    .build();

// Create batch with image generation requests
List<ImageGenerationRequest> requests = List.of(
    new ImageGenerationRequest("A serene mountain landscape at sunset"),
    new ImageGenerationRequest("A futuristic cityscape at night")
);

BatchResponse<Response<Image>> response = model.createBatchInline("My Batch", 1L, requests);

// Poll for completion
BatchName batchName = ((BatchIncomplete<?>) response).batchName();
BatchResponse<Response<Image>> result;
do {
    Thread.sleep(5000);
    result = model.retrieveBatchResults(batchName);
} while (result instanceof BatchIncomplete);

// Process results
if (result instanceof BatchSuccess<Response<Image>> success) {
    for (Response<Image> imageResponse : success.responses()) {
        Image image = imageResponse.content();
        // Save or process the generated image
    }
}
See Also: