Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Googleapis Python genai Chat Get History

From Leeroopedia
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

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)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment