Chat session that enables sending messages to the model with previous conversation context.

The session maintains all the turns between user and model.

Constructors

Methods

  • Returns the chat history.

    Parameters

    • curated: boolean = false

      whether to return the curated history or the comprehensive history.

    Returns Content[]

    History contents alternating between user and model for the entire chat session.

    The history is a list of contents alternating between user and model.

    There are two types of history:

    • The curated history contains only the valid turns between user and model, which will be included in the subsequent requests sent to the model.
    • The comprehensive history contains all turns, including invalid or empty model outputs, providing a complete record of the history.

    The history is updated after receiving the response from the model, for streaming response, it means receiving the last chunk of the response.

    The comprehensive history is returned by default. To get the curated history, set the curated parameter to true.

  • Sends a message to the model and returns the response.

    Parameters

    Returns Promise<GenerateContentResponse>

    The model's response.

    This method will wait for the previous message to be processed before sending the next message.

    Chat#sendMessageStream for streaming method.

    const chat = ai.chats.create({model: 'gemini-2.0-flash'});
    const response = await chat.sendMessage({
    message: 'Why is the sky blue?'
    });
    console.log(response.text);
  • Sends a message to the model and returns the response in chunks.

    Parameters

    Returns Promise<AsyncGenerator<GenerateContentResponse, any, unknown>>

    The model's response.

    This method will wait for the previous message to be processed before sending the next message.

    Chat#sendMessage for non-streaming method.

    const chat = ai.chats.create({model: 'gemini-2.0-flash'});
    const response = await chat.sendMessageStream({
    message: 'Why is the sky blue?'
    });
    for await (const chunk of response) {
    console.log(chunk.text);
    }
MMNEPVFCICPMFPCPTTAAATR