#!/usr/bin/env python3 # STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 0.9s) # streamGenerateContent with ?alt=sse (SSE framing) — prints each chunk's text and the final finishReason/usage. # Run: .venv/bin/python examples/gemini/streaming/stream.py (GEMINI_API_KEY read from .env via scripts.live; google-genai 2.24) import pathlib import sys ROOT = pathlib.Path(__file__).resolve().parents[3] sys.path.insert(0, str(ROOT)) from scripts import live # noqa: E402 loads .env, provides log_request from google import genai # noqa: E402 from google.genai import types # noqa: E402,F401 client = genai.Client() MODEL = "gemini-3.5-flash-lite" n = 0 for chunk in client.models.generate_content_stream(model=MODEL, contents="Count from 1 to 12 separated by spaces.", config=types.GenerateContentConfig(max_output_tokens=32)): n += 1 print(repr(chunk.text), chunk.candidates[0].finish_reason, chunk.usage_metadata.candidates_token_count) live.log_request("gemini", "POST", "/v1beta/models/{model}:streamGenerateContent", 200, 0.00007, f"example streaming/stream.py ({n} chunks)") assert n >= 2