You can use Gemini to generate images from a text prompt. Supported interfaces include the Google Cloud console and the Agent Platform API.
The following Gemini models support image generation:
Click to expand supported models
For more information about Gemini model capabilities, see Gemini models.
Generate images
The following shows how to generate images using either Agent Studio or using the API.
For more information about best practices for prompting, see Design multimodal prompts.
Console
To generate images with Gemini, do the following:
- Open Agent Studio > Create prompt.
- Click Switch model and select one of the displayed models.
- In the Outputs panel, select Image and text from the drop-down menu.
- Write a description of the image you want to generate in the text area of the Write a prompt text area.
- Click the Prompt () button.
Gemini generates an image based on your description. This process takes a few seconds, but can be comparatively slower depending on capacity.
Python
Install
pip install --upgrade google-genai
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
Go
Learn how to install or update the Go.
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
Node.js
Install
npm install @google/genai
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
Java
Learn how to install or update the Java.
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
REST
Run the following command in the terminal to create or overwrite this file in the current directory:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${API_ENDPOINT}:generateContent \
-d '{
"contents": {
"role": "USER",
"parts": [
{
"text": "Create a tutorial explaining how to make a peanut butter and jelly sandwich in three easy steps."
}
]
},
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
},
},
"safetySettings": {
"method": "PROBABILITY",
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
}' 2>/dev/null >response.json
Gemini generates an image based on your description. This process takes a few seconds, but can be comparatively slower depending on capacity.
Generate interleaved images and text
You can use Gemini to generate interleaved images with text responses. For example, you can generate images for each step of a generated recipe without having to make separate requests to the model.
The following Gemini models support generating interleaved images and text:
Click to expand supported models
Console
To generate interleaved images with text responses, do the following:
- Open Agent Studio > Create prompt.
- Click Switch model and select one of the displayed models.
- In the Outputs panel, select Image and text from the drop-down menu.
- Write a description of the image you want to generate in the text area of the Write a prompt text area. For example, "Create a tutorial explaining how to make a peanut butter and jelly sandwich in three steps. For each step, provide a title with the number of the step, an explanation, and also generate an image, generate each image in a 1:1 aspect ratio."
- Click the Prompt () button.
Gemini generates a response based on your description. This process takes a few seconds, but can be comparatively slower depending on capacity.
Python
Install
pip install --upgrade google-genai
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
Java
Learn how to install or update the Java.
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
Go
Learn how to install or update the Go.
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
Node.js
Install
npm install @google/genai
To learn more, see the SDK reference documentation.
Set environment variables to use the Google Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
REST
Run the following command in the terminal to create or overwrite this file in the current directory:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${API_ENDPOINT}:generateContent \
-d '{
"contents": {
"role": "USER",
"parts": [
{
"text": "Generate an illustrated recipe for a paella. Create images to
go alongside the text as you generate the recipe."
}
]
},
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {
"aspectRatio": "16:9",
},
},
"safetySettings": {
"method": "PROBABILITY",
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
}' 2>/dev/null >response.json
Gemini generates an image based on your description. This process takes a few seconds, but can be comparatively slower depending on capacity.
Save generated images to Cloud Storage
Instead of returning raw base64-encoded image bytes inline in the response, you
can configure generateContent and streamGenerateContent requests to write
generated images directly to a Cloud Storage bucket and return the
object URI in fileData.fileUri.
The following Gemini models support Cloud Storage output persistence:
- Gemini Nano Banana 2.1
(
gemini-nano-banana-2.1) - Gemini 3.1 Flash Image
(
gemini-3.1-flash-image) - Gemini 3.1 Flash-Lite Image
(
gemini-3.1-flash-lite-image) - Gemini 3 Pro Image
(
gemini-3-pro-image)
Prerequisites
Before sending a request with Cloud Storage output persistence, verify that the principal authenticating the request has the following Identity and Access Management permission on the destination Cloud Storage bucket:
storage.objects.create(included in roles such as the Storage Object Creator role,roles/storage.objectCreator)
Before generating images, the service verifies storage.objects.create
permission on the target Cloud Storage bucket using the caller's
credentials. If the Cloud Storage bucket does not exist, the caller
lacks write permission, or a VPC Service Controls perimeter blocks the write,
the request fails before generation with a 403 PERMISSION_DENIED error.
Send a request with Cloud Storage output
To write generated images directly to Cloud Storage, configure both of
the following fields in your generateContent or streamGenerateContent
request:
- Set
outputConfig.outputUriPrefixto your destination Cloud Storage bucket or folder prefix (for example,gs://BUCKET_NAME/PREFIX/). - Set
generationConfig.responseFormat[].image.deliveryto"URI".
The following REST request generates an image and writes it directly to a Cloud Storage bucket:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
"https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/gemini-nano-banana-2.1:generateContent" \
-d '{
"contents": [
{
"role": "USER",
"parts": [
{
"text": "Create an architectural rendering of a sustainable glass greenhouse in alpine snow."
}
]
}
],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"responseFormat": [
{
"image": {
"delivery": "URI"
}
}
]
},
"outputConfig": {
"outputUriPrefix": "gs://BUCKET_NAME/PREFIX/"
}
}'
Replace the following:
- PROJECT_ID: Your Google Cloud project ID.
- BUCKET_NAME: The name of the destination Cloud Storage bucket.
- PREFIX: Optional folder path prefix inside the
Cloud Storage bucket (for example,
outputs).
The service writes each generated image part to Cloud Storage using the
naming pattern
gs://BUCKETNAME/PREFIX/imageGENERATIONID.EXTENSION
(appending CANDIDATEINDEX when a response contains
multiple candidates, and IMAGE_INDEX when a candidate
contains multiple images) and returns the URI in fileData:
{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"fileData": {
"mimeType": "image/png",
"fileUri": "gs://my-bucket/outputs/image_1234567890.png"
}
}
]
}
}
]
}
When you configure Cloud Storage output delivery, the service handles responses as follows:
- Mixed text and image responses: Text parts in a
TEXTandIMAGEresponse remain inline inparts[].text. Only generated image parts are written to Cloud Storage and returned asparts[].fileDataobjects. - Thinking images: When
generationConfig.thinkingConfig.includeThoughtsis set totrue, intermediate thinking images (parts[].thought: true) are also written to Cloud Storage using thethoughtimageGENERATION_IDprefix and returned asparts[].fileDataobjects. - Streaming responses (
streamGenerateContent): Each generated image is written to Cloud Storage before the response chunk referencing itsfileData.fileUriis sent. - No-image responses: If a request returns only text or is blocked by safety filters, no object is written to Cloud Storage.
- Encryption and retention: Written objects inherit the destination Cloud Storage bucket's encryption configuration, including Customer-managed encryption keys (CMEK), and follow the Cloud Storage bucket's lifecycle management rules.
Limitations
Cloud Storage output has the following limitation:
Multi-turn editing with cross-project Cloud Storage buckets: The service writes generated images using the credentials of the principal that sent the request. However, when a later request in a multi-turn conversation references a generated image by its
fileData.fileUri, the service reads the object using the Gemini Enterprise Agent Platform service agent of the calling project (service-PROJECT_NUMBER@gcp-sa-aiplatform.iam.gserviceaccount.com). If the destination Cloud Storage bucket is in a different project and the service agent doesn't have thestorage.objects.getpermission on that Cloud Storage bucket, the follow-up request fails with a403 PERMISSION_DENIEDerror even though the original write succeeded.To use generated images as input in multi-turn editing, grant the service agent read access to the destination Cloud Storage bucket (for example, the Storage Object Viewer role,
roles/storage.objectViewer).
What's next
See the following links for more information about Gemini image generation: