How to Stream Real‑Time Responses from the Gemini Interactions API with Python
Set stream=True when calling client.interactions.create() and iterate over the returned generator to receive Server‑Sent Events chunks containing incremental steps from the Gemini model.
The Google Skills repository demonstrates how to build stateful, server‑managed Gemini agents using the Interactions API. When you need to stream real‑time responses from the Gemini Interactions API with Python, the SDK yields an iterable chunk generator that delivers each step as soon as it is produced by the model.
Prerequisites and SDK Version
According to skills/cloud/gemini-interactions-api/SKILL.md, the Interactions API requires the Google Gen AI SDK (google‑genai >= 2.0.0). Older packages such as google‑cloud‑aiplatform or google‑generativeai do not support streaming interactions.
Install the SDK:
pip install "google-genai>=2.0.0"
Configure the environment variables that the client reads automatically:
export GOOGLE_GENAI_USE_ENTERPRISE=true
export GOOGLE_CLOUD_PROJECT="your-project-id"
export GOOGLE_CLOUD_LOCATION="global"
The SDK uses Application Default Credentials to authenticate with the Gemini Enterprise Agent Platform in the global location.
Understanding the Streaming Architecture
When stream=True is passed to client.interactions.create(), the backend returns a Server‑Sent Events (SSE) stream with Transfer‑Encoding: chunked. The SDK wraps this HTTP stream in a Python generator so you can iterate over it with a normal for loop.
Each yielded chunk contains a JSON object with a steps array. The last element of this array holds the most recent model output, allowing you to extract incremental text updates in real time.
Implementation Steps
Initialize the Client
As shown in skills/cloud/agent-platform-inference/scripts/gemini_genai_sdk.py, instantiate genai.Client() without explicit arguments when the environment variables are set:
from google import genai
client = genai.Client()
The constructor reads GOOGLE_GENAI_USE_ENTERPRISE, GOOGLE_CLOUD_PROJECT, and GOOGLE_CLOUD_LOCATION automatically.
Create a Streaming Interaction
Call client.interactions.create() with stream=True to enable real‑time streaming:
response = client.interactions.create(
model="gemini-3-flash-preview",
input="Write a short poem about debugging.",
stream=True
)
Process Chunked Responses
Iterate over the generator and extract text from the latest step in each chunk:
for chunk in response:
if chunk.steps:
step = chunk.steps[-1] # Most recent step
if step.content and step.content[0].text:
print(step.content[0].text, end="", flush=True)
print() # Final newline after stream completes
Complete Working Example
The following implementation combines authentication, streaming creation, and chunk processing as documented in skills/cloud/gemini-interactions-api/SKILL.md:
from google import genai
# Initialize client (reads GOOGLE_GENAI_USE_ENTERPRISE and other env vars)
client = genai.Client()
# Create streaming interaction
response = client.interactions.create(
model="gemini-3-flash-preview",
input="Write a short poem about debugging.",
stream=True
)
# Iterate over streamed chunks and print real-time responses
for chunk in response:
if chunk.steps:
step = chunk.steps[-1]
if step.content and step.content[0].text:
print(step.content[0].text, end="", flush=True)
print()
Key Differences from Vertex AI SDK
The file skills/cloud/agent-platform-inference/scripts/gemini_vertexai_sdk.py demonstrates non‑streaming requests using the legacy Vertex AI SDK. Unlike the Interactions API, which manages stateful conversation threads server‑side and returns incremental steps via SSE, the Vertex AI SDK requires manual state management and returns complete responses only after generation finishes.
For real‑time applications requiring incremental display, the Interactions API with stream=True provides lower latency and simpler architecture.
Summary
- Install
google‑genai >= 2.0.0and setGOOGLE_GENAI_USE_ENTERPRISE,GOOGLE_CLOUD_PROJECT, andGOOGLE_CLOUD_LOCATION. - Authenticate using
genai.Client()which reads environment variables automatically. - Enable streaming by passing
stream=Truetoclient.interactions.create(). - Process chunks by iterating over the response generator and extracting
chunk.steps[-1].content[0].text. - Reference
skills/cloud/gemini-interactions-api/SKILL.mdfor the canonical implementation andskills/cloud/agent-platform-inference/scripts/gemini_genai_sdk.pyfor client initialization patterns.
Frequently Asked Questions
What is the minimum required version of the Google Gen AI SDK for streaming?
You must use google‑genai >= 2.0.0. Earlier versions and legacy packages like google‑cloud‑aiplatform or google‑generativeai do not support streaming interactions with the Interactions API according to the source code in skills/cloud/gemini-interactions-api/SKILL.md.
How does the Interactions API handle authentication?
The genai.Client() constructor reads the environment variables GOOGLE_GENAI_USE_ENTERPRISE, GOOGLE_CLOUD_PROJECT, and GOOGLE_CLOUD_LOCATION to authenticate with Application Default Credentials against the Gemini Enterprise Agent Platform. You can also pass these values explicitly to the constructor if not using environment variables.
Why do I need to access chunk.steps[-1] instead of the first step?
The Interactions API returns an array of steps representing the conversation history. The last element (steps[-1]) contains the most recent model output generated in the current streaming chunk, allowing you to display incremental updates as they arrive from the SSE stream.
Can I use streaming with existing conversation threads?
Yes. When calling client.interactions.create(), you can specify an existing interaction ID to continue a conversation. The streaming behavior remains the same, yielding incremental steps for the new turn in the stateful thread managed by the Gemini Enterprise Agent Platform.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →