> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aihubmax.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini 格式 - 图像生成

> - 使用 Gemini 原生格式的 generateContent 接口生成图像
- 通过 `generationConfig.responseModalities` 设置为 `["TEXT", "IMAGE"]` 或 `["IMAGE"]` 启用图像输出
- 支持文生图：纯文本 prompt 生成图像
- 支持图生图：上传图片 + 文本 prompt 进行图像编辑
- 支持多轮对话方式迭代修改图像
- 通过 `generationConfig.imageConfig` 控制宽高比和图像分辨率
- 流式调用请使用 `/v1beta/models/{model}:streamGenerateContent?alt=sse`




## OpenAPI

````yaml openapi/zh/gemini-format-image-generation.json POST /v1beta/models/{model}:generateContent
openapi: 3.1.0
info:
  title: Gemini 格式 - 图像生成
  version: '1.0'
servers:
  - url: https://api.aihubmax.com
security:
  - BearerAuth: []
paths:
  /v1beta/models/{model}:generateContent:
    post:
      tags:
        - Text Series > Gemini Format
      summary: Gemini 格式 - 图像生成
      description: >
        - 使用 Gemini 原生格式的 generateContent 接口生成图像

        - 通过 `generationConfig.responseModalities` 设置为 `["TEXT", "IMAGE"]` 或
        `["IMAGE"]` 启用图像输出

        - 支持文生图：纯文本 prompt 生成图像

        - 支持图生图：上传图片 + 文本 prompt 进行图像编辑

        - 支持多轮对话方式迭代修改图像

        - 通过 `generationConfig.imageConfig` 控制宽高比和图像分辨率

        - 流式调用请使用 `/v1beta/models/{model}:streamGenerateContent?alt=sse`
      operationId: gemini-format-image-generation
      parameters:
        - name: model
          in: path
          required: true
          schema:
            type: string
            example: gemini-3.1-flash-image-preview
            enum:
              - gemini-3-pro-image-preview
              - gemini-3.1-flash-image-preview
          description: >-
            模型名称。支持 `gemini-3-pro-image-preview`（专业素材制作，高级推理）和
            `gemini-3.1-flash-image-preview`（高效率，针对速度优化）
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ImageGenerationRequest'
            examples:
              text-to-image:
                summary: 文生图（基础）
                description: 纯文本 prompt 生成图像，默认返回文本 + 图像
                value:
                  contents:
                    - role: user
                      parts:
                        - text: 生成一张日落时分海边灯塔的水彩画
                  generationConfig:
                    responseModalities:
                      - TEXT
                      - IMAGE
              image-only:
                summary: 文生图（仅图像）
                description: 设置 responseModalities 为 ["IMAGE"] 仅返回图像
                value:
                  contents:
                    - role: user
                      parts:
                        - text: 一只柯基犬穿着宇航服在月球上散步，卡通风格
                  generationConfig:
                    responseModalities:
                      - IMAGE
              with-aspect-ratio:
                summary: 自定义宽高比和分辨率
                description: 通过 imageConfig 设置宽高比和输出分辨率
                value:
                  contents:
                    - role: user
                      parts:
                        - text: 设计一张现代简约风格的咖啡店菜单海报
                  generationConfig:
                    responseModalities:
                      - TEXT
                      - IMAGE
                    imageConfig:
                      aspectRatio: '9:16'
                      imageSize: 2K
              image-editing:
                summary: 图像编辑（图生图）
                description: 上传图片 + 文本指令，对图像进行编辑修改
                value:
                  contents:
                    - role: user
                      parts:
                        - text: 把这张照片中的天空改成星空效果
                        - inlineData:
                            mimeType: image/jpeg
                            data: BASE64_ENCODED_IMAGE_DATA
                  generationConfig:
                    responseModalities:
                      - TEXT
                      - IMAGE
      responses:
        '200':
          $ref: '#/components/responses/ImageGenerationSuccess'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '402':
          $ref: '#/components/responses/PaymentRequired'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/InternalServerError'
components:
  schemas:
    ImageGenerationRequest:
      type: object
      required:
        - contents
        - generationConfig
      properties:
        contents:
          type: array
          description: 对话内容列表
          items:
            type: object
            required:
              - role
              - parts
            properties:
              role:
                type: string
                description: 消息发送者的角色
                enum:
                  - user
                  - model
              parts:
                type: array
                description: 内容部分列表，可包含文本和图片
                items:
                  type: object
                  properties:
                    text:
                      type: string
                      description: 文本内容（图像生成 prompt 或编辑指令）
                    inlineData:
                      type: object
                      description: 内联数据（用于图像编辑场景，上传待编辑的图片）
                      required:
                        - mimeType
                        - data
                      properties:
                        mimeType:
                          type: string
                          description: MIME 类型
                          enum:
                            - image/png
                            - image/jpeg
                            - image/webp
                        data:
                          type: string
                          description: Base64 编码的图片数据
                    fileData:
                      type: object
                      description: URL 引用。通过公开 HTTPS URL 提供参考图片（免去 base64 编码开销）。
                      required:
                        - fileUri
                      properties:
                        fileUri:
                          type: string
                          description: 图片的公开 HTTPS URL
                          examples:
                            - https://example.com/photo.jpg
                        mimeType:
                          type: string
                          description: 图片 MIME 类型
                          enum:
                            - image/png
                            - image/jpeg
                            - image/webp
                  description: >-
                    内容片段：text（文本 prompt）/ inlineData（base64 图片）/ fileData（图片
                    URL）。


                    图像生成场景下输入仅支持文本和图片类型。
        systemInstruction:
          type: object
          description: 系统指令
          properties:
            parts:
              type: array
              description: 系统指令内容部分
              items:
                type: object
                properties:
                  text:
                    type: string
                    description: 系统指令文本
        generationConfig:
          type: object
          description: 生成配置（图像生成必须设置 responseModalities）
          required:
            - responseModalities
          properties:
            responseModalities:
              type: array
              description: 响应模态。设置为 `["TEXT", "IMAGE"]` 返回文本 + 图像（默认），或 `["IMAGE"]` 仅返回图像
              items:
                type: string
                enum:
                  - TEXT
                  - IMAGE
              example:
                - TEXT
                - IMAGE
            temperature:
              type: number
              description: 采样温度
              minimum: 0
              maximum: 2
            imageConfig:
              type: object
              description: 图像输出配置
              properties:
                aspectRatio:
                  type: string
                  description: 输出图像宽高比。默认为 1:1
                  enum:
                    - '1:1'
                    - '1:4'
                    - '1:8'
                    - '2:3'
                    - '3:2'
                    - '3:4'
                    - '4:1'
                    - '4:3'
                    - '4:5'
                    - '5:4'
                    - '8:1'
                    - '9:16'
                    - '16:9'
                    - '21:9'
                  example: '16:9'
                imageSize:
                  type: string
                  description: 输出图像分辨率。默认为 1K。必须使用大写 K（如 1K、2K、4K），512 不带 K 后缀
                  enum:
                    - '512'
                    - 1K
                    - 2K
                    - 4K
                  example: 1K
        safetySettings:
          type: array
          description: 安全设置
          items:
            type: object
            properties:
              category:
                type: string
                description: 安全类别
                enum:
                  - HARM_CATEGORY_HARASSMENT
                  - HARM_CATEGORY_HATE_SPEECH
                  - HARM_CATEGORY_SEXUALLY_EXPLICIT
                  - HARM_CATEGORY_DANGEROUS_CONTENT
              threshold:
                type: string
                description: 安全阈值
                enum:
                  - BLOCK_NONE
                  - BLOCK_LOW_AND_ABOVE
                  - BLOCK_MEDIUM_AND_ABOVE
                  - BLOCK_ONLY_HIGH
    ImageGenerationResponse:
      type: object
      properties:
        candidates:
          type: array
          description: 候选结果列表
          items:
            type: object
            properties:
              content:
                type: object
                description: 生成的内容
                properties:
                  parts:
                    type: array
                    description: 内容部分列表，可能同时包含文本和图像
                    items:
                      type: object
                      properties:
                        text:
                          type: string
                          description: 生成的文本内容（当 responseModalities 包含 TEXT 时）
                          example: 这是一张日落时分海边灯塔的水彩画。
                        inlineData:
                          type: object
                          description: 生成的图像数据
                          properties:
                            mimeType:
                              type: string
                              description: 图像 MIME 类型
                              example: image/png
                            data:
                              type: string
                              description: Base64 编码的图像数据
                              example: iVBORw0KGgoAAAANSUhEUgAA...
                  role:
                    type: string
                    description: 角色
                    enum:
                      - model
              finishReason:
                type: string
                description: 生成停止原因
                enum:
                  - STOP
                  - MAX_TOKENS
                  - SAFETY
                  - RECITATION
                  - OTHER
                example: STOP
              safetyRatings:
                type: array
                description: 安全评分列表
                items:
                  type: object
                  properties:
                    category:
                      type: string
                      description: 安全类别
                    probability:
                      type: string
                      description: 概率等级
        usageMetadata:
          type: object
          description: 用量统计
          properties:
            promptTokenCount:
              type: integer
              description: 提示 token 数
              example: 15
            candidatesTokenCount:
              type: integer
              description: 候选 token 数（包含图像 token）
              example: 1120
            totalTokenCount:
              type: integer
              description: 总 token 数
              example: 1135
    ErrorResponse400:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 请求参数无效
            type:
              type: string
              example: invalid_request_error
    ErrorResponse401:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: API密钥无效
            type:
              type: string
              example: authentication_error
    ErrorResponse403:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 访问被拒绝
            type:
              type: string
              example: permission_error
    ErrorResponse429:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 请求频率超限
            type:
              type: string
              example: rate_limit_error
    ErrorResponse500:
      type: object
      properties:
        error:
          type: object
          properties:
            message:
              type: string
              example: 服务器内部错误
            type:
              type: string
              example: server_error
  responses:
    ImageGenerationSuccess:
      description: 图像生成响应
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ImageGenerationResponse'
    BadRequest:
      description: 请求错误
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse400'
    Unauthorized:
      description: 未授权
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse401'
    PaymentRequired:
      description: 余额不足
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse400'
    Forbidden:
      description: 禁止访问
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse403'
    TooManyRequests:
      description: 请求过多
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse429'
    InternalServerError:
      description: 服务器内部错误
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse500'
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: |
        ## 所有接口均需要使用Bearer Token进行认证 ##

        使用时在请求头中添加：

        `Authorization: Bearer YOUR_API_KEY`

````