block_low_and_above | AI生成和翻译

Home 2025.08

您遇到的错误提示:

Only block_low_and_above is supported for safetySetting.

这是因为您在为 Imagen-4 模型配置 GenerateImagesConfig 时设置了 safety_filter_level="BLOCK_MEDIUM_AND_ABOVE"(或其他值)——但在您请求时(2025年8月),该模型的图像安全过滤仅支持 "BLOCK_LOW_AND_ABOVE" (Google Cloud)。


✅ 修复代码的方法

只需将 safety_filter_level 更新为支持的值:

from google import genai
from google.genai.types import GenerateImagesConfig

client = genai.Client()

image = client.models.generate_images(
    model="imagen-4.0-generate-preview-06-06",
    prompt="一只正在读报纸的狗",
    config=GenerateImagesConfig(
        image_size="2K",
        number_of_images=1,
        safety_filter_level="BLOCK_LOW_AND_ABOVE",
        person_generation="ALLOW_ADULT",
    ),
)

image.generated_images[0].image.save("output-image.png")
print(
    f"创建图像使用了 {len(image.generated_images[0].image.image_bytes)} 字节"
)

🧾 其他可选参数:


TL;DR — 快速检查清单

步骤 操作说明
✅ 1 将 safety_filter_level 改为 "BLOCK_LOW_AND_ABOVE"
🧾 2 保持 person_generation="ALLOW_ADULT"(或省略使用默认值)
🚫 3 不要使用 "BLOCK_MEDIUM_AND_ABOVE""BLOCK_ONLY_HIGH"
⏱ 4 在 Google 添加更多级别前不要更改安全设置

如果遇到其他错误请随时告知——很乐意为您提供帮助!


Back Donate