API 参考
Gemini Stream Generate Content
通过 RunAPI 的 Gemini 兼容 Stream Generate Content operation 发送请求。
01
概览
使用兼容模型和对应协议的请求、响应结构调用 RunAPI 的 Gemini 兼容 Stream Generate Content operation。
快速开始
- 创建 API Key,并将其设置为 RUNAPI_API_KEY。
- 选择兼容模型,然后发送文档所列的路径参数和 JSON 请求体。
- 读取此 operation 展示的 JSON 响应或服务器发送事件,并处理协议错误。
端点
- 基础 URL
https://runapi.ai- Contract 版本
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
兼容模型
打开模型页可查看当前价格、限流和商业使用详情。
展开 52 个兼容模型
- claude-fable-5
- claude-sonnet-5
- claude-opus-5
- claude-opus-4-8
- claude-opus-4-7
- claude-opus-4-6
- claude-sonnet-4-6
- claude-opus-4-5-20251101
- claude-sonnet-4-5-20250929
- claude-haiku-4-5-20251001
- claude-opus-4-1-20250805
- gpt-5.5
- gpt-5.5-pro
- gpt-5.4
- gpt-5.4-mini
- gpt-5.4-nano
- gpt-5.4-pro
- gpt-5.3-codex
- gpt-5.3-codex-spark
- gpt-5.2
- gpt-5.2-pro
- gpt-5.6-luna
- gpt-5.6-sol
- gpt-5.6-terra
- gemini-3.5-flash
- gemini-3.1-pro-preview
- gemini-3-flash-preview
- gemini-2.5-pro
- gemini-2.5-flash
- deepseek-v4-pro
- deepseek-v4-flash
- glm-5.2
- glm-5.1
- glm-5-turbo
- glm-5
- glm-4.7
- glm-4.6
- glm-4.5
- glm-4.5-air
- kimi-k3
- kimi-k2.7-code
- kimi-k2.6
- kimi-k2.5
- MiniMax-M3
- MiniMax-M2.7
- MiniMax-M2.7-highspeed
- MiniMax-M2.5
- MiniMax-M2.5-highspeed
- MiniMax-M2.1
- MiniMax-M2
- grok-4.5
- grok-4.3
04
JSON 请求体请求 Contract
这里只列出同时具有公共协议与 RunAPI 支持证据的字段;其他 JSON 请求体字段会原样传递。
JSON 请求体4 个字段
contentsarray
必填
Conversation content in Gemini Content and Part format.
generationConfigobject
可选
Gemini generation configuration for the request.
systemInstructionobject
可选
System instructions represented as Gemini Content.
toolsarray
可选
Tools the model may call.
路径参数1 个字段
modelstring
必填
RunAPI model identifier placed in the wire path.
请求示例
JSON
{
"contents": [
{
"parts": [
{
"text": "Say hello in one sentence."
}
],
"role": "user"
}
]
}
05
text/event-stream
SSE 事件:chunk
Schema
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
HTTP 400
协议错误:invalid_request
Schema
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
Usage
Schema
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 示例都会发送此 Contract 中经过验证的请求,无需协议专用 SDK。
CURL
curl -X POST https://runapi.ai/v1beta/models/gemini-3.5-flash:streamGenerateContent\?alt\=sse \
-H x-goog-api-key:\ YOUR_API_TOKEN \
-H Content-Type:\ application/json \
-d \{\"contents\":\[\{\"role\":\"user\",\"parts\":\[\{\"text\":\"Say\ hello\ in\ one\ sentence.\"\}\]\}\]\}
JAVASCRIPT
const response = await fetch("https://runapi.ai/v1beta/models/gemini-3.5-flash:streamGenerateContent?alt=sse", {
method: "POST",
headers: {
"x-goog-api-key": "YOUR_API_TOKEN",
"Content-Type": "application/json"
},
body: JSON.stringify({
"contents": [
{
"role": "user",
"parts": [
{
"text": "Say hello in one sentence."
}
]
}
]
})
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { value, done } = await reader.read();
if (done) break;
console.log(decoder.decode(value, { stream: true }));
}
安装
BASH
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": "Say hello in one sentence."}]}]},
stream=True
)
response.raise_for_status()
for line in response.iter_lines(decode_unicode=True):
if line:
print(line)
09