Gemini streamGenerateContent
curl --request POST \
--url https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent \
--header 'Content-Type: application/json' \
--header 'x-goog-api-key: <api-key>' \
--data '
{
"contents": [
{
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
}
],
"systemInstruction": {
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
},
"generationConfig": {
"temperature": 0.7,
"topP": 123,
"maxOutputTokens": 123,
"responseMimeType": "<string>"
},
"safetySettings": [
{
"category": "<string>",
"threshold": "<string>"
}
],
"tools": [
{}
]
}
'import requests
url = "https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent"
payload = {
"contents": [
{
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
}
],
"systemInstruction": {
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
},
"generationConfig": {
"temperature": 0.7,
"topP": 123,
"maxOutputTokens": 123,
"responseMimeType": "<string>"
},
"safetySettings": [
{
"category": "<string>",
"threshold": "<string>"
}
],
"tools": [{}]
}
headers = {
"x-goog-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-goog-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
contents: [
{
role: '<string>',
parts: [
{
text: '<string>',
inlineData: {mimeType: '<string>', data: '<string>'},
fileData: {mimeType: '<string>', fileUri: '<string>'}
}
]
}
],
systemInstruction: {
role: '<string>',
parts: [
{
text: '<string>',
inlineData: {mimeType: '<string>', data: '<string>'},
fileData: {mimeType: '<string>', fileUri: '<string>'}
}
]
},
generationConfig: {
temperature: 0.7,
topP: 123,
maxOutputTokens: 123,
responseMimeType: '<string>'
},
safetySettings: [{category: '<string>', threshold: '<string>'}],
tools: [{}]
})
};
fetch('https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'contents' => [
[
'role' => '<string>',
'parts' => [
[
'text' => '<string>',
'inlineData' => [
'mimeType' => '<string>',
'data' => '<string>'
],
'fileData' => [
'mimeType' => '<string>',
'fileUri' => '<string>'
]
]
]
]
],
'systemInstruction' => [
'role' => '<string>',
'parts' => [
[
'text' => '<string>',
'inlineData' => [
'mimeType' => '<string>',
'data' => '<string>'
],
'fileData' => [
'mimeType' => '<string>',
'fileUri' => '<string>'
]
]
]
],
'generationConfig' => [
'temperature' => 0.7,
'topP' => 123,
'maxOutputTokens' => 123,
'responseMimeType' => '<string>'
],
'safetySettings' => [
[
'category' => '<string>',
'threshold' => '<string>'
]
],
'tools' => [
[
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-goog-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent"
payload := strings.NewReader("{\n \"contents\": [\n {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n }\n ],\n \"systemInstruction\": {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n },\n \"generationConfig\": {\n \"temperature\": 0.7,\n \"topP\": 123,\n \"maxOutputTokens\": 123,\n \"responseMimeType\": \"<string>\"\n },\n \"safetySettings\": [\n {\n \"category\": \"<string>\",\n \"threshold\": \"<string>\"\n }\n ],\n \"tools\": [\n {}\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-goog-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent")
.header("x-goog-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"contents\": [\n {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n }\n ],\n \"systemInstruction\": {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n },\n \"generationConfig\": {\n \"temperature\": 0.7,\n \"topP\": 123,\n \"maxOutputTokens\": 123,\n \"responseMimeType\": \"<string>\"\n },\n \"safetySettings\": [\n {\n \"category\": \"<string>\",\n \"threshold\": \"<string>\"\n }\n ],\n \"tools\": [\n {}\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-goog-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"contents\": [\n {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n }\n ],\n \"systemInstruction\": {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n },\n \"generationConfig\": {\n \"temperature\": 0.7,\n \"topP\": 123,\n \"maxOutputTokens\": 123,\n \"responseMimeType\": \"<string>\"\n },\n \"safetySettings\": [\n {\n \"category\": \"<string>\",\n \"threshold\": \"<string>\"\n }\n ],\n \"tools\": [\n {}\n ]\n}"
response = http.request(request)
puts response.read_body"data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"这里是第一段\"}]}}]}\n\n"{
"error": 123,
"message": "<string>"
}文本
/v1beta/models/{model}:streamGenerateContent
Streams incremental Gemini generation events.
POST
/
v1beta
/
models
/
{model}
:streamGenerateContent
Gemini streamGenerateContent
curl --request POST \
--url https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent \
--header 'Content-Type: application/json' \
--header 'x-goog-api-key: <api-key>' \
--data '
{
"contents": [
{
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
}
],
"systemInstruction": {
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
},
"generationConfig": {
"temperature": 0.7,
"topP": 123,
"maxOutputTokens": 123,
"responseMimeType": "<string>"
},
"safetySettings": [
{
"category": "<string>",
"threshold": "<string>"
}
],
"tools": [
{}
]
}
'import requests
url = "https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent"
payload = {
"contents": [
{
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
}
],
"systemInstruction": {
"role": "<string>",
"parts": [
{
"text": "<string>",
"inlineData": {
"mimeType": "<string>",
"data": "<string>"
},
"fileData": {
"mimeType": "<string>",
"fileUri": "<string>"
}
}
]
},
"generationConfig": {
"temperature": 0.7,
"topP": 123,
"maxOutputTokens": 123,
"responseMimeType": "<string>"
},
"safetySettings": [
{
"category": "<string>",
"threshold": "<string>"
}
],
"tools": [{}]
}
headers = {
"x-goog-api-key": "<api-key>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'x-goog-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
contents: [
{
role: '<string>',
parts: [
{
text: '<string>',
inlineData: {mimeType: '<string>', data: '<string>'},
fileData: {mimeType: '<string>', fileUri: '<string>'}
}
]
}
],
systemInstruction: {
role: '<string>',
parts: [
{
text: '<string>',
inlineData: {mimeType: '<string>', data: '<string>'},
fileData: {mimeType: '<string>', fileUri: '<string>'}
}
]
},
generationConfig: {
temperature: 0.7,
topP: 123,
maxOutputTokens: 123,
responseMimeType: '<string>'
},
safetySettings: [{category: '<string>', threshold: '<string>'}],
tools: [{}]
})
};
fetch('https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'contents' => [
[
'role' => '<string>',
'parts' => [
[
'text' => '<string>',
'inlineData' => [
'mimeType' => '<string>',
'data' => '<string>'
],
'fileData' => [
'mimeType' => '<string>',
'fileUri' => '<string>'
]
]
]
]
],
'systemInstruction' => [
'role' => '<string>',
'parts' => [
[
'text' => '<string>',
'inlineData' => [
'mimeType' => '<string>',
'data' => '<string>'
],
'fileData' => [
'mimeType' => '<string>',
'fileUri' => '<string>'
]
]
]
],
'generationConfig' => [
'temperature' => 0.7,
'topP' => 123,
'maxOutputTokens' => 123,
'responseMimeType' => '<string>'
],
'safetySettings' => [
[
'category' => '<string>',
'threshold' => '<string>'
]
],
'tools' => [
[
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-goog-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent"
payload := strings.NewReader("{\n \"contents\": [\n {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n }\n ],\n \"systemInstruction\": {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n },\n \"generationConfig\": {\n \"temperature\": 0.7,\n \"topP\": 123,\n \"maxOutputTokens\": 123,\n \"responseMimeType\": \"<string>\"\n },\n \"safetySettings\": [\n {\n \"category\": \"<string>\",\n \"threshold\": \"<string>\"\n }\n ],\n \"tools\": [\n {}\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-goog-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent")
.header("x-goog-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"contents\": [\n {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n }\n ],\n \"systemInstruction\": {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n },\n \"generationConfig\": {\n \"temperature\": 0.7,\n \"topP\": 123,\n \"maxOutputTokens\": 123,\n \"responseMimeType\": \"<string>\"\n },\n \"safetySettings\": [\n {\n \"category\": \"<string>\",\n \"threshold\": \"<string>\"\n }\n ],\n \"tools\": [\n {}\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api-vip.apigo.ai/v1beta/models/{model}:streamGenerateContent")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-goog-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"contents\": [\n {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n }\n ],\n \"systemInstruction\": {\n \"role\": \"<string>\",\n \"parts\": [\n {\n \"text\": \"<string>\",\n \"inlineData\": {\n \"mimeType\": \"<string>\",\n \"data\": \"<string>\"\n },\n \"fileData\": {\n \"mimeType\": \"<string>\",\n \"fileUri\": \"<string>\"\n }\n }\n ]\n },\n \"generationConfig\": {\n \"temperature\": 0.7,\n \"topP\": 123,\n \"maxOutputTokens\": 123,\n \"responseMimeType\": \"<string>\"\n },\n \"safetySettings\": [\n {\n \"category\": \"<string>\",\n \"threshold\": \"<string>\"\n }\n ],\n \"tools\": [\n {}\n ]\n}"
response = http.request(request)
puts response.read_body"data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"这里是第一段\"}]}}]}\n\n"{
"error": 123,
"message": "<string>"
}用于 Gemini 的流式文本生成和增量事件输出。
- 官方常见鉴权方式是
x-goog-api-key: {API_KEY},也支持查询参数 API Key - 请求体结构与
generateContent基本一致,核心仍是contents - 返回内容是事件流而不是一次性 JSON,客户端需要按顺序拼接增量片段
- 停止原因和安全信息可能在后续 chunk 中才完整出现
授权
路径参数
Gemini model name.
示例:
"gemini-2.5-pro"
请求体
application/json
响应
Successful Gemini streaming response
The response is of type string.
⌘I
