": { "code": 400, "message": "Request contains an invalid argument.", "status": ...

2025年12月04日 15:11 processing

错误信息

curl --location 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent?key=AIzaSyC2LeIkBMb4aGvasrOBR1hG5yMQ0vUJm64' \ --header 'Content-Type: application/json' \ --data '{ "contents": [ { "role": "user", "parts": [ { "text": "draw a cat" } ] } ], "generationConfig": { "responseModalities": [ "TEXT", "IMAGE" ], "imageConfig": { "aspectRatio": "16:9", "imageSize": "1K" } } }' { "error": { "code": 400, "message": "Request contains an invalid argument.", "status": "INVALID_ARGUMENT" } }

AI 解决方案

报错分析报告

1. 错误翻译


原文:Request contains an invalid argument.
译文:请求包含无效参数。

2. 原因分析


该错误发生在调用Google Gemini API时,主要原因是:
  • API参数配置错误:在generationConfig中同时请求了TEXTIMAGE输出模式

  • 模型限制gemini-2.5-flash-image模型不支持同时生成文本和图像

  • 参数冲突responseModalities配置与模型能力不匹配
  • 3. 解决方案

    方案一:仅生成文本

    {
        "contents": [
          {
            "role": "user",
            "parts": [
              {
                "text": "draw a cat"
              }
            ]
          }
        ],
        "generationConfig": {
          "responseModalities": [
            "TEXT"
          ]
        }
    }
    

    方案二:仅生成图像(如果模型支持)

    {
        "contents": [
          {
            "role": "user",
            "parts": [
              {
                "text": "draw a cat"
              }
            ]
          }
        ],
        "generationConfig": {
          "responseModalities": [
            "IMAGE"
          ],
          "imageConfig": {
            "aspectRatio": "16:9",
            "imageSize": "1K"
          }
        }
    }
    

    4. ️ 预防措施


  • 查阅官方文档:使用API前仔细阅读模型的输入输出限制和要求

  • 参数验证:在发送请求前,使用API Playground或测试工具验证参数配置

  • 渐进式开发:先使用基本配置测试成功,再逐步添加高级功能


  • 提示:建议查阅Google Gemini API官方文档,确认gemini-2.5-flash-image模型支持的具体功能。