Class GoogleAiGeminiImageModel
java.lang.Object
dev.langchain4j.model.googleai.GoogleAiGeminiImageModel
- All Implemented Interfaces:
ImageModel
Represents a Google AI Gemini model for image generation and editing.
Gemini can generate and process images conversationally. You can prompt either the fast Gemini 2.5 Flash Image (Nano Banana) or the advanced Gemini 3 Pro Preview image (Nano Banana Pro).
Key Capabilities
- Text-to-Image: Generate high-quality images from text descriptions.
- Image Editing: Use text prompts to edit and adjust a given image, or use multiple input images to compose new scenes and transfer styles.
- High-Fidelity Text Rendering: Accurately generate images that contain legible and well-placed text, ideal for logos, diagrams, and posters.
All generated images include a SynthID watermark.
Supported Models
- gemini-2.5-flash-image: Optimized for speed and efficiency, ideal for high-volume, low-latency tasks. Generates images at 1024px resolution.
- gemini-3-pro-image-preview Designed for professional asset production with advanced reasoning, Google Search grounding, and up to 4K resolution output.
Example Usage
GoogleAiGeminiImageModel model = GoogleAiGeminiImageModel.builder()
.apiKey(System.getenv("GOOGLE_AI_GEMINI_API_KEY"))
.modelName("gemini-2.5-flash-image")
.aspectRatio("16:9")
.build();
// Generate an image
Response<Image> response = model.generate("A serene mountain landscape at sunset");
// Edit an existing image
Response<Image> edited = model.edit(originalImage, "Add a hot air balloon to the sky");
Limitations
- For best performance, use supported languages: EN, ar-EG, de-DE, es-MX, fr-FR, hi-IN, id-ID, it-IT, ja-JP, ko-KR, pt-BR, ru-RU, ua-UA, vi-VN, zh-CN.
- Image generation does not support audio or video inputs.
- gemini-2.5-flash-image works best with up to 3 images as input, while gemini-3-pro-image-preview supports up to 14 reference images.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classException thrown when image generation or editing fails.static classBuilder for constructingGoogleAiGeminiImageModelinstances. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Returns a new builder for constructing GoogleAiGeminiImageModel instances.Edits an existing image using a mask to specify the area to modify.Edits an existing image based on a text prompt.Generates an image from a text prompt.Returns the model name used for image generation.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ImageModel
generate
-
Method Details
-
builder
Returns a new builder for constructing GoogleAiGeminiImageModel instances. -
modelName
Returns the model name used for image generation. -
generate
Generates an image from a text prompt.Example prompts:
- "A photorealistic close-up portrait of an elderly Japanese ceramicist"
- "A kawaii-style sticker of a happy red panda wearing a tiny bamboo hat"
- "Create a modern, minimalist logo for a coffee shop called 'The Daily Grind'"
- Specified by:
generatein interfaceImageModel- Parameters:
prompt- the text description of the image to generate- Returns:
- a Response containing the generated Image with base64 data and MIME type
- Throws:
GoogleAiGeminiImageModel.GeminiImageGenerationException- if no image is generated or an error occurs
-
edit
Edits an existing image based on a text prompt.This method allows you to add, remove, or modify elements in an image using natural language instructions. The model will match the original image's style, lighting, and perspective when making changes.
Example prompts:
- "Add a small wizard hat on the cat's head"
- "Change the blue sofa to a brown leather chesterfield"
- "Transform this photo into Van Gogh's Starry Night style"
- Specified by:
editin interfaceImageModel- Parameters:
image- the image to edit (must have base64Data set, URL-based images are not supported)prompt- the text description of the edit to apply- Returns:
- a Response containing the edited Image
- Throws:
IllegalArgumentException- if image is null or prompt is blankGoogleAiGeminiImageModel.GeminiImageGenerationException- if the image has no base64 data or editing fails
-
edit
Edits an existing image using a mask to specify the area to modify.This method allows for more precise inpainting by providing a mask image that defines which areas of the original image should be modified. The mask and edit prompt work together to produce targeted changes while preserving the rest of the image.
- Specified by:
editin interfaceImageModel- Parameters:
image- the image to edit (must have base64Data set)mask- the mask image defining the area to edit (must have base64Data set)prompt- the text description of the edit to apply to the masked area- Returns:
- a Response containing the edited Image
- Throws:
IllegalArgumentException- if image, mask is null or prompt is blankGoogleAiGeminiImageModel.GeminiImageGenerationException- if editing fails
-