結論先說
- 接口切換為統一多模態生成:Imagen 關停後生圖全面併入 Gemini 多模態鏈路,需把
generate_images()改為generate_content()並改填gemini-nano-banana-2.1。 - 顯式聲明
response_modalities:須在GenerateContentConfig傳入response_modalities=["IMAGE"](或["TEXT", "IMAGE"]),否則可能僅回傳文字。 - 安全與人物策略併入標準體系:
safety_filter_level改為safety_settings,person_generation由內置安全護欄接管並在攔截時返回finish_reason。 - 多候選圖改用並發或 Batch API:
generate_content單次返回單路圖,多圖選優可在 Python 用asyncio並發或切至五折 Batch。 - 遷移期消費端配套:聯調期高頻調試提示詞,可看 Gemini Google AI Ultra 5x/20x 充值指南 或 Gemini Google AI Ultra 5x 人工 iOS。

圖:本站根據 Gemini Imagen 遷移說明 與 Nano Banana 2.1 文檔 整理的示意圖
本文根據 Google AI Studio 官方文檔《Gemini Nano Banana 2.1》《Image generation》與《Imagen 遷移說明》整理,資料核對於 2026-12-03。配圖為本站整理的示意圖,不是介面截圖。本站(bigolab.com)由益陽市赫山區必高科技有限公司獨立運營,是獨立第三方服務商,不是 Google 官方網站。不構成執業稅務或投資建議。
適用於誰
- 仍在調用
generate_images與imagen-3.0-*,遇模型關停報錯的後端開發者; - 需把
safety_filter_level、person_generation與number_of_images遷移至新棧的團隊; - 想同步接入
gemini-nano-banana-2.1原生4K輸出與多圖融合的工程負責人。
一、Imagen 與 Nano Banana 2.1 參數映射表
| 遷移項 | 舊版 Imagen | 新版 2.1 | 注意要點 |
|---|---|---|---|
| 方法 | generate_images | generate_content | 卸載舊包 google-generativeai |
| 模型 | imagen-3.0-* | gemini-nano-banana-2.1 | 須綁定 Paid 結算 |
| 模態 | 預設僅出圖 | response_modalities | 設 ["IMAGE"] 或 ["TEXT","IMAGE"] |
| 尺寸 | aspect_ratio | ImageConfig | 支持 1K/2K/4K 與 1:4–8:1 |
| 安全 | safety_filter_level | safety_settings | 映射標準 HarmCategory |
| 人物 | person_generation | 內置安全護欄接管 | 移除舊枚舉防報錯 |
| 取圖 | generated_images | part.as_image() | 加空值守衛與日誌 |
二、安全替換與異步並發遷移代碼
下方 Python 遷移適配器展示了標準 safety_settings 替換、異常 finish_reason 捕獲及用 asyncio 替代 number_of_images 的完整寫法:
import asyncio
import os
from pathlib import Path
from google import genai
from google.genai import types
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
SAFETY_RULES = [
types.SafetySetting(
category=types.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold=types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
),
types.SafetySetting(
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=types.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
),
]
def generate_single_image(prompt: str, aspect_ratio: str, size: str, out_path: Path) -> Path:
response = client.models.generate_content(
model="gemini-nano-banana-2.1",
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(aspect_ratio=aspect_ratio, image_size=size),
safety_settings=SAFETY_RULES,
thinking_config=types.ThinkingConfig(thinking_level="MEDIUM"),
),
)
for part in response.parts or []:
if part.inline_data is not None:
out_path.parent.mkdir(parents=True, exist_ok=True)
part.as_image().save(str(out_path))
return out_path
reason = response.candidates[0].finish_reason if response.candidates else "NO_CANDIDATES"
raise RuntimeError(f"無有效圖像輸出,finish_reason={reason}")
async def generate_variants(prompt: str, count: int = 3) -> list[Path]:
tasks = [
asyncio.to_thread(generate_single_image, prompt, "16:9", "2K", Path(f"migrated/var_{i}.png"))
for i in range(1, count + 1)
]
return await asyncio.gather(*tasks)
人工核對要點:
- 核對舊請求中已移除
person_generation與獨立negative_prompt字段,防參數校驗報錯。 - 核對下游流處理:若上游原本讀取
image_bytes,改取part.inline_data.data。 - 核對並發數與項目級 IPM 限額,遇
429時加指數退避或改走 Batch API。

圖:本站根據 Gemini Image generation 文檔 整理的示意圖
訂閱與開通提示:遷移期配套消費端高額帳號,可選 Gemini Google AI Ultra 5x 人工 iOS 或 Gemini Google AI Ultra 20x 人工 iOS,備號可選 谷歌電郵成品號(質保 3 天首登、封號不在質保範圍,須遵守 Google 服務條款),多廠商並行測試可看 ChatGPT Plus 充值指南 與 全部商品。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費,企業批量採購支持籤中文合同並對公結算。
常見問題
問:舊版 negative_prompt 在 2.1 裏怎麼寫?
答:2.1 不設獨立 negative_prompt,把排除項用清晰正向約束寫進 contents 末尾即可。
問:遷移後出現 finish_reason=IMAGE_OTHER 且圖為空怎麼辦?
答:表示觸發內置肖像或安全策略攔截,檢查提示詞是否含真實公眾人物或敏感符號,改為中性描述重試。
問:一次要出 4 張候選圖,並發調用會不會很慢?
答:用 asyncio.to_thread 並發 4 路耗時接近單張生成;非實時批處理切到 Batch API 還能享五折。
問:沒有外幣卡如何採購配套帳號並辦理 6% 增值稅發票?
答:訪問 AI 訂閱與帳號服務總覽 進入 Google 帳號與訂閱指南 下單即可。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費。本站明示質保訂閱、不質保封號,請嚴格遵守服務商條款合規使用。
相關教學
- Antigravity 報錯 400 怎麼排查
- Antigravity 報錯 User location is not supported for the API use
- Antigravity 2.0 混切 Gemini 4 與 Claude 模型省額度指南
- Antigravity 2.0 桌面端額度消耗對比與團隊採購
0 條評論
還沒有評論,來搶沙發~