Send multi-turn Gemini chat prompts without losing application control.
A Gemini chat session uses previous user and model messages as context for the next response. The SDK can manage that history for convenience, while production applications still need explicit limits, validation, persistence, privacy controls, and recovery behavior.
Chat API essentials
Conversation history
Each follow-up depends on earlier turns. Keep roles and ordering intact, and decide which messages are necessary for the next request.
System instructions
Set persistent role, style, constraints, or response format separately from the current user message.
Streaming
Stream partial output for responsive interfaces, or wait for a complete response when validation must happen before display.
Operational controls
Bound tokens, latency, retries, storage, tool access, and user input before treating a chat prototype as production-ready.
Start and continue a chat
Create a chat with a current model, send the first message, then send follow-ups through the same session object. The SDK assembles prior turns into the context needed for subsequent responses.
- Store the model identifier in configuration rather than hardcoding an example model.
- Give each conversation an application-owned identifier for logs and persistence.
- Handle empty responses, blocked output, network failures, and timeouts explicitly.
Manage conversation history
Chat history consumes context and may contain sensitive data. Keep only the turns required for continuity, summarize older material when appropriate, and make retention a deliberate product policy.
- Preserve the user and model role sequence when building REST requests manually.
- Do not trust a client-provided transcript without authorization and integrity checks.
- Let users understand, delete, or restart stored conversations where appropriate.
Choose streaming or complete responses
Streaming improves perceived responsiveness for longer answers, but partial text can be incomplete or unsafe to act on. Non-streaming responses are simpler when the application must validate the entire result first.
- Render streamed text as untrusted content and support cancellation.
- Delay irreversible actions until tool arguments and the final response are validated.
- Measure time to first token, total latency, completion rate, and user cancellation.
Tune generation conservatively
Temperature, output token limits, stop sequences, and sampling controls can affect variability and length. Defaults and recommended ranges vary by model, so change one setting at a time and evaluate it on your own tasks.
- Use explicit output requirements before relying on sampling changes.
- Set output and cost budgets that match the interface and use case.
- Confirm supported parameters for the selected model in current Google documentation.
Secure the production chat flow
Model output, retrieved content, uploaded files, and user messages are untrusted inputs. A secure chat architecture enforces permissions and validation outside the model.
- Keep API keys on the server and apply per-user rate limits and abuse controls.
- Authorize every tool call and validate its arguments against a strict schema.
- Avoid logging secrets or full conversations by default; document retention and redaction.
Python
The second message follows the first within one SDK chat session. Configure GEMINI_MODEL with a currently supported model identifier.
import os
from google import genai
client = genai.Client()
model = os.environ["GEMINI_MODEL"]
chat = client.chats.create(model=model)
first = chat.send_message(
"Explain context caching for a backend developer."
)
print(first.text)
follow_up = chat.send_message(
"Now give me a production-readiness checklist."
)
print(follow_up.text)