Gemini ネイティブ
コンテンツのストリーム生成
Google Gemini API形式を使用してコンテンツ生成をストリーミングします
Gemini generateContent の SSE 版です。メタデータのみのイベントや、finishReason のない途中の内容も返されます。ストリームは自然な EOF で終了し、[DONE] マーカーは追加されません。
パスパラメータ
モデル名 (例: gemini-2.5-pro, gemini-3.5-flash)。
クエリパラメータ
APIキー (ヘッダー認証の代替)。
リクエストボディ
コンテンツ生成と同じです。
generationConfig.candidateCount、ツール、メディア形式、その他の任意フィールドへの対応は選択したモデルによって異なります。
レスポンス
各オブジェクトに部分的なレスポンスが含まれる、JSONオブジェクトのストリームを返します。
リクエスト
curl -N -X POST "https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"parts": [{"text": "Tell me a story"}]
}
]
}'import google.generativeai as genai
genai.configure(
api_key="sk-your-api-key",
transport="rest",
client_options={"api_endpoint": "api.tokenlab.sh"}
)
model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content("Tell me a story", stream=True)
for chunk in response:
for candidate in chunk.candidates:
for part in candidate.content.parts:
if part.text:
print(part.text, end="")const response = await fetch(
'https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{ parts: [{ text: 'Tell me a story' }] }]
})
}
);
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(decoder.decode(value, { stream: true }));
}package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
payload := map[string]interface{}{
"contents": []map[string]interface{}{
{"parts": []map[string]string{{"text": "Tell me a story"}}},
},
}
jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST",
"https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key",
bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}<?php
$payload = [
'contents' => [
['parts' => [['text' => 'Tell me a story']]]
]
];
$ch = curl_init('https://api.tokenlab.sh/v1beta/models/gemini-2.5-pro:streamGenerateContent?key=sk-your-api-key');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_WRITEFUNCTION => function($ch, $data) {
echo $data;
return strlen($data);
}
]);
curl_exec($ch);
curl_close($ch);ビジョン入力の例
ストリーミングの vision リクエストは、非ストリーミングのエンドポイントと同じ contents[].parts[] 構造を使用します。
{
"contents": [
{
"role": "user",
"parts": [
{ "text": "Please describe this image." },
{
"inline_data": {
"mime_type": "image/jpeg",
"data": "/9j/4AAQSkZJRgABAQ..."
}
}
]
}
]
}レスポンス
{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{"text": "Once upon a time"}
]
}
}
]
}認証
BearerAuth APIキー認証。Dashboard > API > API KeysでAPIキーを作成または管理します。
場所: header
パスパラメータ
モデル名 (例: gemini-2.5-pro)
クエリパラメータ
APIキー (ヘッダー認証の代替)
ヘッダー
リクエストごとの配信ポリシー。APIキーおよびWorkspaceのデフォルト設定を上書きします。自動的にまず TokenLab Verified を試行し、出力、リクエストの受け入れ、または永続的なリソース作成の前に、一度だけ Official に切り替える場合があります。
指定できる値
- "auto"
- "verified"
- "official"
リクエストボディ
application/json
ネイティブGemini GenerateContentリクエスト。ProtoJSONのlowerCamelCaseと元のprotoのsnake_caseの両方が受け入れられ、保持されます(混在リクエストを含む)。1つのフィールドに対して両方の綴りが存在する場合、TokenLabはそれらをマージしたり、ローカルの優先順位を選択したりしません。不明なフィールドはベストエフォートで転送されます。
レスポンス
text/event-stream
application/json
application/json
application/json
application/json
application/json