How do I get the chat history with the new SDK Javascript

With the new gen ai SDK is there a way to get the chat history from a chat created with chats.create?

Seen here https://ai.google.dev/gemini-api/docs/text-generation#multi-turn-conversations

Hey @AndyK , You can use chat.history function to access the chat history.

Thanks! I also found this documentation on GitHub on the chat class which is really useful.
Chat | @google/genai

It looks like the answer is no, but I wonder if there are “setters” for the chat parameters so I could switch which model I’m using mid conversation. Or would I need to start a whole new chat and copy the history?

Hi @AndyK,

Based on my understanding, you cannot directly change the model in the middle of a conversation; there are no direct methods available. You can create a new chat session and provide the history of the previous one.

You could do something like this:

  const ai = new GoogleGenAI({vertexai: false, apiKey: GEMINI_API_KEY});
  const chat = ai.chats.create({model: 'gemini-2.0-flash'});
  const response = await chat.sendMessage({message: 'Why is the sky blue?'});
  const history = chat.getHistory();

  const chat2 = ai.chats.create({model: 'gemini-2.0-flash-lite',history:history});
const response2 = chat2.sendMessage({message: 'What did you say about the sky?'});

Let me know if this helps or if I’m missing anything. Thank you!

1 Like