googleapis / googleapis/python-genai

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

Aperta
#1,791 2 commenti 0 reazioni 1 assegnatario Assegnata a @kkorpal Vedi su GitHub
api: gemini-api type: bug
Lingua principale
Python
Stelle
4k
Fork
1k
Merge medio
2g 12h
PR unite (30g)
41

Descrizione

**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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.