This package contains resources to access Google AI/ML models and other Google services via Vertex AI. Authorization to these services use service account credentials stored on the local file system or provided through the Google Cloud Platform environment it is running on.
If you are running this on a platform where the credentials cannot be provided this way, consider using the @langchain/google-vertexai-web package instead. You do not need to use both packages. See the section on Authorization below.
$ pnpm install @langchain/google-vertexai
Authorization is done through a Google Cloud Service Account.
To handle service accounts, this package uses the google-auth-library
package, and you may wish to consult the documentation for that library
about how it does so. But in short, classes in this package will use
credentials from the first of the following that apply:
apiKey attributeauthInfo attributeAPI_KEYGOOGLE_APPLICATION_CREDENTIALS environment
variable.gcloud auth application-default login, then the
default credentials.To route ChatVertexAI through the LangSmith gateway, set
LANGSMITH_GATEWAY=true and LANGSMITH_GATEWAY_API_KEY. The gateway key falls
back to LANGSMITH_API_KEY. You can also set LANGSMITH_GATEWAY to a custom
gateway root URL.
import { ChatVertexAI } from "@langchain/google-vertexai";
const model = new ChatVertexAI({ model: "gemini-2.5-flash" });
const response = await model.invoke("Hello!");
console.log(response.content);
Gateway requests use the /vertex route and do not require Google credentials
when using Vertex Express. Passing your own credentials (authOptions or
apiKey) or an explicit endpoint opts out of gateway routing and sends
requests straight to Vertex AI.
When using tools with Gemini models through Vertex AI, be aware of the following Zod schema limitations:
.discriminatedUnion() is not supported// ❌ This will throw an error
z.discriminatedUnion("type", [
z.object({ type: z.literal("a"), value: z.string() }),
z.object({ type: z.literal("b"), value: z.number() }),
]);
// ✅ Use a flat object with optional fields instead
z.object({
type: z.enum(["a", "b"]),
stringValue: z.string().optional(),
numberValue: z.number().optional(),
});
z.union() is not supported// ❌ This will throw an error
z.union([z.string(), z.number()]);
// ✅ Consider using separate optional fields
z.object({
stringValue: z.string().optional(),
numberValue: z.number().optional(),
});
.positive() is automatically converted// ⚠️ This is automatically converted to .min(0.01)
z.number().positive();
// ✅ Prefer using .min() directly
z.number().min(0.01);
If you use unsupported schema features, you'll receive descriptive error messages:
"Gemini cannot handle union types (discriminatedUnion, anyOf, oneOf)""Failed to convert tool '[toolName]' schema for Gemini".min() instead of .positive() for number constraintsIntegration with Google Vertex AI chat models.
Setup:
Install @langchain/google-vertexai and set your stringified
Vertex AI credentials as an environment variable named `GOOGLE_APPLICATION_CREDENTI
Integration with a Google Vertex AI LLM using the "@langchain/google-gauth" package for auth.
Integration with a Google Vertex AI embeddings model using the "@langchain/google-gauth" package for auth.