0

I am currently have a tool that uses Gemini-2.5-flash-image to generate images. However, I am only getting 1 image to be generated. I want the model to create multiple images for example 3 images at once. What is the ideal way to set it up ?

Is it only possible by looping and calling the model multiple times to get more images ? but that seems inefficient to make multiple calls. Seems like this should be possible within a single call.

It also says in the docs (https://docs.cloud.google.com/vertex-ai/generative-ai/docs/models/gemini/2-5-flash-image): “Maximum number of output images per prompt: 10”

Below is the code Ive setup.

Things ive tried:

  • Changing candidate_count value doesn’t create more images.

  • I also changed the prompt and asked it to directly created 3 images but still only creates 1 image.

generation_config = GenerateContentConfig(
            response_modalities=[Modality.IMAGE],
            candidate_count=1,  # Only one candidate is supported.
            safety_settings=[
                SafetySetting(
                    category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
                    threshold=HarmBlockThreshold.BLOCK_ONLY_HIGH,
                ),
            ],
        )

response = client.models.generate_content(
                    model="gemini-2.5-flash-image",
                    contents=f"Generate an image of: {prompt}",
                    config=generation_config,
                )


2
  • wild guess candidate_count=1, Commented Oct 29 at 19:54
  • Yes I tried that and asked to generate multiple images but it still only provides 1 image. Commented Oct 30 at 7:54

1 Answer 1

0

Multiple generation result ≠ candidate_count.

You can generate multiple images results from gemini-2.5-flash-image by doing the following:

  • Don't set candidate_count

  • Set response modality have both "TEXT" and "IMAGE"

  • Prompt message to generate multiple images.

Here is sample snippet to try (works for me):

import base64
import os
from google import genai
from google.genai import types

def generate_and_save_images():
  # Initialize the client. Ensure GOOGLE_CLOUD_API_KEY is set in your environment.
  client = genai.Client(
      vertexai=True,
      api_key=os.environ.get("GOOGLE_CLOUD_API_KEY"),
  )

  model = "gemini-2.5-flash-image"
  prompt_text = "Generate three distinct images: A futuristic city skyline at sunset, a fluffy white cat on a red couch, and a tranquil forest stream."

  contents = [
    types.Content(
      role="user",
      parts=[
        types.Part.from_text(text=prompt_text)
      ]
    ),
  ]

  # Define safety settings
  safety_settings = [
    types.SafetySetting(
      category="HARM_CATEGORY_HATE_SPEECH",
      threshold="OFF"
    ),
    types.SafetySetting(
      category="HARM_CATEGORY_DANGEROUS_CONTENT",
      threshold="OFF"
    ),
    types.SafetySetting(
      category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
      threshold="OFF"
    ),
    types.SafetySetting(
      category="HARM_CATEGORY_HARASSMENT",
      threshold="OFF"
    )
  ]

  # Configure generation parameters. safety_settings is part of GenerateContentConfig.
  generate_content_config = types.GenerateContentConfig(
    temperature = 1,
    top_p = 0.95,
    max_output_tokens = 32768,
    # candidate_count = 1,
    response_modalities = ["TEXT", "IMAGE"],
    safety_settings = safety_settings,
  )

  print(f"Requesting '{generate_content_config.candidate_count}' images from '{model}'...")
  try:
    # Use the non-streaming generate_content method.
    # safety_settings is NOT passed as a separate argument here.
    response = client.models.generate_content(
      model = model,
      contents = contents,
      config = generate_content_config,
    )
except Exception as e:
    print(f"An error occurred during generate_content: {e}")

# Run the function
generate_and_save_images()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.