TheChatSessionclass is used to make multiturn send message requests. You can instantiate this class by using thestartChatmethod in theGenerativeModelclass. ThesendMessagemethod makes an async call to get the response of a chat message at at once. ThesendMessageStreammethod makes an async call to stream the response of a chat message as it's being generated.
constchat=generativeModel.startChat();constresult1=awaitchat.sendMessage("How can I learn more about Node.js?");console.log('Response: ',JSON.stringify(result1.response));constresult2=awaitchat.sendMessage("What about python?");console.log('Response: ',JSON.stringify(result2.response));
constchat=generativeModel.startChat();constchatInput="How can I learn more about Node.js?";constresult=awaitchat.sendMessageStream(chatInput);forawait(constitemofresult.stream){console.log(item.candidates[0].content.parts[0].text);}constresponse=awaitresult.response;console.log('aggregated response: ',JSON.stringify(result.response));
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Class ChatSession (1.10.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.10.0 (latest)](/nodejs/docs/reference/vertexai/latest/vertexai/chatsession)\n- [1.9.0](/nodejs/docs/reference/vertexai/1.9.0/vertexai/chatsession)\n- [1.8.1](/nodejs/docs/reference/vertexai/1.8.1/vertexai/chatsession)\n- [1.7.0](/nodejs/docs/reference/vertexai/1.7.0/vertexai/chatsession)\n- [1.6.0](/nodejs/docs/reference/vertexai/1.6.0/vertexai/chatsession)\n- [1.5.0](/nodejs/docs/reference/vertexai/1.5.0/vertexai/chatsession)\n- [1.4.1](/nodejs/docs/reference/vertexai/1.4.1/vertexai/chatsession)\n- [1.3.1](/nodejs/docs/reference/vertexai/1.3.1/vertexai/chatsession)\n- [1.2.0](/nodejs/docs/reference/vertexai/1.2.0/vertexai/chatsession)\n- [1.1.0](/nodejs/docs/reference/vertexai/1.1.0/vertexai/chatsession)\n- [1.0.0](/nodejs/docs/reference/vertexai/1.0.0/vertexai/chatsession)\n- [0.5.0](/nodejs/docs/reference/vertexai/0.5.0/vertexai/chatsession)\n- [0.4.0](/nodejs/docs/reference/vertexai/0.4.0/vertexai/chatsession)\n- [0.3.1](/nodejs/docs/reference/vertexai/0.3.1/vertexai/chatsession)\n- [0.2.1](/nodejs/docs/reference/vertexai/0.2.1/vertexai/chatsession) \nThe `ChatSession` class is used to make multiturn send message requests. You can instantiate this class by using the `startChat` method in the `GenerativeModel` class. The `sendMessage` method makes an async call to get the response of a chat message at at once. The `sendMessageStream` method makes an async call to stream the response of a chat message as it's being generated.\n\nPackage\n-------\n\n[@google-cloud/vertexai](../overview.html)\n\nConstructors\n------------\n\n### (constructor)(request, requestOptions)\n\n constructor(request: StartChatSessionRequest, requestOptions?: RequestOptions);\n\nConstructs a new instance of the `ChatSession` class\n\nProperties\n----------\n\n### requestOptions\n\n protected readonly requestOptions?: RequestOptions;\n\nMethods\n-------\n\n### getHistory()\n\n getHistory(): Promise\u003cContent[]\u003e;\n\n### sendMessage(request)\n\n sendMessage(request: string | Array\u003cstring | Part\u003e): Promise\u003cGenerateContentResult\u003e;\n\nMakes an async call to send chat message.\n\nThe response is returned in [GenerateContentResult.response](/nodejs/docs/reference/vertexai/latest/vertexai/generatecontentresult).\n\n**Example** \n\n\n const chat = generativeModel.startChat();\n const result1 = await chat.sendMessage(\"How can I learn more about Node.js?\");\n console.log('Response: ', JSON.stringify(result1.response));\n\n const result2 = await chat.sendMessage(\"What about python?\");\n console.log('Response: ', JSON.stringify(result2.response));\n\n### sendMessageStream(request)\n\n sendMessageStream(request: string | Array\u003cstring | Part\u003e): Promise\u003cStreamGenerateContentResult\u003e;\n\nMakes an async call to stream send message.\n\nThe response is streamed chunk by chunk in [StreamGenerateContentResult.stream](/nodejs/docs/reference/vertexai/latest/vertexai/streamgeneratecontentresult). The aggregated response is avaliable in [StreamGenerateContentResult.response](/nodejs/docs/reference/vertexai/latest/vertexai/streamgeneratecontentresult) after all chunks are returned.\n\n**Example** \n\n\n const chat = generativeModel.startChat();\n const chatInput = \"How can I learn more about Node.js?\";\n const result = await chat.sendMessageStream(chatInput);\n for await (const item of result.stream) {\n console.log(item.candidates[0].content.parts[0].text);\n }\n const response = await result.response;\n console.log('aggregated response: ', JSON.stringify(result.response));"]]