Stateless Gemini API and Maintaining Continuous Conversations for Multiple Users

Welcome to the forum.

Yes, you correctly answered your question. Just like a browser handling HTTP (also a stateless protocol), the state is managed by the client. To keep a multi-turn chat going, each client re-sends the entire chat history for that user with each new request.

The client library has a support facility: generative-ai-python/docs/api/google/generativeai/ChatSession.md at main · google-gemini/generative-ai-python · GitHub

Your app probably will want to provide a way for users to revisit a chat session from a few days ago as well, in which case you will want to provide a save (make the state of the ChatSession object persistent) and a retrieve (load history from the persistent store and assign history to a freshly initialized empty ChatSession) plus a delete to permanently erase unwanted old chat.

Hope that helps.

3 Likes