* GenerateContentRequest.generation_config.response_mime_type: allowed mimetypes...

2026年01月17日 00:31 processing

错误信息

* GenerateContentRequest.generation_config.response_mime_type: allowed mimetypes are `text/plain`, `application/json`, `application/xml`, `application/yaml` and `text/***.enum`. (request id: 20260117002407173616480WWClXZ77) (request id: 20260117002407172346745o8OhM4S9

AI 解决方案

代码糕手报错分析报告

1. 错误翻译


原文:GenerateContentRequest.generation_config.response_mime_type: allowed mimetypes are `text/plain`, `application/json`, `application/xml`, `application/yaml` and `text/***.enum`.
译文:GenerateContentRequest.generation_config.response_mime_type: 允许的MIME类型是 `text/plain`、`application/json`、`application/xml`、`application/yaml` 和 `text/***.enum`。

2. 原因分析


根本原因:API请求中设置了无效的 response_mime_type 值。

具体分析

  • 这是使用Google Vertex AI的GenerateContent API时发生的错误

  • 在请求的 generation_config.response_mime_type 字段中传入了不在允许列表中的MIME类型

  • 系统期望的是特定的响应格式,但收到了不支持的类型
  • 3. 解决方案

    方案一:设置有效的MIME类型(以JSON格式为例)

    # 使用Google Cloud Vertex AI SDK
    from vertexai.generative_models import GenerativeModel
    
    # 正确的配置示例
    model = GenerativeModel("gemini-pro")
    
    response = model.generate_content(
        "Hello, how are you?",
        generation_config={
            "max_output_tokens": 1000,
            "temperature": 0.2,
            "top_p": 0.95,
            "top_k": 40,
            # 设置有效的response_mime_type
            "response_mime_type": "application/json"  # ✅ 或 "text/plain"、"application/xml"等
        }
    )
    

    方案二:完全移除不必要的配置(如果不需要特定格式)

    response = model.generate_content(
        "Hello, how are you?",
        generation_config={
            "max_output_tokens": 1000,
            "temperature": 0.2
            # 不设置response_mime_type,使用默认值
        }
    )
    

    ️ 4. 预防措施

    最佳实践


  • 枚举值验证:在设置 response_mime_type 前,先验证值是否为以下之一:

  • - text/plain
    - application/json
    - application/xml
    - application/yaml
    - text/.enum(如 text/markdown.enum

    • API文档查阅:使用新API时,先查阅官方文档中的参数约束部分

    工具推荐


  • Postman/Insomnia:使用API测试工具预调试请求参数

  • Google Cloud控制台:使用Vertex AI的测试控制台验证配置

  • 类型检查:在代码中使用枚举类型代替字符串常量
  • ⚡ 快速检查清单


    ✅ 确认response_mime_type值在允许列表中
    ✅ 注意大小写敏感(通常是全小写)
    ✅ 检查JSON格式是否正确,特别是引号的使用
    ✅ 使用IDE的自动补全功能选择正确的枚举值
    


    本次错误是由于传入了无效的MIME类型值导致的,请确保使用允许列表中的值。*