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

# Gemini streamGenerateContent

> 原生 SSE 文本与函数工具调用。

`POST /v1beta/models/{model}:streamGenerateContent?alt=sse`

使用 `x-goog-api-key` 发送网关令牌，查询参数 `alt=sse` 选择 SSE。请求结构与非流式 generateContent 相同，路径动作不同；不要在请求体中添加 Chat 的 `stream` 代替它。

每个 SSE data 包含 Gemini 响应。文本和 `functionCall` 位于 `candidates[].content.parts`；检查 `promptFeedback`、候选索引和最终 `finishReason`，不能将 EOF 单独视为成功。工具定义见 `tools[].functionDeclarations`，结果通过 `functionResponse` 回传。

[完整 Python SSE 与工具闭环示例](/zh/guides/native-gemini-streaming)包含两个运行模式、签名保留、可选 ID 关联与错误处理。规范只展开常用字段；不承诺 Live API、细粒度参数扩展或所有渠道均可用。

[Read in English](/en/api-reference/gemini-stream)

## 媒体内容项

原生 `contents[].parts[]` 可以包含 `inlineData: {mimeType, data}` 或 `fileData: {mimeType, fileUri}`，前者使用纯 Base64；内容格式和来源必须受目标上游支持。`videoMetadata`、`mediaResolution` 是条件支持的原生扩展，不是网关统一转码或抽帧服务。

媒体输入不改变响应协议和完成检查。网关没有在已核查范围内提供 Gemini Files 上传/管理或 Interactions 端点；SDK 自动上传会产生额外的未支持请求。先使用 [视频理解的完整非流式示例](/zh/guides/video-understanding)，流式输出仍按本接口 SSE 事件处理。

## SSE 内置工具元数据

请求接受相同的有条件原生 `googleSearch`、`urlContext`、`codeExecution` 工具字段。原生 SSE 转发上游 JSON 事件。跨事件保留候选元数据和代码/结果 part，不要把全部事件扁平化成文本，也不要假设每个事件都有完整引用记录。已有文本/函数 SSE 示例没有实现这些内置工具消费者。先使用[非流式内置工具示例](/zh/guides/gemini-builtins)；增加流式处理前需按原生协议累积字节偏移与来源关联。


## OpenAPI

````yaml openapi/relay.json POST /v1beta/models/{model}:streamGenerateContent
openapi: 3.0.3
info:
  title: API — Core inference API
  version: 0.5.0
  description: >-
    面向接入的精选规范，不是全部管理或供应商接口。 / Curated integration specification, not the entire
    management or provider API.
  license:
    name: AGPL-3.0-or-later
servers:
  - url: https://gateway.example.com
    description: >-
      Example only; replace with your gateway origin and optional path prefix,
      without /v1.
security:
  - BearerAuth: []
paths:
  /v1beta/models/{model}:streamGenerateContent:
    post:
      summary: Gemini 原生流式生成 / Native Gemini streaming generation
      description: >-
        原生 SSE；每个 data 包含 Gemini 响应对象。完成状态与工具处理见指南。 / Native SSE; each data
        payload is a Gemini response object. See the guide for completion and
        tool handling.
      operationId: streamGeminiContent
      parameters:
        - name: model
          in: path
          required: true
          description: >-
            裸模型 ID，不含 models/ 和动作后缀 / Bare model ID without models/ or an action
            suffix.
          schema:
            type: string
            example: YOUR_MODEL_ID
        - name: alt
          in: query
          required: true
          schema:
            type: string
            enum:
              - sse
          description: 选择 SSE 传输 / Select SSE transport.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - contents
              properties:
                contents:
                  type: array
                  minItems: 1
                  items:
                    type: object
                    properties:
                      role:
                        type: string
                      parts:
                        type: array
                        items:
                          type: object
                          properties:
                            text:
                              type: string
                            thought:
                              type: boolean
                            thoughtSignature:
                              type: string
                            functionCall:
                              type: object
                              properties:
                                id:
                                  type: string
                                name:
                                  type: string
                                args:
                                  type: object
                                  additionalProperties: true
                              additionalProperties: true
                            functionResponse:
                              type: object
                              properties:
                                id:
                                  type: string
                                name:
                                  type: string
                                response:
                                  type: object
                                  additionalProperties: true
                              additionalProperties: true
                            inlineData:
                              type: object
                              required:
                                - mimeType
                                - data
                              properties:
                                mimeType:
                                  type: string
                                  example: video/mp4
                                data:
                                  type: string
                                  description: >-
                                    纯 Base64，不带 Data URL 前缀 / Plain Base64, no
                                    Data URL prefix
                              description: >-
                                原生内联媒体，格式与大小依模型 / Native inline media;
                                model-dependent formats and limits
                            fileData:
                              type: object
                              properties:
                                mimeType:
                                  type: string
                                fileUri:
                                  type: string
                              description: >-
                                上游可访问的 URI；不是网关上传服务 / Upstream-accessible URI,
                                not a gateway upload service
                            videoMetadata:
                              type: object
                              additionalProperties: true
                              description: >-
                                模型依赖的原生扩展；请核对实际上游结构 / Model-dependent native
                                extension; verify the upstream structure
                            mediaResolution:
                              type: object
                              additionalProperties: true
                              description: >-
                                模型依赖的原生扩展；请核对实际上游结构 / Model-dependent native
                                extension; verify the upstream structure
                            executableCode:
                              type: object
                              properties:
                                language:
                                  type: string
                                  example: PYTHON
                                code:
                                  type: string
                              additionalProperties: true
                            codeExecutionResult:
                              type: object
                              properties:
                                outcome:
                                  type: string
                                  description: 检查 OUTCOME_OK / Check OUTCOME_OK
                                output:
                                  type: string
                              additionalProperties: true
                          additionalProperties: true
                    additionalProperties: true
                systemInstruction:
                  type: object
                  properties:
                    role:
                      type: string
                    parts:
                      type: array
                      items:
                        type: object
                        properties:
                          text:
                            type: string
                          thought:
                            type: boolean
                          thoughtSignature:
                            type: string
                          functionCall:
                            type: object
                            properties:
                              id:
                                type: string
                              name:
                                type: string
                              args:
                                type: object
                                additionalProperties: true
                            additionalProperties: true
                          functionResponse:
                            type: object
                            properties:
                              id:
                                type: string
                              name:
                                type: string
                              response:
                                type: object
                                additionalProperties: true
                            additionalProperties: true
                        additionalProperties: true
                  additionalProperties: true
                tools:
                  type: array
                  items:
                    type: object
                    properties:
                      functionDeclarations:
                        type: array
                        items:
                          type: object
                          required:
                            - name
                          properties:
                            name:
                              type: string
                            description:
                              type: string
                            parameters:
                              type: object
                              additionalProperties: true
                              description: >-
                                工具参数 Schema；示例额外执行本地校验 / Tool parameter schema;
                                examples also validate locally.
                          additionalProperties: true
                      googleSearch:
                        type: object
                        additionalProperties: true
                        description: >-
                          以空对象启用；依赖原生模型及渠道支持 / Enable with an empty object;
                          native model and channel support required
                        example: {}
                      urlContext:
                        type: object
                        additionalProperties: true
                        description: >-
                          以空对象启用；依赖原生模型及渠道支持 / Enable with an empty object;
                          native model and channel support required
                        example: {}
                      codeExecution:
                        type: object
                        additionalProperties: true
                        description: >-
                          以空对象启用；依赖原生模型及渠道支持 / Enable with an empty object;
                          native model and channel support required
                        example: {}
                    additionalProperties: true
              additionalProperties: true
      responses:
        '200':
          description: >-
            检查候选结束状态并处理流内错误 / Validate candidate finish status and in-stream
            errors.
          content:
            text/event-stream:
              schema:
                type: string
        default:
          description: >-
            网关或上游错误，结构可能随协议变化 / Gateway or upstream error; structure may vary by
            protocol.
          content:
            application/json:
              schema:
                type: object
                additionalProperties: true
      security:
        - GeminiApiKey: []
        - BearerAuth: []
components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      description: 模型调用 API Key，不是控制台登录凭据 / Model API key, not console login credentials
    GeminiApiKey:
      type: apiKey
      in: header
      name: x-goog-api-key
      description: 网关令牌 / Gateway token

````

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