Implementation:Googleapis Python genai Chats Create
| Knowledge Sources | |
|---|---|
| Domains | NLP, Conversational_AI |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for creating multi-turn chat sessions with automatic history management provided by the google-genai chats module.
Description
Chats.create instantiates a Chat session bound to a specific model. The session stores shared configuration (system instruction, tools, safety settings) and conversation history. Each subsequent send_message call automatically appends user and model turns to the history. Optional history parameter allows seeding the session with prior conversation turns for continuity.
Usage
Call client.chats.create to start a new chat session. Pass model (required), optional config for shared settings, and optional history for pre-existing conversation context. Use the returned Chat object for all subsequent message exchanges.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/chats.py
- Lines: L337-365 (Chats class and create method)
Signature
class Chats:
def create(
self,
*,
model: str,
config: Optional[GenerateContentConfigOrDict] = None,
history: Optional[list[ContentOrDict]] = None,
) -> Chat:
"""Creates a new chat session.
Args:
model: Model resource ID (e.g., 'gemini-2.0-flash').
config: Shared generation config for all messages.
history: Optional initial conversation history.
"""
Import
from google import genai
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | Yes | Model resource ID |
| config | Optional[GenerateContentConfigOrDict] | No | Shared config for all messages (system_instruction, tools, etc.) |
| history | Optional[list[ContentOrDict]] | No | Initial conversation history to seed the session |
Outputs
| Name | Type | Description |
|---|---|---|
| Chat | Chat | Stateful chat session with send_message, send_message_stream, get_history methods |
Usage Examples
Basic Chat Session
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
chat = client.chats.create(
model="gemini-2.0-flash",
config=types.GenerateContentConfig(
system_instruction="You are a helpful coding assistant."
)
)
# First turn
response = chat.send_message("What is a Python decorator?")
print(response.text)
# Second turn (history is automatic)
response = chat.send_message("Can you show me an example?")
print(response.text)
Chat with Pre-seeded History
chat = client.chats.create(
model="gemini-2.0-flash",
history=[
types.Content(
parts=[types.Part.from_text(text="What is Python?")],
role="user"
),
types.Content(
parts=[types.Part.from_text(text="Python is a programming language.")],
role="model"
),
]
)
# Continue the conversation
response = chat.send_message("What are its best features?")
print(response.text)