Skip to main content
POST
/
v1beta
/
models
/
{model}
:streamGenerateContent
streamGenerateContent
curl --request POST \
  --url https://openp.ai/v1beta/models/{model}:streamGenerateContent
import requests

url = "https://openp.ai/v1beta/models/{model}:streamGenerateContent"

response = requests.post(url)

print(response.text)
const options = {method: 'POST'};

fetch('https://openp.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://openp.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",
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
package main

import (
"fmt"
"net/http"
"io"
)

func main() {

url := "https://openp.ai/v1beta/models/{model}:streamGenerateContent"

req, _ := http.NewRequest("POST", url, nil)

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

fmt.Println(string(body))

}
HttpResponse<String> response = Unirest.post("https://openp.ai/v1beta/models/{model}:streamGenerateContent")
.asString();
require 'uri'
require 'net/http'

url = URI("https://openp.ai/v1beta/models/{model}:streamGenerateContent")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)

response = http.request(request)
puts response.read_body
Identical to generateContent, but returned as a stream — chunks are pushed as they’re generated, reducing time-to-first-token.

Request

curl "https://openp.ai/v1beta/models/gemini-3.1-pro-preview:streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: $OPENPAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "Tell a 100-word story"}]}]
  }'
Note the query parameter ?alt=sse — without it, the response is a JSON array (one complete candidate per item); only with alt=sse do you get a standard SSE stream (the data: {...} form).

Stream chunks

data: {"candidates":[{"content":{"role":"model","parts":[{"text":"Once"}]},"index":0}]}

data: {"candidates":[{"content":{"role":"model","parts":[{"text":" upon"}]},"index":0}]}

...

data: {"candidates":[{"finishReason":"STOP","index":0}],"usageMetadata":{...}}
The last chunk usually carries finishReason and usageMetadata.

Python (google-genai)

from google import genai

client = genai.Client(
    api_key="sk-...",
    http_options={"base_url": "https://openp.ai"},
)

stream = client.models.generate_content_stream(
    model="gemini-3.1-pro-preview",
    contents="Tell a 100-word story",
)
for chunk in stream:
    print(chunk.text, end="", flush=True)

Notes

  • Streaming responses also support tools / function calling / thinking mode — those fields appear within some chunk.
  • The client must handle retries after an SSE disconnect (it is not idempotent — a retry is billed again).