Stateless Gemini API and Maintaining Continuous Conversations for Multiple Users

Hi everyone,

I’m currently exploring the Gemini API for building a conversational system. I understand that the API is stateless, which raises a question about managing conversation history effectively.

Specifically, my scenario involves multiple users (let’s say 5 for simplicity) accessing the same API endpoint. Each user needs to have their own independent, continuous conversation thread.

Given the stateless nature of the API, how can I ensure that:

  1. Each user’s conversation history is preserved across their individual interactions?

  2. The API can differentiate between the 5 ongoing conversations and doesn’t mix up the context?

Do I need to handle all the context management on the client-side by sending the relevant history with each request? Are there any recommended techniques or best practices for this?

Any insights or guidance on implementing persistent conversations with a stateless Gemini API would be greatly appreciated.

Thanks in advance!

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