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

# 流式生成内容

该接口使用与[生成内容](https://docs.geekai.co/cn/api/gemini.md)相同的请求格式，逐步返回模型的生成结果。设置 `alt=sse` 可通过 Server-Sent Events 接收响应，每个 `data` 事件包含一个 JSON 响应片段。

调用时在 `x-goog-api-key` 请求头中填写[极客智坊 API KEY](https://geekai.co/user/api_keys)，并将 `{model}` 替换为 Gemini 模型 ID。具体功能取决于模型和代理渠道，详细格式请参阅 [Gemini 官方流式接口文档](https://aistudio.google.com/api-reference/generate-content/stream-generate-content.md)。

```bash theme={null}
curl --no-buffer 'https://geekai.co/api/v1beta/models/gemini-3-flash-preview:streamGenerateContent?alt=sse' \
  -H "x-goog-api-key: $GEEKAI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"contents":[{"parts":[{"text":"你好，请介绍一下自己。"}]}]}'
```


## OpenAPI

````yaml openapi_gemini.yaml POST /models/{model}:streamGenerateContent
openapi: 3.1.0
info:
  title: 极客智坊 Gemini GenerateContent API
  version: 1.0.0
  description: Gemini 原生内容生成接口。模型及代理渠道支持的功能可能不同。
servers:
  - url: https://geekai.co/api/v1beta
    description: 国内代理入口
  - url: https://geekai.dev/api/v1beta
    description: 国际代理入口
security:
  - bearerAuth: []
  - apiKeyAuth: []
paths:
  /models/{model}:streamGenerateContent:
    post:
      summary: 流式生成内容
      description: >
        使用与 generateContent 相同的请求格式，逐步返回模型生成的结果。

        设置 alt=sse 可通过 Server-Sent Events 接收响应，每个 data 事件包含一个
        GenerateContentResponse JSON 对象。

        响应片段可能只包含部分字段，客户端应依次处理片段；模型及渠道对功能的支持情况可能不同。
      operationId: streamGenerateContent
      parameters:
        - name: model
          in: path
          required: true
          description: Gemini 模型 ID，不包含 models/ 前缀。
          schema:
            type: string
            example: gemini-3-flash-preview
        - name: alt
          in: query
          required: false
          description: 设置为 sse 时以 Server-Sent Events 格式返回流式响应。
          schema:
            type: string
            example: sse
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GenerateContentRequest'
            example:
              contents:
                - role: user
                  parts:
                    - text: 你好，请介绍一下自己。
      responses:
        '200':
          description: 流式生成结果；使用 alt=sse 时，data 事件包含 JSON 格式的响应片段。
          content:
            text/event-stream:
              schema:
                type: string
                description: SSE 事件流，事件之间以空行分隔。
              x-event-schema:
                $ref: '#/components/schemas/GenerateContentResponse'
              example: >+
                data:
                {"candidates":[{"content":{"role":"model","parts":[{"text":"你好！"}]},"index":0}]}


                data:
                {"candidates":[{"content":{"role":"model","parts":[{"text":"我是
                Gemini。"}]},"finishReason":"STOP","index":0}]}

        default:
          description: 请求失败。请根据 HTTP 状态码和错误详情排查。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
      externalDocs:
        description: Gemini 官方流式生成内容接口文档
        url: >-
          https://aistudio.google.com/api-reference/generate-content/stream-generate-content.md
components:
  schemas:
    GenerateContentRequest:
      type: object
      required:
        - contents
      properties:
        contents:
          type: array
          description: 当前请求的对话内容；多轮对话需包含历史消息和最新消息。
          items:
            $ref: '#/components/schemas/Content'
        systemInstruction:
          $ref: '#/components/schemas/Content'
          description: 开发者提供的系统指令，目前仅支持文本。
        tools:
          type: array
          description: 模型可使用的工具；可用工具取决于模型和渠道。
          items:
            $ref: '#/components/schemas/Tool'
        toolConfig:
          $ref: '#/components/schemas/ToolConfig'
        safetySettings:
          type: array
          description: 按类别覆盖默认安全设置；同一类别不能重复。
          items:
            $ref: '#/components/schemas/SafetySetting'
        generationConfig:
          $ref: '#/components/schemas/GenerationConfig'
        cachedContent:
          type: string
          description: 缓存内容资源名称，格式为 cachedContents/{cachedContent}。
        serviceTier:
          type: string
          description: 请求的服务等级，是否可用取决于模型和渠道。
          enum:
            - unspecified
            - standard
            - flex
            - priority
        store:
          type: boolean
          description: 此请求的日志记录配置，设置时覆盖项目级配置。
        continuationToken:
          type: string
          description: 用于继续 finishReason 为 CONTINUATION 的响应；contents 必须与原请求一致。
        labels:
          type: object
          description: 用户定义的请求标签。
          additionalProperties:
            type: string
    ErrorResponse:
      type: object
      properties:
        error:
          type: object
          properties:
            code:
              type: integer
            message:
              type: string
            status:
              type: string
            details:
              type: array
              items:
                type: object
    Content:
      type: object
      description: 一轮消息，由角色和按顺序排列的内容片段组成。
      properties:
        role:
          type: string
          description: 消息来源，通常为 user 或 model。
        parts:
          type: array
          items:
            $ref: '#/components/schemas/Part'
    Tool:
      type: object
      description: 工具定义。不同工具的可用性以模型和渠道为准。
      properties:
        functionDeclarations:
          type: array
          description: 可由模型请求调用的客户端函数。
          items:
            $ref: '#/components/schemas/FunctionDeclaration'
        codeExecution:
          type: object
          description: 启用代码执行。
        googleSearch:
          type: object
          description: Google 搜索工具配置。
        googleSearchRetrieval:
          type: object
          description: Google 搜索检索配置。
        urlContext:
          type: object
          description: URL 上下文检索配置。
        fileSearch:
          type: object
          description: 文件搜索配置。
          properties:
            fileSearchStoreNames:
              type: array
              items:
                type: string
            topK:
              type: integer
            metadataFilter:
              type: string
        computerUse:
          type: object
          description: 计算机操作工具配置。
        googleMaps:
          type: object
          description: Google Maps 工具配置。
        mcpServers:
          type: array
          description: MCP 服务配置。
          items:
            type: object
    ToolConfig:
      type: object
      properties:
        functionCallingConfig:
          type: object
          description: 函数调用模式和允许调用的函数。
          properties:
            mode:
              type: string
              enum:
                - MODE_UNSPECIFIED
                - AUTO
                - ANY
                - NONE
                - VALIDATED
              description: 默认 AUTO；ANY 要求调用函数，NONE 禁止调用函数。
            allowedFunctionNames:
              type: array
              description: 限制可调用的函数名称，仅用于 ANY 或 VALIDATED 模式。
              items:
                type: string
        retrievalConfig:
          type: object
          description: 检索配置。
          properties:
            latLng:
              type: object
              properties:
                latitude:
                  type: number
                  minimum: -90
                  maximum: 90
                longitude:
                  type: number
                  minimum: -180
                  maximum: 180
            languageCode:
              type: string
              description: BCP 47 语言代码。
        includeServerSideToolInvocations:
          type: boolean
          description: 是否在响应内容中包含服务端工具调用和结果。
    SafetySetting:
      type: object
      required:
        - category
        - threshold
      properties:
        category:
          type: string
          description: 安全类别，例如 HARM_CATEGORY_HARASSMENT。
        threshold:
          type: string
          description: 对应类别的拦截阈值。
          enum:
            - HARM_BLOCK_THRESHOLD_UNSPECIFIED
            - BLOCK_LOW_AND_ABOVE
            - BLOCK_MEDIUM_AND_ABOVE
            - BLOCK_ONLY_HIGH
            - BLOCK_NONE
            - 'OFF'
    GenerationConfig:
      type: object
      description: 生成参数。默认值和可用范围可能因模型而异。
      properties:
        candidateCount:
          type: integer
          description: 返回的候选结果数量，默认 1。
        stopSequences:
          type: array
          description: 停止生成的字符串，最多 5 个；结果不包含匹配到的停止字符串。
          maxItems: 5
          items:
            type: string
        maxOutputTokens:
          type: integer
          description: 每个候选结果的最大输出 token 数。
        temperature:
          type: number
          minimum: 0
          maximum: 2
          description: 控制输出随机性；Gemini 3.6 Flash 及更新模型会忽略此参数。
        topP:
          type: number
          description: 核采样的累计概率阈值；Gemini 3.6 Flash 及更新模型会忽略此参数。
        topK:
          type: integer
          description: 采样时考虑的最大候选 token 数；部分模型不支持，Gemini 3.6 Flash 及更新模型会忽略此参数。
        seed:
          type: integer
          description: 解码随机种子。
        responseMimeType:
          type: string
          description: 输出文本的 MIME 类型，如 text/plain、application/json 或 text/x.enum。
        responseSchema:
          type: object
          description: 输出结构的 Gemini Schema；与 responseJsonSchema 互斥。
        responseJsonSchema:
          description: 输出结构的 JSON Schema；与 responseSchema 互斥。
        presencePenalty:
          type: number
          description: 基于 token 是否已出现调整其生成概率。
        frequencyPenalty:
          type: number
          description: 根据 token 出现次数调整其生成概率。
        responseLogprobs:
          type: boolean
          description: 是否返回 token 对数概率。
        logprobs:
          type: integer
          minimum: 0
          maximum: 20
          description: 返回的候选 token 对数概率数量；需启用 responseLogprobs。
        enableEnhancedCivicAnswers:
          type: boolean
          description: 启用增强的公民信息回答，可能并非所有模型都支持。
        responseModalities:
          type: array
          description: 请求的输出模态，支持的组合取决于模型。
          items:
            type: string
            enum:
              - MODALITY_UNSPECIFIED
              - TEXT
              - IMAGE
              - AUDIO
        speechConfig:
          type: object
          description: 语音生成配置。
        thinkingConfig:
          type: object
          description: 思考配置；不支持思考的模型设置此字段会报错。
          properties:
            includeThoughts:
              type: boolean
              description: 是否在可用时返回思考内容。
            thinkingBudget:
              type: integer
              description: 思考 token 预算。
            thinkingLevel:
              type: string
              enum:
                - THINKING_LEVEL_UNSPECIFIED
                - MINIMAL
                - LOW
                - MEDIUM
                - HIGH
                - XHIGH
              description: 思考深度；适用于 Gemini 3 或更新模型。
        imageConfig:
          type: object
          description: 图片生成配置，支持情况取决于模型。
          properties:
            aspectRatio:
              type: string
              description: 图片宽高比，如 1:1 或 16:9。
            imageSize:
              type: string
              description: 图片尺寸，如 512、1K、2K 或 4K。
        mediaResolution:
          type: string
          enum:
            - MEDIA_RESOLUTION_UNSPECIFIED
            - MEDIA_RESOLUTION_LOW
            - MEDIA_RESOLUTION_MEDIUM
            - MEDIA_RESOLUTION_HIGH
          description: 输入媒体的处理分辨率。
        enableAffectiveDialog:
          type: boolean
          description: 是否根据检测到的情绪调整回复。
        responseFormat:
          type: object
          description: 按文本、音频或图片模态配置输出格式。
        translationConfig:
          type: object
          description: 翻译配置。
          properties:
            targetLanguageCode:
              type: string
              description: 目标语言的 BCP 47 代码。
            echoTargetLanguage:
              type: boolean
        audioTranscriptionConfig:
          type: object
          description: 音频转写配置。
    Part:
      type: object
      description: 内容片段；text、inlineData、fileData、functionCall 等数据字段互斥。
      properties:
        text:
          type: string
          description: 文本内容。
        inlineData:
          $ref: '#/components/schemas/Blob'
        fileData:
          $ref: '#/components/schemas/FileData'
        functionCall:
          $ref: '#/components/schemas/FunctionCall'
        functionResponse:
          $ref: '#/components/schemas/FunctionResponse'
        executableCode:
          type: object
          description: 模型生成的可执行代码。
          properties:
            language:
              type: string
            code:
              type: string
        codeExecutionResult:
          type: object
          description: 代码执行结果。
          properties:
            outcome:
              type: string
            output:
              type: string
        thought:
          type: boolean
          description: 标识此片段是否为模型思考内容。
        thoughtSignature:
          type: string
          description: Base64 编码的思考签名；后续请求可能需要原样传回。
        videoMetadata:
          type: object
          description: 视频数据的处理配置，仅适用于视频片段。
          properties:
            startOffset:
              type: string
              description: 视频起始偏移，如 1.5s。
            endOffset:
              type: string
              description: 视频结束偏移。
            fps:
              type: number
              description: 视频采样帧率。
    FunctionDeclaration:
      type: object
      required:
        - name
        - description
      properties:
        name:
          type: string
          description: 函数名称。
        description:
          type: string
          description: 函数用途。
        parameters:
          type: object
          description: 函数参数的 Gemini Schema，与 parametersJsonSchema 互斥。
        parametersJsonSchema:
          type: object
          description: 函数参数的 JSON Schema，与 parameters 互斥。
        response:
          type: object
          description: 函数返回值的 Gemini Schema，与 responseJsonSchema 互斥。
        responseJsonSchema:
          type: object
          description: 函数返回值的 JSON Schema，与 response 互斥。
    Blob:
      type: object
      description: 内联多模态数据。
      properties:
        mimeType:
          type: string
          description: 数据的 MIME 类型。
          example: image/png
        data:
          type: string
          description: Base64 编码的数据。
        displayName:
          type: string
          description: 数据的显示名称。
    FileData:
      type: object
      description: 通过 URI 引用的文件。
      properties:
        mimeType:
          type: string
          description: 文件的 MIME 类型。
        fileUri:
          type: string
          description: 文件 URI。
        displayName:
          type: string
          description: 文件的显示名称。
    FunctionCall:
      type: object
      required:
        - name
      description: 模型生成的函数调用，函数由客户端执行。
      properties:
        name:
          type: string
          description: 函数名称。
        id:
          type: string
          description: 调用标识，用于匹配函数响应。
        args:
          type: object
          description: 函数参数。
          additionalProperties: true
    FunctionResponse:
      type: object
      required:
        - name
        - response
      description: 客户端执行函数后的结果。
      properties:
        name:
          type: string
          description: 函数名称。
        id:
          type: string
          description: 对应的函数调用标识。
        response:
          type: object
          description: 函数执行结果；发生错误时可包含 error 字段。
          additionalProperties: true
        parts:
          type: array
          description: 函数返回的多模态内容片段。
          items:
            type: object
            properties:
              inlineData:
                $ref: '#/components/schemas/Blob'
              fileData:
                $ref: '#/components/schemas/FileData'
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: 极客智坊 API KEY
    apiKeyAuth:
      type: apiKey
      in: header
      name: x-goog-api-key
      description: 极客智坊 API KEY

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.