Implementation:Googleapis Python genai Chat Get History
| Knowledge Sources | |
|---|---|
| Domains | NLP, Conversational_AI |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for retrieving conversation history from a chat session provided by the google-genai chats module.
Description
Chat.get_history returns the ordered list of Content objects representing all conversation turns. When curated=True, it filters out turns where errors occurred (e.g., safety filter blocks), returning only the successful conversation thread. The history is maintained in the _BaseChat class and shared between Chat (sync) and AsyncChat (async) implementations.
Usage
Call chat.get_history() to retrieve the full conversation log. Use curated=True to get only successful turns. The returned list can be serialized for session persistence or passed as history to a new chats.create call for session restoration.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/chats.py
- Lines: L171-185 (get_history in _BaseChat)
Signature
class _BaseChat:
def get_history(self, curated: bool = False) -> list[Content]:
"""Returns the conversation history.
Args:
curated: If True, returns only successful (non-errored) turns.
"""
Import
from google import genai
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| curated | bool | No | If True, filter out errored turns (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| history | list[Content] | Ordered list of conversation turns (user + model Content objects) |
Usage Examples
Inspect History
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
chat = client.chats.create(model="gemini-2.0-flash")
chat.send_message("What is Python?")
chat.send_message("What about JavaScript?")
# Get full history
history = chat.get_history()
for turn in history:
print(f"{turn.role}: {turn.parts[0].text[:50]}...")
Session Persistence
# Save history from current session
history = chat.get_history(curated=True)
# Restore in a new session
new_chat = client.chats.create(
model="gemini-2.0-flash",
history=history
)
# Continue the conversation
response = new_chat.send_message("What were we discussing?")
print(response.text)