Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Workflow:Googleapis Python genai Multi Turn Chat

From Leeroopedia
Knowledge Sources
Domains LLMs, Conversational_AI, Generative_AI
Last Updated 2026-02-15 14:00 GMT

Overview

End-to-end process for managing multi-turn conversations with Gemini models using the Google GenAI SDK's chat session interface.

Description

This workflow provides a conversational interface on top of the content generation API. The SDK's Chats module automatically manages conversation history, appending each user message and model response to maintain context across turns. This eliminates the need to manually construct and pass the full conversation history with each request. The chat interface supports both synchronous and asynchronous execution, with optional streaming.

Usage

Execute this workflow when building interactive conversational applications such as chatbots, virtual assistants, or interactive Q&A systems where the model needs to maintain context across multiple exchanges. This is preferable to raw generate_content calls when conversation state management is desired.

Execution Steps

Step 1: Client Initialization

Create a GenAI client configured for either the Gemini Developer API or Vertex AI. The chats module is available on both the sync client (client.chats) and the async client (client.aio.chats).

Key considerations:

  • Both Gemini Developer API and Vertex AI are supported
  • Async chats use client.aio.chats.create()

Step 2: Chat Session Creation

Create a new chat session using client.chats.create(), specifying the model and optional configuration. The config parameter accepts a GenerateContentConfig for setting system instructions, safety settings, tools, temperature, and other generation parameters that persist across all messages in the session. Optional initial history can be provided.

Key considerations:

  • Configuration set at chat creation applies to all messages in the session
  • System instructions are set once and apply to every turn
  • Tools and function calling can be configured at the session level
  • Initial conversation history can be pre-loaded

Step 3: Message Exchange

Send messages using chat.send_message() for non-streaming or chat.send_message_stream() for streaming responses. Each call automatically appends the user message and model response to the internal conversation history. Subsequent calls include the full history, enabling the model to reference prior context.

Key considerations:

  • Each send_message call includes the full conversation history
  • The chat object maintains state; do not create a new chat for follow-up messages
  • Streaming returns an iterator of response chunks
  • Async variants: await chat.send_message() and await chat.send_message_stream()

Step 4: History Management

Access the conversation history via chat.get_history() to inspect, export, or debug the conversation. The history contains all Content objects exchanged between user and model, including any function call interactions. The history can be used for logging, analytics, or transferring conversation state.

Key considerations:

  • History includes all turns: user messages, model responses, and tool interactions
  • History is maintained in-memory on the client side
  • The chat validates responses before appending to history to prevent corrupted state

Step 5: Response Processing

Process responses from each turn using the same patterns as standard content generation. Access response.text for text content, response.function_calls for tool invocations, and response.candidates for detailed metadata.

Key considerations:

  • Each response is a standard GenerateContentResponse
  • Streaming chunks follow the same pattern as generate_content_stream
  • Token usage accumulates across the conversation as history grows

Execution Diagram

GitHub URL

Workflow Repository