googleapis / googleapis/python-genai

[Bug] ChatSession history fragmentation when using send_message_stream with Thinking (Gemini 3 Pro)

Open
#1,791 2 comments 0 reactions 1 assignee Assigned to @kkorpal View on GitHub
api: gemini-api type: bug
Dominant language
Python
Stars
4k
Forks
1k
Avg merge
2d 11h
Merged PRs (30d)
40

Description

**Description**
When using `Gemini 3 Pro Preview` with `thinking_config` enabled, the `ChatSession` history becomes fragmented when using `send_message_stream`.

Instead of appending a single model turn with the complete response, the SDK appends multiple model turns corresponding to the streaming chunks (including thought chunks). This causes the model to lose context in subsequent turns because the history is malformed (e.g., multiple consecutive 'model' roles without intervening 'user' roles).

**Steps to Reproduce**
Run the following standalone script:

```python
import os
# pip install google-genai
from google import genai
from google.genai import types

# Ensure GEMINI_API_KEY is set in environment

def reproduce_issue():
client = genai.Client(api_key=os.getenv('GEMINI_API_KEY'))
model_id = 'gemini-3-pro-preview'

print(f"Testing model: {model_id}")

config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
include_thoughts=True,
thinking_budget=-1
)
)

chat = client.chats.create(model=model_id, config=config)

# 1. Send a question with streaming
q1 = "Hi, explain entropy in 1 sentence."
print(f"\nUser: {q1}")

# Consume the stream
for chunk in chat.send_message_stream(q1):
pass

print("\n--- Checking History ---")
# Expected: 2 items (User, Model)
# Actual: Many items (User, Model, Model, Model...)
history = chat.get_history()
print(f"History length: {len(history)}")

for i, msg in enumerate(history):
role = msg.role
print(f"{i}: [{role}] Parts: {len(msg.parts) if msg.parts else 0}")

if __name__ == "__main__":
reproduce_issue()
```

**Expected Behavior**
The `ChatSession` should accumulate all streaming chunks into a single history entry (Model Turn) containing the final text and the thought signature, preserving the conversation structure [User, Model, User, Model].

**Actual Behavior**
The history contains multiple fragmented entries for a single response, breaking the conversation context for future turns.

**Environment**
- SDK: google-genai (latest)
- Model: gemini-3-pro-preview

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.