# How to Stream Real‑Time Responses from the Gemini Interactions API with Python

> Stream real-time responses from the Gemini Interactions API with Python by setting stream=True. Iterate over the generator to receive incremental Server-Sent Events chunks directly.

- Repository: [Google/skills](https://github.com/google/skills)
- Tags: how-to-guide
- Published: 2026-06-11

---

**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`](https://github.com/google/skills/blob/main/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:

```bash
pip install "google-genai>=2.0.0"

```

Configure the environment variables that the client reads automatically:

```bash
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`](https://github.com/google/skills/blob/main/skills/cloud/agent-platform-inference/scripts/gemini_genai_sdk.py), instantiate `genai.Client()` without explicit arguments when the environment variables are set:

```python
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:

```python
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:

```python
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`](https://github.com/google/skills/blob/main/skills/cloud/gemini-interactions-api/SKILL.md):

```python
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`](https://github.com/google/skills/blob/main/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.0` and set `GOOGLE_GENAI_USE_ENTERPRISE`, `GOOGLE_CLOUD_PROJECT`, and `GOOGLE_CLOUD_LOCATION`.
- **Authenticate** using `genai.Client()` which reads environment variables automatically.
- **Enable streaming** by passing `stream=True` to `client.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.md`](https://github.com/google/skills/blob/main/skills/cloud/gemini-interactions-api/SKILL.md) for the canonical implementation and [`skills/cloud/agent-platform-inference/scripts/gemini_genai_sdk.py`](https://github.com/google/skills/blob/main/skills/cloud/agent-platform-inference/scripts/gemini_genai_sdk.py) for 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`](https://github.com/google/skills/blob/main/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.