コンテンツへスキップ
APIリファレンス
APIリファレンス

Gemini Stream Generate Content

RunAPIのGemini互換Stream Generate Contentオペレーションを通じてリクエストを送信します。

01

概要

RunAPIのGemini互換のStream Generate Content操作を、互換性のあるモデルおよびプロトコルのリクエスト・レスポンス形式とともに使用してください。

クイックスタート

  1. APIキーを作成し、RUNAPI_API_KEYとして設定します。
  2. 互換性のあるモデルを選択し、ドキュメントに記載されたパスパラメーターとJSONボディを送信します。
  3. このオペレーションに示された JSON レスポンスまたはサーバー送信イベントを読み取り、プロトコルエラーを処理してください。

エンドポイント

POST /v1beta/models/{model}:streamGenerateContent?alt=sse
ベースURL
https://runapi.ai
コントラクトバージョン
v1
推奨認証方式
x-goog-api-key: YOUR_API_TOKEN
02

認証情報の送信方法

RunAPIは以下に列挙された各キャリアを受け付けます。生成されたサンプルはプロトコルの推奨ヘッダーを使用します。

header
Authorization: Bearer YOUR_API_TOKEN
header
x-api-key: YOUR_API_TOKEN
header - 推奨
x-goog-api-key: YOUR_API_TOKEN
query
?key=YOUR_API_TOKEN
03

互換モデル

現在の料金、レート制限、商用利用の詳細については、モデルページを開いてください。

互換モデルを56件表示
04

リクエスト仕様

JSONボディ

公開プロトコルとRunAPIサポートの両方の証拠があるフィールドのみが記載されています。追加のJSONボディフィールドはそのまま渡されます。

JSON ボディフィールド4件
contentsarray
必須

Gemini ContentおよびPart形式の会話コンテンツ。

generationConfigobject
任意

リクエストに対する Gemini 生成設定。

systemInstructionobject
任意

Gemini Contentとして表されたシステム指示。

toolsarray
任意

モデルが呼び出せるツール。

パスパラメータフィールド1件
modelstring
必須

ワイヤーパスに配置されるRunAPIのモデル識別子。

リクエスト例

JSON
{
  "contents": [
    {
      "parts": [
        {
          "text": "Summarize one practical improvement for an API deployment."
        }
      ],
      "role": "user"
    }
  ]
}
05

SSEイベント:chunk

text/event-stream

スキーマ

JSON
{
  "additionalProperties": true,
  "properties": {
    "candidates": {
      "items": {
        "additionalProperties": true,
        "properties": {
          "content": {
            "additionalProperties": true,
            "properties": {
              "parts": {
                "type": "array"
              },
              "role": {
                "type": "string"
              }
            },
            "type": "object"
          },
          "finishReason": {
            "type": "string"
          },
          "index": {
            "type": "integer"
          }
        },
        "type": "object"
      },
      "type": "array"
    },
    "modelVersion": {
      "type": "string"
    },
    "responseId": {
      "type": "string"
    },
    "usageMetadata": {
      "additionalProperties": true,
      "properties": {
        "candidatesTokenCount": {
          "type": "integer"
        },
        "promptTokenCount": {
          "type": "integer"
        },
        "totalTokenCount": {
          "type": "integer"
        }
      },
      "type": "object"
    }
  },
  "type": "object"
}

JSON
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Hello"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0
    }
  ],
  "modelVersion": "gemini-3.5-flash",
  "responseId": "resp_example",
  "usageMetadata": {
    "candidatesTokenCount": 8,
    "promptTokenCount": 12,
    "totalTokenCount": 20
  }
}
06

プロトコルエラー: invalid_request

HTTP 400

スキーマ

JSON
{
  "additionalProperties": true,
  "properties": {
    "error": {
      "additionalProperties": true,
      "properties": {
        "code": {
          "type": "integer"
        },
        "message": {
          "type": "string"
        },
        "status": {
          "type": "string"
        }
      },
      "required": [
        "code",
        "message",
        "status"
      ],
      "type": "object"
    }
  },
  "required": [
    "error"
  ],
  "type": "object"
}

JSON
{
  "error": {
    "code": 400,
    "message": "contents is required",
    "status": "INVALID_ARGUMENT"
  }
}
07

使用方法

スキーマ

JSON
{
  "additionalProperties": true,
  "properties": {
    "candidatesTokenCount": {
      "type": "integer"
    },
    "promptTokenCount": {
      "type": "integer"
    },
    "totalTokenCount": {
      "type": "integer"
    }
  },
  "type": "object"
}

JSON
{
  "candidatesTokenCount": 8,
  "promptTokenCount": 12,
  "totalTokenCount": 20
}
08

生成されたコードサンプル

各cURL、JavaScript、およびPythonのサンプルは、プロトコル固有のSDKを必要とせずに、このコントラクトから検証済みのリクエストを送信します。

インストール

Shell
pip install requests
PYTHON
import requests

response = requests.post(
    "https://runapi.ai/v1beta/models/gemini-3.5-flash:streamGenerateContent?alt=sse",
    headers={"x-goog-api-key": "YOUR_API_TOKEN", "Content-Type": "application/json"},
    json={"contents": [{"role": "user", "parts": [{"text": "Summarize one practical improvement for an API deployment."}]}]},
    stream=True
)
response.raise_for_status()
for line in response.iter_lines(decode_unicode=True):
    if line:
        print(line)