!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

You are viewing the **preview** v0.2 docs. View the **stable** v0.1 docs [here](/v0.1/docs/get_started/introduction/). Leave feedback on the v0.2 docs [here](https://github.com/langchain-ai/langchainjs/discussions/5386).

[

![🦜️🔗 Langchain](/v0.2/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.2/img/brand/wordmark-dark.png)

](/v0.2/)[Integrations](/v0.2/docs/integrations/platforms/)[API Reference](https://v02.api.js.langchain.com)

[More](#)

*   [People](/v0.2/docs/people/)
*   [Community](/v0.2/docs/community)
*   [Tutorials](/v0.2/docs/additional_resources/tutorials)
*   [Contributing](/v0.2/docs/contributing)

[v0.2](#)

*   [v0.2](/v0.2/docs/introduction)
*   [v0.1](https://js.langchain.com/v0.1/docs/get_started/introduction)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.2/docs/integrations/platforms/)
    
    *   [Providers](/v0.2/docs/integrations/platforms/)
    *   [Anthropic](/v0.2/docs/integrations/platforms/anthropic)
    *   [AWS](/v0.2/docs/integrations/platforms/aws)
    *   [Google](/v0.2/docs/integrations/platforms/google)
    *   [Microsoft](/v0.2/docs/integrations/platforms/microsoft)
    *   [OpenAI](/v0.2/docs/integrations/platforms/openai)
*   [Components](/v0.2/docs/integrations/components)
    
    *   [Chat models](/v0.2/docs/integrations/chat/)
        
        *   [Chat models](/v0.2/docs/integrations/chat/)
        *   [Alibaba Tongyi](/v0.2/docs/integrations/chat/alibaba_tongyi)
        *   [Anthropic](/v0.2/docs/integrations/chat/anthropic)
        *   [Anthropic Tools](/v0.2/docs/integrations/chat/anthropic_tools)
        *   [Azure OpenAI](/v0.2/docs/integrations/chat/azure)
        *   [Baidu Wenxin](/v0.2/docs/integrations/chat/baidu_wenxin)
        *   [Bedrock](/v0.2/docs/integrations/chat/bedrock)
        *   [Cloudflare Workers AI](/v0.2/docs/integrations/chat/cloudflare_workersai)
        *   [Cohere](/v0.2/docs/integrations/chat/cohere)
        *   [Fake LLM](/v0.2/docs/integrations/chat/fake)
        *   [Fireworks](/v0.2/docs/integrations/chat/fireworks)
        *   [Friendli](/v0.2/docs/integrations/chat/friendli)
        *   [Google GenAI](/v0.2/docs/integrations/chat/google_generativeai)
        *   [(Legacy) Google PaLM/VertexAI](/v0.2/docs/integrations/chat/google_palm)
        *   [Google Vertex AI](/v0.2/docs/integrations/chat/google_vertex_ai)
        *   [Groq](/v0.2/docs/integrations/chat/groq)
        *   [Llama CPP](/v0.2/docs/integrations/chat/llama_cpp)
        *   [Minimax](/v0.2/docs/integrations/chat/minimax)
        *   [Mistral AI](/v0.2/docs/integrations/chat/mistral)
        *   [NIBittensorChatModel](/v0.2/docs/integrations/chat/ni_bittensor)
        *   [Ollama](/v0.2/docs/integrations/chat/ollama)
        *   [Ollama Functions](/v0.2/docs/integrations/chat/ollama_functions)
        *   [OpenAI](/v0.2/docs/integrations/chat/openai)
        *   [PremAI](/v0.2/docs/integrations/chat/premai)
        *   [PromptLayer OpenAI](/v0.2/docs/integrations/chat/prompt_layer_openai)
        *   [TogetherAI](/v0.2/docs/integrations/chat/togetherai)
        *   [WebLLM](/v0.2/docs/integrations/chat/web_llm)
        *   [YandexGPT](/v0.2/docs/integrations/chat/yandex)
        *   [ZhipuAI](/v0.2/docs/integrations/chat/zhipuai)
    *   [LLMs](/v0.2/docs/integrations/llms/)
        
    *   [Embedding models](/v0.2/docs/integrations/text_embedding)
        
    *   [Document loaders](/v0.2/docs/integrations/document_loaders)
        
    *   [Document transformers](/v0.2/docs/integrations/document_transformers)
        
    *   [Vector stores](/v0.2/docs/integrations/vectorstores)
        
    *   [Retrievers](/v0.2/docs/integrations/retrievers)
        
    *   [Tools](/v0.2/docs/integrations/tools)
        
    *   [Toolkits](/v0.2/docs/integrations/toolkits)
        
    *   [Stores](/v0.2/docs/integrations/stores/)
        

*   [](/v0.2/)
*   [Components](/v0.2/docs/integrations/components)
*   [Chat models](/v0.2/docs/integrations/chat/)
*   Google Vertex AI

On this page

ChatVertexAI
============

LangChain.js supports Google Vertex AI chat models as an integration. It supports two different methods of authentication based on whether you're running in a Node environment or a web environment.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

### Node[​](#node "Direct link to Node")

To call Vertex AI models in Node, you'll need to install the `@langchain/google-vertexai` package:

tip

See [this section for general instructions on installing integration packages](/v0.2/docs/how_to/installation#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/google-vertexai

    yarn add @langchain/google-vertexai

    pnpm add @langchain/google-vertexai

tip

We're unifying model params across all packages. We now suggest using `model` instead of `modelName`, and `apiKey` for API keys.

You should make sure the Vertex AI API is enabled for the relevant project and that you've authenticated to Google Cloud using one of these methods:

*   You are logged into an account (using `gcloud auth application-default login`) permitted to that project.
*   You are running on a machine using a service account that is permitted to the project.
*   You have downloaded the credentials for a service account that is permitted to the project and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path of this file.

tip

See [this section for general instructions on installing integration packages](/v0.2/docs/how_to/installation#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/google-vertexai

    yarn add @langchain/google-vertexai

    pnpm add @langchain/google-vertexai

### Web[​](#web "Direct link to Web")

To call Vertex AI models in web environments (like Edge functions), you'll need to install the `@langchain/google-vertexai-web` package:

*   npm
*   Yarn
*   pnpm

    npm install @langchain/google-vertexai-web

    yarn add @langchain/google-vertexai-web

    pnpm add @langchain/google-vertexai-web

Then, you'll need to add your service account credentials directly as a `GOOGLE_VERTEX_AI_WEB_CREDENTIALS` environment variable:

    GOOGLE_VERTEX_AI_WEB_CREDENTIALS={"type":"service_account","project_id":"YOUR_PROJECT-12345",...}

Lastly, you may also pass your credentials directly in code like this:

    import { ChatVertexAI } from "@langchain/google-vertexai-web";const model = new ChatVertexAI({  authOptions: {    credentials: {"type":"service_account","project_id":"YOUR_PROJECT-12345",...},  },});

Usage[​](#usage "Direct link to Usage")
---------------------------------------

The entire family of `gemini` models are available by specifying the `modelName` parameter.

For example:

    import { ChatVertexAI } from "@langchain/google-vertexai";// Or, if using the web entrypoint:// import { ChatVertexAI } from "@langchain/google-vertexai-web";const model = new ChatVertexAI({  temperature: 0.7,  model: "gemini-1.0-pro",});const response = await model.invoke("Why is the ocean blue?");console.log(response);/*AIMessageChunk {  content: [{ type: 'text', text: 'The ocean appears blue due to a phenomenon called Rayleigh scattering. This occurs when sunlight' }],  additional_kwargs: {},  response_metadata: {}} */

#### API Reference:

*   [ChatVertexAI](https://v02.api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html) from `@langchain/google-vertexai`

tip

See the LangSmith trace for the example above [here](https://smith.langchain.com/public/9fb579d8-4987-4302-beca-29a684ae2f4c/r).

### Streaming[​](#streaming "Direct link to Streaming")

`ChatVertexAI` also supports streaming in multiple chunks for faster responses:

    import { ChatVertexAI } from "@langchain/google-vertexai";// Or, if using the web entrypoint:// import { ChatVertexAI } from "@langchain/google-vertexai-web";const model = new ChatVertexAI({  temperature: 0.7,});const stream = await model.stream([  ["system", "You are a funny assistant that answers in pirate language."],  ["human", "What is your favorite food?"],]);for await (const chunk of stream) {  console.log(chunk);}/*AIMessageChunk {  content: [{ type: 'text', text: 'Ahoy there, matey! Me favorite grub be fish and chips, with' }],  additional_kwargs: {},  response_metadata: { data: { candidates: [Array], promptFeedback: [Object] } }}AIMessageChunk {  content: [{ type: 'text', text: " a hearty pint o' grog to wash it down. What be yer fancy, landlubber?" }],  additional_kwargs: {},  response_metadata: { data: { candidates: [Array] } }}AIMessageChunk {  content: '',  additional_kwargs: {},  response_metadata: { finishReason: 'stop' }}*/

#### API Reference:

*   [ChatVertexAI](https://v02.api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html) from `@langchain/google-vertexai`

tip

See the LangSmith trace for the example above [here](https://smith.langchain.com/public/ba4cb190-3f60-49aa-a6f8-7d31316d94cf/r).

### Tool calling[​](#tool-calling "Direct link to Tool calling")

`ChatVertexAI` also supports calling the model with a tool:

    import { ChatVertexAI } from "@langchain/google-vertexai";import { type GeminiTool } from "@langchain/google-vertexai/types";import { zodToGeminiParameters } from "@langchain/google-vertexai/utils";import { z } from "zod";// Or, if using the web entrypoint:// import { ChatVertexAI } from "@langchain/google-vertexai-web";const calculatorSchema = z.object({  operation: z    .enum(["add", "subtract", "multiply", "divide"])    .describe("The type of operation to execute"),  number1: z.number().describe("The first number to operate on."),  number2: z.number().describe("The second number to operate on."),});const geminiCalculatorTool: GeminiTool = {  functionDeclarations: [    {      name: "calculator",      description: "A simple calculator tool",      parameters: zodToGeminiParameters(calculatorSchema),    },  ],};const model = new ChatVertexAI({  temperature: 0.7,  model: "gemini-1.0-pro",}).bind({  tools: [geminiCalculatorTool],});const response = await model.invoke("What is 1628253239 times 81623836?");console.log(JSON.stringify(response.additional_kwargs, null, 2));/*{  "tool_calls": [    {      "id": "calculator",      "type": "function",      "function": {        "name": "calculator",        "arguments": "{\"number2\":81623836,\"number1\":1628253239,\"operation\":\"multiply\"}"      }    }  ],} */

#### API Reference:

*   [ChatVertexAI](https://v02.api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html) from `@langchain/google-vertexai`
*   [GeminiTool](https://v02.api.js.langchain.com/interfaces/langchain_google_common_types.GeminiTool.html) from `@langchain/google-vertexai/types`
*   [zodToGeminiParameters](https://v02.api.js.langchain.com/functions/langchain_google_common.zodToGeminiParameters.html) from `@langchain/google-vertexai/utils`

tip

See the LangSmith trace for the example above [here](https://smith.langchain.com/public/49e1c32c-395a-45e2-afba-913aa3389137/r).

### `withStructuredOutput`[​](#withstructuredoutput "Direct link to withstructuredoutput")

Alternatively, you can also use the `withStructuredOutput` method:

    import { ChatVertexAI } from "@langchain/google-vertexai";import { z } from "zod";// Or, if using the web entrypoint:// import { ChatVertexAI } from "@langchain/google-vertexai-web";const calculatorSchema = z.object({  operation: z    .enum(["add", "subtract", "multiply", "divide"])    .describe("The type of operation to execute"),  number1: z.number().describe("The first number to operate on."),  number2: z.number().describe("The second number to operate on."),});const model = new ChatVertexAI({  temperature: 0.7,  model: "gemini-1.0-pro",}).withStructuredOutput(calculatorSchema);const response = await model.invoke("What is 1628253239 times 81623836?");console.log(response);/*{ operation: 'multiply', number1: 1628253239, number2: 81623836 } */

#### API Reference:

*   [ChatVertexAI](https://v02.api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html) from `@langchain/google-vertexai`

tip

See the LangSmith trace for the example above [here](https://smith.langchain.com/public/41bbbddb-f357-4bfa-a111-def8294a4514/r).

### VertexAI tools agent[​](#vertexai-tools-agent "Direct link to VertexAI tools agent")

The Gemini family of models not only support tool calling, but can also be used in the Tool Calling agent. Here's an example:

    import { z } from "zod";import { DynamicStructuredTool } from "@langchain/core/tools";import { AgentExecutor, createToolCallingAgent } from "langchain/agents";import { ChatPromptTemplate } from "@langchain/core/prompts";import { ChatVertexAI } from "@langchain/google-vertexai";// Uncomment this if you're running inside a web/edge environment.// import { ChatVertexAI } from "@langchain/google-vertexai-web";const llm: any = new ChatVertexAI({  temperature: 0,});// Prompt template must have "input" and "agent_scratchpad input variables"const prompt = ChatPromptTemplate.fromMessages([  ["system", "You are a helpful assistant"],  ["placeholder", "{chat_history}"],  ["human", "{input}"],  ["placeholder", "{agent_scratchpad}"],]);const currentWeatherTool = new DynamicStructuredTool({  name: "get_current_weather",  description: "Get the current weather in a given location",  schema: z.object({    location: z.string().describe("The city and state, e.g. San Francisco, CA"),  }),  func: async () => Promise.resolve("28 °C"),});const agent = await createToolCallingAgent({  llm,  tools: [currentWeatherTool],  prompt,});const agentExecutor = new AgentExecutor({  agent,  tools: [currentWeatherTool],});const input = "What's the weather like in Paris?";const { output } = await agentExecutor.invoke({ input });console.log(output);/* It's 28 degrees Celsius in Paris.*/

#### API Reference:

*   [DynamicStructuredTool](https://v02.api.js.langchain.com/classes/langchain_core_tools.DynamicStructuredTool.html) from `@langchain/core/tools`
*   [AgentExecutor](https://v02.api.js.langchain.com/classes/langchain_agents.AgentExecutor.html) from `langchain/agents`
*   [createToolCallingAgent](https://v02.api.js.langchain.com/functions/langchain_agents.createToolCallingAgent.html) from `langchain/agents`
*   [ChatPromptTemplate](https://v02.api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [ChatVertexAI](https://v02.api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html) from `@langchain/google-vertexai`

tip

See the LangSmith trace for the agent example above [here](https://smith.langchain.com/public/5615ee35-ba76-433b-8639-9b321cb6d4bf/r).

* * *

#### Was this page helpful?

  

#### You can leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

(Legacy) Google PaLM/VertexAI

](/v0.2/docs/integrations/chat/google_palm)[

Next

Groq

](/v0.2/docs/integrations/chat/groq)

*   [Setup](#setup)
    *   [Node](#node)
    *   [Web](#web)
*   [Usage](#usage)
    *   [Streaming](#streaming)
    *   [Tool calling](#tool-calling)
    *   [`withStructuredOutput`](#withstructuredoutput)
    *   [VertexAI tools agent](#vertexai-tools-agent)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.