LangChain.js integration for Google Vertex AI - access Google AI/ML models and services using service account credentials. Supports chat models, embeddings, and tool calling with Gemini.
Integrate Google Vertex AI models and services into your LangChain.js applications. This skill provides access to Google's AI/ML capabilities including Gemini models, embeddings, and other Vertex AI services using service account authentication.
Use this skill when you need to:
**Note:** If you're running in an environment where service account credentials cannot be provided (e.g., browser environments), use `@langchain/google-vertexai-web` instead.
Install the package:
```bash
pnpm install @langchain/google-vertexai
```
Or with npm:
```bash
npm install @langchain/google-vertexai
```
Configure authentication using one of these methods (in order of precedence):
```typescript
import { ChatVertexAI } from "@langchain/google-vertexai";
const model = new ChatVertexAI({
apiKey: "your-api-key-here"
});
```
```typescript
const model = new ChatVertexAI({
authInfo: {
// Service account credentials object
}
});
```
```bash
export API_KEY="your-api-key-here"
```
```bash
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-key.json"
```
If running on GCP or after running:
```bash
gcloud auth application-default login
```
```typescript
import { ChatVertexAI } from "@langchain/google-vertexai";
// Initialize the model
const model = new ChatVertexAI({
model: "gemini-pro",
temperature: 0.7,
});
// Invoke the model
const response = await model.invoke("What is LangChain?");
console.log(response.content);
```
```typescript
import { VertexAIEmbeddings } from "@langchain/google-vertexai";
const embeddings = new VertexAIEmbeddings({
model: "textembedding-gecko"
});
const vectors = await embeddings.embedDocuments([
"Hello world",
"LangChain is awesome"
]);
```
When using tools with Gemini models, follow these schema constraints:
```typescript
import { z } from "zod";
import { tool } from "@langchain/core/tools";
// ✅ Flat object structures
const weatherTool = tool(
async ({ location, unit }) => {
// Tool implementation
return `Weather in ${location}`;
},
{
name: "get_weather",
description: "Get weather for a location",
schema: z.object({
location: z.string().describe("City name"),
unit: z.enum(["celsius", "fahrenheit"]).optional(),
}),
}
);
// ✅ Enums instead of unions
const calculatorTool = tool(
async ({ operation, a, b }) => {
// Implementation
},
{
name: "calculator",
schema: z.object({
operation: z.enum(["add", "subtract", "multiply", "divide"]),
a: z.number(),
b: z.number(),
}),
}
);
// ✅ Optional fields instead of discriminated unions
const dataTool = tool(
async ({ type, stringValue, numberValue }) => {
// Implementation
},
{
name: "process_data",
schema: z.object({
type: z.enum(["text", "number"]),
stringValue: z.string().optional(),
numberValue: z.number().optional(),
}),
}
);
```
```typescript
// ❌ Discriminated unions - NOT SUPPORTED
z.discriminatedUnion("type", [
z.object({ type: z.literal("a"), value: z.string() }),
z.object({ type: z.literal("b"), value: z.number() }),
]);
// ❌ Union types - NOT SUPPORTED
z.union([z.string(), z.number()]);
// ⚠️ Positive refinement - automatically converted to .min(0.01)
z.number().positive(); // Use z.number().min(0.01) instead
```
```typescript
import { ChatVertexAI } from "@langchain/google-vertexai";
import { z } from "zod";
import { tool } from "@langchain/core/tools";
// Define tool
const searchTool = tool(
async ({ query }) => {
return `Search results for: ${query}`;
},
{
name: "web_search",
description: "Search the web",
schema: z.object({
query: z.string().describe("Search query"),
limit: z.number().min(1).max(10).optional(),
}),
}
);
// Bind tools to model
const model = new ChatVertexAI({
model: "gemini-pro"
}).bindTools([searchTool]);
// Invoke with tools
const response = await model.invoke("Search for LangChain documentation");
```
1. **Use Simple Schemas**: Keep tool schemas flat and straightforward
2. **Prefer Enums**: Use `z.enum()` instead of union types or discriminated unions
3. **Explicit Constraints**: Use `.min()` and `.max()` explicitly instead of `.positive()`
4. **Test Schemas**: Validate tool schemas before production deployment
5. **Optional Fields**: Use `.optional()` for flexibility instead of complex union types
6. **Clear Descriptions**: Add `.describe()` to all schema fields for better model understanding
```typescript
const stream = await model.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk.content);
}
```
```typescript
import { HumanMessage } from "@langchain/core/messages";
const response = await model.invoke([
new HumanMessage({
content: [
{ type: "text", text: "What's in this image?" },
{ type: "image_url", image_url: "https://example.com/image.jpg" }
]
})
]);
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/langchain-google-vertex-ai/raw