UniterRouter
API ReferenceGoogle Compatible

Stream Generate Content

Gemini Streaming API - Real-time content generation with Gemini Pro, Gemini 1.5. Reduce latency and improve UX with Server-Sent Events streaming.

Stream Generate Content

Generate content from a Gemini model with real-time streaming. Receive tokens as they are generated for faster time-to-first-token.

Recommended for long responses: Streaming provides a better user experience for longer outputs by showing content progressively.

Endpoint

POST https://api.uniterouter.ai/v1beta/models/{model}:streamGenerateContent

Authentication

x-goog-api-key: your_uniterouter_api_key

Path Parameters

ParameterTypeRequiredDescription
modelstringYesModel ID (e.g., gemini-2.0-flash)

Query Parameters

ParameterTypeDefaultDescription
altstringjsonSet to sse for Server-Sent Events format

Request Body

Same as Generate Content. All parameters are supported.

Example Request

curl -X POST "https://api.uniterouter.ai/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $UNITEROUTER_API_KEY" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{"text": "Write a short story about a robot"}]
      }
    ]
  }'
import google.generativeai as genai

genai.configure(
    api_key="your_uniterouter_api_key",
    transport="rest",
    client_options={"api_endpoint": "api.uniterouter.ai"}
)

model = genai.GenerativeModel("gemini-2.0-flash")

# Stream the response
response = model.generate_content(
    "Write a short story about a robot",
    stream=True
)

for chunk in response:
    print(chunk.text, end="", flush=True)
import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI("your_uniterouter_api_key", {
  baseUrl: "https://api.uniterouter.ai"
});

const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });

const result = await model.generateContentStream({
  contents: [
    {
      role: "user",
      parts: [{ text: "Write a short story about a robot" }]
    }
  ]
});

for await (const chunk of result.stream) {
  const text = chunk.text();
  process.stdout.write(text);
}

Streaming Response Format

Each chunk in the stream contains a partial response:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {"text": "Once upon a time, "}
        ],
        "role": "model"
      },
      "finishReason": null,
      "index": 0
    }
  ]
}

The final chunk includes finishReason and usage metadata:

{
  "candidates": [
    {
      "content": {
        "parts": [
          {"text": "The end."}
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 8,
    "candidatesTokenCount": 250,
    "totalTokenCount": 258
  }
}

Streaming Chat

For multi-turn streaming conversations:

model = genai.GenerativeModel("gemini-2.0-flash")
chat = model.start_chat(history=[])

# First message (streaming)
response = chat.send_message("Hello! Who are you?", stream=True)
for chunk in response:
    print(chunk.text, end="")

print()  # New line

# Follow-up message (streaming)
response = chat.send_message("What can you help me with?", stream=True)
for chunk in response:
    print(chunk.text, end="")

Server-Sent Events (SSE)

When using alt=sse, the response format is:

data: {"candidates":[{"content":{"parts":[{"text":"Hello"}]}}]}

data: {"candidates":[{"content":{"parts":[{"text":" there"}]}}]}

data: [DONE]

Error Handling

Streaming requests may fail mid-stream. Always handle potential errors:

try:
    response = model.generate_content("Your prompt", stream=True)
    for chunk in response:
        print(chunk.text, end="")
except Exception as e:
    print(f"Stream error: {e}")

How is this guide?