Fix for Gemini Image Generation using Nano Banana

https://ai.google.dev/gemini-api/docs/image-generation?authuser=1#rest
This commit is contained in:
Harsh Raj 2025-09-14 14:23:18 +05:30 committed by GitHub
parent 171021cfa4
commit ac045ee2d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 8 deletions

View File

@ -586,17 +586,15 @@ async def image_generations(
model = get_image_model(request) model = get_image_model(request)
data = { data = {
"instances": {"prompt": form_data.prompt}, "prompt": {"text": form_data.prompt},
"parameters": { "samples": form_data.n
"sampleCount": form_data.n,
"outputOptions": {"mimeType": "image/png"},
},
} }
# Use asyncio.to_thread for the requests.post call # Use asyncio.to_thread for the requests.post call
r = await asyncio.to_thread( r = await asyncio.to_thread(
requests.post, requests.post,
url=f"{request.app.state.config.IMAGES_GEMINI_API_BASE_URL}/models/{model}:predict", url=f"{request.app.state.config.IMAGES_GEMINI_API_BASE_URL}/models/{model}:generateImage?key={request.app.state.config.IMAGES_GEMINI_API_KEY}",
json=data, json=data,
headers=headers, headers=headers,
) )
@ -605,9 +603,9 @@ async def image_generations(
res = r.json() res = r.json()
images = [] images = []
for image in res["predictions"]: for image in res.get("images", []):
image_data, content_type = load_b64_image_data( image_data, content_type = load_b64_image_data(
image["bytesBase64Encoded"] image["imageBytes"]
) )
url = upload_image(request, image_data, content_type, data, user) url = upload_image(request, image_data, content_type, data, user)
images.append({"url": url}) images.append({"url": url})