I spent like 3 hours tinkering in Python to make everything work to be met with “400 Model does not support the requested response modalities: image”, I go on the forum and turns out it’s not avaible to everyone. My question is where do I file a request or something similar to maybe get access to API image generation?
Hi @user15000, Welcome to forum!!!
Have you tried new image generation model “gemini-2.0-flash-exp-image-generation”. Here is the doc. Let me know if you are able to generate images or facing any issues.
Thanks
Well, “gemini-2.0-flash-exp-image-generation” is exactly the model that gives me error 400. A person on this forum that had a similar problem with audio generation was told audio and image generations for API aren’t avaiable for everyone
Hi, I tried working on it a bit, and I got something functional. It’s not perfect, but it gets the job done.
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
def generate_colored_image(input_image_path: str, output_image_path: str = "colored_image.png"):
# Set your API key
api_key = "YOUR_API_KEY"
client = genai.Client(api_key=api_key)
# Load the image using PIL
image = Image.open(input_image_path)
# Prompt to colorize the black-and-white image without altering details
text_prompt = (
"Colorize this black and white image while preserving the original structure, "
"details, and proportions exactly. Do not alter faces, objects, or any other "
"elements in the image. Apply realistic and natural colors that match the original scene."
)
# Prepare the input contents: text prompt and image
contents = [text_prompt, image]
# Request the model to generate both text and image output
response = client.models.generate_content(
model="gemini-2.0-flash-exp-image-generation",
contents=contents,
config=types.GenerateContentConfig(
response_modalities=["Text", "Image"]
)
)
# Iterate over the response to handle text and image output
for part in response.candidates[0].content.parts:
if part.text is not None:
print("Text Response:", part.text)
elif part.inline_data is not None:
generated_image = Image.open(BytesIO(part.inline_data.data))
generated_image.save(output_image_path)
print(f"✅ Colored image saved at: {output_image_path}")
if __name__ == "__main__":
generate_colored_image("localpatch/input_image.jpg")
Uses "YOUR_API_KEY"
as a placeholder, making it clear that users need to insert their own credentials.
The file path remains relative (localpatch/input_image.jpg
) for easier integration into an online service.
Hi @user15000,
Yeah, that was related to early access. But, with “gemini-2.0-flash-exp-image-generation” you can generate images.
Can you once check your region is under available region. Error 400 is mostly related to API availability in that region. You can refer this Troubleshooting guide.