Google Vertex Provider
The Google Vertex provider for the AI SDK contains language model support for the Google Vertex AI APIs. This includes support for Google's Gemini models and Anthropic's Claude partner models.
The Google Vertex provider is compatible with both Node.js and Edge runtimes.
The Edge runtime is supported through the @ai-sdk/google-vertex/edge
sub-module. More details can be found in the Google Vertex Edge
Runtime and Google Vertex Anthropic Edge
Runtime sections below.
Setup
The Google Vertex and Google Vertex Anthropic providers are both available in the @ai-sdk/google-vertex module. You can install it with
pnpm add @ai-sdk/google-vertex
Google Vertex Provider Usage
The Google Vertex provider instance is used to create model instances that call the Vertex AI API. The models available with this provider include Google's Gemini models. If you're looking to use Anthropic's Claude models, see the Google Vertex Anthropic Provider section below.
Provider Instance
You can import the default provider instance vertex from @ai-sdk/google-vertex:
import { vertex } from '@ai-sdk/google-vertex';If you need a customized setup, you can import createVertex from @ai-sdk/google-vertex and create a provider instance with your settings:
import { createVertex } from '@ai-sdk/google-vertex';
const vertex = createVertex({ project: 'my-project', // optional location: 'us-central1', // optional});Google Vertex supports two different authentication implementations depending on your runtime environment.
Node.js Runtime
The Node.js runtime is the default runtime supported by the AI SDK. It supports all standard Google Cloud authentication options through the google-auth-library. Typical use involves setting a path to a json credentials file in the GOOGLE_APPLICATION_CREDENTIALS environment variable. The credentials file can be obtained from the Google Cloud Console.
If you want to customize the Google authentication options you can pass them as options to the createVertex function, for example:
import { createVertex } from '@ai-sdk/google-vertex';
const vertex = createVertex({ googleAuthOptions: { credentials: { client_email: 'my-email', private_key: 'my-private-key', }, },});Optional Provider Settings
You can use the following optional settings to customize the provider instance:
-
project string
The Google Cloud project ID that you want to use for the API calls. It uses the
GOOGLE_VERTEX_PROJECTenvironment variable by default. -
location string
The Google Cloud location that you want to use for the API calls, e.g.
us-central1. It uses theGOOGLE_VERTEX_LOCATIONenvironment variable by default. -
googleAuthOptions object
Optional. The Authentication options used by the Google Auth Library. See also the GoogleAuthOptions interface.
-
authClient object An
AuthClientto use. -
keyFilename string Path to a .json, .pem, or .p12 key file.
-
keyFile string Path to a .json, .pem, or .p12 key file.
-
credentials object Object containing client_email and private_key properties, or the external account client options.
-
clientOptions object Options object passed to the constructor of the client.
-
scopes string | string[] Required scopes for the desired API request.
-
projectId string Your project ID.
-
universeDomain string The default service domain for a given Cloud universe.
-
-
headers Resolvable<Record<string, string | undefined>>
Headers to include in the requests. Can be provided in multiple formats:
- A record of header key-value pairs:
Record<string, string | undefined> - A function that returns headers:
() => Record<string, string | undefined> - An async function that returns headers:
async () => Record<string, string | undefined> - A promise that resolves to headers:
Promise<Record<string, string | undefined>>
- A record of header key-value pairs:
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. Defaults to the global
fetchfunction. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing. -
baseURL string
Optional. Base URL for the Google Vertex API calls e.g. to use proxy servers. By default, it is constructed using the location and project:
https://${location}-aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/google
Edge Runtime
Edge runtimes (like Vercel Edge Functions and Cloudflare Workers) are lightweight JavaScript environments that run closer to users at the network edge. They only provide a subset of the standard Node.js APIs. For example, direct file system access is not available, and many Node.js-specific libraries (including the standard Google Auth library) are not compatible.
The Edge runtime version of the Google Vertex provider supports Google's Application Default Credentials through environment variables. The values can be obtained from a json credentials file from the Google Cloud Console.
You can import the default provider instance vertex from @ai-sdk/google-vertex/edge:
import { vertex } from '@ai-sdk/google-vertex/edge';The /edge sub-module is included in the @ai-sdk/google-vertex package, so
you don't need to install it separately. You must import from
@ai-sdk/google-vertex/edge to differentiate it from the Node.js provider.
If you need a customized setup, you can import createVertex from @ai-sdk/google-vertex/edge and create a provider instance with your settings:
import { createVertex } from '@ai-sdk/google-vertex/edge';
const vertex = createVertex({ project: 'my-project', // optional location: 'us-central1', // optional});For Edge runtime authentication, you'll need to set these environment variables from your Google Default Application Credentials JSON file:
GOOGLE_CLIENT_EMAILGOOGLE_PRIVATE_KEYGOOGLE_PRIVATE_KEY_ID(optional)
These values can be obtained from a service account JSON file from the Google Cloud Console.
Optional Provider Settings
You can use the following optional settings to customize the provider instance:
-
project string
The Google Cloud project ID that you want to use for the API calls. It uses the
GOOGLE_VERTEX_PROJECTenvironment variable by default. -
location string
The Google Cloud location that you want to use for the API calls, e.g.
us-central1. It uses theGOOGLE_VERTEX_LOCATIONenvironment variable by default. -
googleCredentials object
Optional. The credentials used by the Edge provider for authentication. These credentials are typically set through environment variables and are derived from a service account JSON file.
-
clientEmail string The client email from the service account JSON file. Defaults to the contents of the
GOOGLE_CLIENT_EMAILenvironment variable. -
privateKey string The private key from the service account JSON file. Defaults to the contents of the
GOOGLE_PRIVATE_KEYenvironment variable. -
privateKeyId string The private key ID from the service account JSON file (optional). Defaults to the contents of the
GOOGLE_PRIVATE_KEY_IDenvironment variable.
-
-
headers Resolvable<Record<string, string | undefined>>
Headers to include in the requests. Can be provided in multiple formats:
- A record of header key-value pairs:
Record<string, string | undefined> - A function that returns headers:
() => Record<string, string | undefined> - An async function that returns headers:
async () => Record<string, string | undefined> - A promise that resolves to headers:
Promise<Record<string, string | undefined>>
- A record of header key-value pairs:
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. Defaults to the global
fetchfunction. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.
Language Models
You can create models that call the Vertex API using the provider instance.
The first argument is the model id, e.g. gemini-1.5-pro.
const model = vertex('gemini-1.5-pro');If you are using your own
models, the name
of your model needs to start with projects/.
Google Vertex models support also some model specific settings that are not part of the standard call settings. You can pass them as an options argument:
const model = vertex('gemini-1.5-pro');
await generateText({ model, providerOptions: { google: { safetySettings: [ { category: 'HARM_CATEGORY_UNSPECIFIED', threshold: 'BLOCK_LOW_AND_ABOVE', }, ], }, },});The following optional provider options are available for Google Vertex models:
-
structuredOutputs boolean
Optional. Enable structured output. Default is true.
This is useful when the JSON Schema contains elements that are not supported by the OpenAPI schema version that Google Vertex uses. You can use this to disable structured outputs if you need to.
See Troubleshooting: Schema Limitations for more details.
-
safetySettings Array<{ category: string; threshold: string }>
Optional. Safety settings for the model.
-
category string
The category of the safety setting. Can be one of the following:
HARM_CATEGORY_UNSPECIFIEDHARM_CATEGORY_HATE_SPEECHHARM_CATEGORY_DANGEROUS_CONTENTHARM_CATEGORY_HARASSMENTHARM_CATEGORY_SEXUALLY_EXPLICITHARM_CATEGORY_CIVIC_INTEGRITY
-
threshold string
The threshold of the safety setting. Can be one of the following:
HARM_BLOCK_THRESHOLD_UNSPECIFIEDBLOCK_LOW_AND_ABOVEBLOCK_MEDIUM_AND_ABOVEBLOCK_ONLY_HIGHBLOCK_NONE
-
-
audioTimestamp boolean
Optional. Enables timestamp understanding for audio files. Defaults to false.
This is useful for generating transcripts with accurate timestamps. Consult Google's Documentation for usage details.
-
labels object
Optional. Defines labels used in billing reports.
Consult Google's Documentation for usage details.
You can use Google Vertex language models to generate text with the generateText function:
import { vertex } from '@ai-sdk/google-vertex';import { generateText } from 'ai';
const { text } = await generateText({ model: vertex('gemini-1.5-pro'), prompt: 'Write a vegetarian lasagna recipe for 4 people.',});Google Vertex language models can also be used in the streamText function
(see AI SDK Core).
Code Execution
With Code Execution, certain Gemini models on Vertex AI can generate and execute Python code. This allows the model to perform calculations, data manipulation, and other programmatic tasks to enhance its responses.
You can enable code execution by adding the code_execution tool to your request.
import { vertex } from '@ai-sdk/google-vertex';import { generateText } from 'ai';
const result = await generateText({ model: vertex('gemini-2.5-pro'), tools: { code_execution: vertex.tools.codeExecution({}) }, prompt: 'Use python to calculate 20th fibonacci number. Then find the nearest palindrome to it.',});The response will contain tool-call and tool-result parts for the executed code.
URL Context
URL Context allows Gemini models to retrieve and analyze content from URLs. Supported models: Gemini 2.5 Flash-Lite, 2.5 Pro, 2.5 Flash, 2.0 Flash.
import { vertex } from '@ai-sdk/google-vertex';import { generateText } from 'ai';
const result = await generateText({ model: vertex('gemini-2.5-pro'), tools: { url_context: vertex.tools.urlContext({}) }, prompt: 'What are the key points from https://example.com/article?',});Google Search
Google Search enables Gemini models to access real-time web information. Supported models: Gemini 2.5 Flash-Lite, 2.5 Flash, 2.0 Flash, 2.5 Pro.
import { vertex } from '@ai-sdk/google-vertex';import { generateText } from 'ai';
const result = await generateText({ model: vertex('gemini-2.5-pro'), tools: { google_search: vertex.tools.googleSearch({}) }, prompt: 'What are the latest developments in AI?',});Reasoning (Thinking Tokens)
Google Vertex AI, through its support for Gemini models, can also emit "thinking" tokens, representing the model's reasoning process. The AI SDK exposes these as reasoning information.
To enable thinking tokens for compatible Gemini models via Vertex, set includeThoughts: true in the thinkingConfig provider option. Since the Vertex provider uses the Google provider's underlying language model, these options are passed through providerOptions.google:
import { vertex } from '@ai-sdk/google-vertex';import { GoogleGenerativeAIProviderOptions } from '@ai-sdk/google'; // Note: importing from @ai-sdk/googleimport { generateText, streamText } from 'ai';
// For generateText:const { text, reasoning, reasoningDetails } = await generateText({ model: vertex('gemini-2.0-flash-001'), // Or other supported model via Vertex providerOptions: { google: { // Options are nested under 'google' for Vertex provider thinkingConfig: { includeThoughts: true, // thinkingBudget: 2048, // Optional }, } satisfies GoogleGenerativeAIProviderOptions, }, prompt: 'Explain quantum computing in simple terms.',});
console.log('Reasoning:', reasoning);console.log('Reasoning Details:', reasoningDetails);console.log('Final Text:', text);
// For streamText:const result = streamText({ model: vertex('gemini-2.0-flash-001'), // Or other supported model via Vertex providerOptions: { google: { // Options are nested under 'google' for Vertex provider thinkingConfig: { includeThoughts: true, // thinkingBudget: 2048, // Optional }, } satisfies GoogleGenerativeAIProviderOptions, }, prompt: 'Explain quantum computing in simple terms.',});
for await (const part of result.fullStream) { if (part.type === 'reasoning') { process.stdout.write(`THOUGHT: ${part.textDelta}\n`); } else if (part.type === 'text-delta') { process.stdout.write(part.textDelta); }}When includeThoughts is true, parts of the API response marked with thought: true will be processed as reasoning.
- In
generateText, these contribute to thereasoning(string) andreasoningDetails(array) fields. - In
streamText, these are emitted asreasoningstream parts.
Refer to the Google Vertex AI documentation on "thinking" for model compatibility and further details.
File Inputs
The Google Vertex provider supports file inputs, e.g. PDF files.
import { vertex } from '@ai-sdk/google-vertex';import { generateText } from 'ai';
const { text } = await generateText({ model: vertex('gemini-1.5-pro'), messages: [ { role: 'user', content: [ { type: 'text', text: 'What is an embedding model according to this document?', }, { type: 'file', data: fs.readFileSync('./data/ai.pdf'), mediaType: 'application/pdf', }, ], }, ],});The AI SDK will automatically download URLs if you pass them as data, except
for gs:// URLs. You can use the Google Cloud Storage API to upload larger
files to that location.
See File Parts for details on how to use files in prompts.
Safety Ratings
The safety ratings provide insight into the safety of the model's response. See Google Vertex AI documentation on configuring safety filters.
Example response excerpt:
{ "safetyRatings": [ { "category": "HARM_CATEGORY_HATE_SPEECH", "probability": "NEGLIGIBLE", "probabilityScore": 0.11027937, "severity": "HARM_SEVERITY_LOW", "severityScore": 0.28487435 }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "probability": "HIGH", "blocked": true, "probabilityScore": 0.95422274, "severity": "HARM_SEVERITY_MEDIUM", "severityScore": 0.43398145 }, { "category": "HARM_CATEGORY_HARASSMENT", "probability": "NEGLIGIBLE", "probabilityScore": 0.11085559, "severity": "HARM_SEVERITY_NEGLIGIBLE", "severityScore": 0.19027223 }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "probability": "NEGLIGIBLE", "probabilityScore": 0.22901751, "severity": "HARM_SEVERITY_NEGLIGIBLE", "severityScore": 0.09089675 } ]}For more details, see the Google Vertex AI documentation on grounding with Google Search.
Troubleshooting
Schema Limitations
The Google Vertex API uses a subset of the OpenAPI 3.0 schema, which does not support features such as unions. The errors that you get in this case look like this:
GenerateContentRequest.generation_config.response_schema.properties[occupation].type: must be specified
By default, structured outputs are enabled (and for tool calling they are required). You can disable structured outputs for object generation as a workaround:
const result = await generateObject({ model: vertex('gemini-1.5-pro'), providerOptions: { google: { structuredOutputs: false, }, }, schema: z.object({ name: z.string(), age: z.number(), contact: z.union([ z.object({ type: z.literal('email'), value: z.string(), }), z.object({ type: z.literal('phone'), value: z.string(), }), ]), }), prompt: 'Generate an example person for testing.',});The following Zod features are known to not work with Google Vertex:
z.unionz.record
Model Capabilities
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
|---|---|---|---|---|
gemini-2.0-flash-001 | ||||
gemini-2.0-flash-exp | ||||
gemini-1.5-flash | ||||
gemini-1.5-pro |
The table above lists popular models. Please see the Google Vertex AI docs for a full list of available models. The table above lists popular models. You can also pass any available provider model ID as a string if needed.
Embedding Models
You can create models that call the Google Vertex AI embeddings API using the .textEmbeddingModel() factory method:
const model = vertex.textEmbeddingModel('text-embedding-004');Google Vertex AI embedding models support additional settings. You can pass them as an options argument:
import { vertex } from '@ai-sdk/google-vertex';import { embed } from 'ai';
const model = vertex.textEmbeddingModel('text-embedding-004');
const { embedding } = await embed({ model, value: 'sunny day at the beach', providerOptions: { google: { outputDimensionality: 512, // optional, number of dimensions for the embedding taskType: 'SEMANTIC_SIMILARITY', // optional, specifies the task type for generating embeddings autoTruncate: false, // optional }, },});The following optional provider options are available for Google Vertex AI embedding models:
-
outputDimensionality: number
Optional reduced dimension for the output embedding. If set, excessive values in the output embedding are truncated from the end.
-
taskType: string
Optional. Specifies the task type for generating embeddings. Supported task types include:
SEMANTIC_SIMILARITY: Optimized for text similarity.CLASSIFICATION: Optimized for text classification.CLUSTERING: Optimized for clustering texts based on similarity.RETRIEVAL_DOCUMENT: Optimized for document retrieval.RETRIEVAL_QUERY: Optimized for query-based retrieval.QUESTION_ANSWERING: Optimized for answering questions.FACT_VERIFICATION: Optimized for verifying factual information.CODE_RETRIEVAL_QUERY: Optimized for retrieving code blocks based on natural language queries.
-
title: string
Optional. The title of the document being embedded. This helps the model produce better embeddings by providing additional context. Only valid when
taskTypeis set to'RETRIEVAL_DOCUMENT'. -
autoTruncate: boolean
Optional. When set to
true, input text will be truncated if it exceeds the maximum length. When set tofalse, an error is returned if the input text is too long. Defaults totrue.
Model Capabilities
| Model | Max Values Per Call | Parallel Calls |
|---|---|---|
text-embedding-004 | 2048 |
The table above lists popular models. You can also pass any available provider model ID as a string if needed.
Image Models
You can create Imagen models that call the Imagen on Vertex AI API
using the .image() factory method. For more on image generation with the AI SDK see generateImage().
import { vertex } from '@ai-sdk/google-vertex';import { experimental_generateImage as generateImage } from 'ai';
const { image } = await generateImage({ model: vertex.image('imagen-3.0-generate-002'), prompt: 'A futuristic cityscape at sunset', aspectRatio: '16:9',});Further configuration can be done using Google Vertex provider options. You can validate the provider options using the GoogleVertexImageProviderOptions type.
import { vertex } from '@ai-sdk/google-vertex';import { GoogleVertexImageProviderOptions } from '@ai-sdk/google-vertex';import { experimental_generateImage as generateImage } from 'ai';
const { image } = await generateImage({ model: vertex.image('imagen-3.0-generate-002'), providerOptions: { vertex: { negativePrompt: 'pixelated, blurry, low-quality', } satisfies GoogleVertexImageProviderOptions, }, // ...});The following provider options are available:
-
negativePrompt string A description of what to discourage in the generated images.
-
personGeneration
allow_adult|allow_all|dont_allowWhether to allow person generation. Defaults toallow_adult. -
safetySetting
block_low_and_above|block_medium_and_above|block_only_high|block_noneWhether to block unsafe content. Defaults toblock_medium_and_above. -
addWatermark boolean Whether to add an invisible watermark to the generated images. Defaults to
true. -
storageUri string Cloud Storage URI to store the generated images.
Imagen models do not support the size parameter. Use the aspectRatio
parameter instead.
Additional information about the images can be retrieved using Google Vertex meta data.
import { vertex } from '@ai-sdk/google-vertex';import { GoogleVertexImageProviderOptions } from '@ai-sdk/google-vertex';import { experimental_generateImage as generateImage } from 'ai';
const { image, providerMetadata } = await generateImage({ model: vertex.image('imagen-3.0-generate-002'), prompt: 'A futuristic cityscape at sunset', aspectRatio: '16:9',});
console.log( `Revised prompt: ${providerMetadata.vertex.images[0].revisedPrompt}`,);Model Capabilities
| Model | Aspect Ratios |
|---|---|
imagen-3.0-generate-001 | 1:1, 3:4, 4:3, 9:16, 16:9 |
imagen-3.0-generate-002 | 1:1, 3:4, 4:3, 9:16, 16:9 |
imagen-3.0-fast-generate-001 | 1:1, 3:4, 4:3, 9:16, 16:9 |
imagen-4.0-generate-preview-06-06 | 1:1, 3:4, 4:3, 9:16, 16:9 |
imagen-4.0-fast-generate-preview-06-06 | 1:1, 3:4, 4:3, 9:16, 16:9 |
imagen-4.0-ultra-generate-preview-06-06 | 1:1, 3:4, 4:3, 9:16, 16:9 |
Google Vertex Anthropic Provider Usage
The Google Vertex Anthropic provider for the AI SDK offers support for Anthropic's Claude models through the Google Vertex AI APIs. This section provides details on how to set up and use the Google Vertex Anthropic provider.
Provider Instance
You can import the default provider instance vertexAnthropic from @ai-sdk/google-vertex/anthropic:
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic';If you need a customized setup, you can import createVertexAnthropic from @ai-sdk/google-vertex/anthropic and create a provider instance with your settings:
import { createVertexAnthropic } from '@ai-sdk/google-vertex/anthropic';
const vertexAnthropic = createVertexAnthropic({ project: 'my-project', // optional location: 'us-central1', // optional});Node.js Runtime
For Node.js environments, the Google Vertex Anthropic provider supports all standard Google Cloud authentication options through the google-auth-library. You can customize the authentication options by passing them to the createVertexAnthropic function:
import { createVertexAnthropic } from '@ai-sdk/google-vertex/anthropic';
const vertexAnthropic = createVertexAnthropic({ googleAuthOptions: { credentials: { client_email: 'my-email', private_key: 'my-private-key', }, },});Optional Provider Settings
You can use the following optional settings to customize the Google Vertex Anthropic provider instance:
-
project string
The Google Cloud project ID that you want to use for the API calls. It uses the
GOOGLE_VERTEX_PROJECTenvironment variable by default. -
location string
The Google Cloud location that you want to use for the API calls, e.g.
us-central1. It uses theGOOGLE_VERTEX_LOCATIONenvironment variable by default. -
googleAuthOptions object
Optional. The Authentication options used by the Google Auth Library. See also the GoogleAuthOptions interface.
-
authClient object An
AuthClientto use. -
keyFilename string Path to a .json, .pem, or .p12 key file.
-
keyFile string Path to a .json, .pem, or .p12 key file.
-
credentials object Object containing client_email and private_key properties, or the external account client options.
-
clientOptions object Options object passed to the constructor of the client.
-
scopes string | string[] Required scopes for the desired API request.
-
projectId string Your project ID.
-
universeDomain string The default service domain for a given Cloud universe.
-
-
headers Resolvable<Record<string, string | undefined>>
Headers to include in the requests. Can be provided in multiple formats:
- A record of header key-value pairs:
Record<string, string | undefined> - A function that returns headers:
() => Record<string, string | undefined> - An async function that returns headers:
async () => Record<string, string | undefined> - A promise that resolves to headers:
Promise<Record<string, string | undefined>>
- A record of header key-value pairs:
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. Defaults to the global
fetchfunction. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.
Edge Runtime
Edge runtimes (like Vercel Edge Functions and Cloudflare Workers) are lightweight JavaScript environments that run closer to users at the network edge. They only provide a subset of the standard Node.js APIs. For example, direct file system access is not available, and many Node.js-specific libraries (including the standard Google Auth library) are not compatible.
The Edge runtime version of the Google Vertex Anthropic provider supports Google's Application Default Credentials through environment variables. The values can be obtained from a json credentials file from the Google Cloud Console.
For Edge runtimes, you can import the provider instance from @ai-sdk/google-vertex/anthropic/edge:
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic/edge';To customize the setup, use createVertexAnthropic from the same module:
import { createVertexAnthropic } from '@ai-sdk/google-vertex/anthropic/edge';
const vertexAnthropic = createVertexAnthropic({ project: 'my-project', // optional location: 'us-central1', // optional});For Edge runtime authentication, set these environment variables from your Google Default Application Credentials JSON file:
GOOGLE_CLIENT_EMAILGOOGLE_PRIVATE_KEYGOOGLE_PRIVATE_KEY_ID(optional)
Optional Provider Settings
You can use the following optional settings to customize the provider instance:
-
project string
The Google Cloud project ID that you want to use for the API calls. It uses the
GOOGLE_VERTEX_PROJECTenvironment variable by default. -
location string
The Google Cloud location that you want to use for the API calls, e.g.
us-central1. It uses theGOOGLE_VERTEX_LOCATIONenvironment variable by default. -
googleCredentials object
Optional. The credentials used by the Edge provider for authentication. These credentials are typically set through environment variables and are derived from a service account JSON file.
-
clientEmail string The client email from the service account JSON file. Defaults to the contents of the
GOOGLE_CLIENT_EMAILenvironment variable. -
privateKey string The private key from the service account JSON file. Defaults to the contents of the
GOOGLE_PRIVATE_KEYenvironment variable. -
privateKeyId string The private key ID from the service account JSON file (optional). Defaults to the contents of the
GOOGLE_PRIVATE_KEY_IDenvironment variable.
-
-
headers Resolvable<Record<string, string | undefined>>
Headers to include in the requests. Can be provided in multiple formats:
- A record of header key-value pairs:
Record<string, string | undefined> - A function that returns headers:
() => Record<string, string | undefined> - An async function that returns headers:
async () => Record<string, string | undefined> - A promise that resolves to headers:
Promise<Record<string, string | undefined>>
- A record of header key-value pairs:
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. Defaults to the global
fetchfunction. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.
Language Models
You can create models that call the Anthropic Messages API using the provider instance.
The first argument is the model id, e.g. claude-3-haiku-20240307.
Some models have multi-modal capabilities.
const model = anthropic('claude-3-haiku-20240307');You can use Anthropic language models to generate text with the generateText function:
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic';import { generateText } from 'ai';
const { text } = await generateText({ model: vertexAnthropic('claude-3-haiku-20240307'), prompt: 'Write a vegetarian lasagna recipe for 4 people.',});Anthropic language models can also be used in the streamText, generateObject, and streamObject functions
(see AI SDK Core).
The Anthropic API returns streaming tool calls all at once after a delay. This
causes the streamObject function to generate the object fully after a delay
instead of streaming it incrementally.
The following optional provider options are available for Anthropic models:
-
sendReasoningbooleanOptional. Include reasoning content in requests sent to the model. Defaults to
true.If you are experiencing issues with the model handling requests involving reasoning content, you can set this to
falseto omit them from the request. -
thinkingobjectOptional. See Reasoning section for more details.
Reasoning
Anthropic has reasoning support for the claude-3-7-sonnet@20250219 model.
You can enable it using the thinking provider option
and specifying a thinking budget in tokens.
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic';import { generateText } from 'ai';
const { text, reasoning, reasoningDetails } = await generateText({ model: vertexAnthropic('claude-3-7-sonnet@20250219'), prompt: 'How many people will live in the world in 2040?', providerOptions: { anthropic: { thinking: { type: 'enabled', budgetTokens: 12000 }, }, },});
console.log(reasoning); // reasoning textconsole.log(reasoningDetails); // reasoning details including redacted reasoningconsole.log(text); // text responseSee AI SDK UI: Chatbot for more details on how to integrate reasoning into your chatbot.
Cache Control
Anthropic cache control is in a Pre-Generally Available (GA) state on Google Vertex. For more see Google Vertex Anthropic cache control documentation.
In the messages and message parts, you can use the providerOptions property to set cache control breakpoints.
You need to set the anthropic property in the providerOptions object to { cacheControl: { type: 'ephemeral' } } to set a cache control breakpoint.
The cache creation input tokens are then returned in the providerMetadata object
for generateText and generateObject, again under the anthropic property.
When you use streamText or streamObject, the response contains a promise
that resolves to the metadata. Alternatively you can receive it in the
onFinish callback.
import { vertexAnthropic } from '@ai-sdk/google-vertex/anthropic';import { generateText } from 'ai';
const errorMessage = '... long error message ...';
const result = await generateText({ model: vertexAnthropic('claude-3-5-sonnet-20240620'), messages: [ { role: 'user', content: [ { type: 'text', text: 'You are a JavaScript expert.' }, { type: 'text', text: `Error message: ${errorMessage}`, providerOptions: { anthropic: { cacheControl: { type: 'ephemeral' } }, }, }, { type: 'text', text: 'Explain the error message.' }, ], }, ],});
console.log(result.text);console.log(result.providerMetadata?.anthropic);// e.g. { cacheCreationInputTokens: 2118, cacheReadInputTokens: 0 }You can also use cache control on system messages by providing multiple system messages at the head of your messages array:
const result = await generateText({ model: vertexAnthropic('claude-3-5-sonnet-20240620'), messages: [ { role: 'system', content: 'Cached system message part', providerOptions: { anthropic: { cacheControl: { type: 'ephemeral' } }, }, }, { role: 'system', content: 'Uncached system message part', }, { role: 'user', content: 'User prompt', }, ],});For more on prompt caching with Anthropic, see Google Vertex AI's Claude prompt caching documentation and Anthropic's Cache Control documentation.
Computer Use
Anthropic provides three built-in tools that can be used to interact with external systems:
- Bash Tool: Allows running bash commands.
- Text Editor Tool: Provides functionality for viewing and editing text files.
- Computer Tool: Enables control of keyboard and mouse actions on a computer.
They are available via the tools property of the provider instance.
For more background see Anthropic's Computer Use documentation.
Bash Tool
The Bash Tool allows running bash commands. Here's how to create and use it:
const bashTool = vertexAnthropic.tools.bash_20241022({ execute: async ({ command, restart }) => { // Implement your bash command execution logic here // Return the result of the command execution },});Parameters:
command(string): The bash command to run. Required unless the tool is being restarted.restart(boolean, optional): Specifying true will restart this tool.
Text Editor Tool
The Text Editor Tool provides functionality for viewing and editing text files:
const textEditorTool = vertexAnthropic.tools.textEditor_20241022({ execute: async ({ command, path, file_text, insert_line, new_str, old_str, view_range, }) => { // Implement your text editing logic here // Return the result of the text editing operation },});Parameters:
command('view' | 'create' | 'str_replace' | 'insert' | 'undo_edit'): The command to run.path(string): Absolute path to file or directory, e.g./repo/file.pyor/repo.file_text(string, optional): Required forcreatecommand, with the content of the file to be created.insert_line(number, optional): Required forinsertcommand. The line number after which to insert the new string.new_str(string, optional): New string forstr_replaceorinsertcommands.old_str(string, optional): Required forstr_replacecommand, containing the string to replace.view_range(number[], optional): Optional forviewcommand to specify line range to show.
Computer Tool
The Computer Tool enables control of keyboard and mouse actions on a computer:
const computerTool = vertexAnthropic.tools.computer_20241022({ displayWidthPx: 1920, displayHeightPx: 1080, displayNumber: 0, // Optional, for X11 environments
execute: async ({ action, coordinate, text }) => { // Implement your computer control logic here // Return the result of the action
// Example code: switch (action) { case 'screenshot': { // multipart result: return { type: 'image', data: fs .readFileSync('./data/screenshot-editor.png') .toString('base64'), }; } default: { console.log('Action:', action); console.log('Coordinate:', coordinate); console.log('Text:', text); return `executed ${action}`; } } },
// map to tool result content for LLM consumption: toModelOutput(result) { return typeof result === 'string' ? [{ type: 'text', text: result }] : [{ type: 'image', data: result.data, mediaType: 'image/png' }]; },});Parameters:
action('key' | 'type' | 'mouse_move' | 'left_click' | 'left_click_drag' | 'right_click' | 'middle_click' | 'double_click' | 'screenshot' | 'cursor_position'): The action to perform.coordinate(number[], optional): Required formouse_moveandleft_click_dragactions. Specifies the (x, y) coordinates.text(string, optional): Required fortypeandkeyactions.
These tools can be used in conjunction with the claude-3-5-sonnet-v2@20241022 model to enable more complex interactions and tasks.
Model Capabilities
The latest Anthropic model list on Vertex AI is available here. See also Anthropic Model Comparison.
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming | Computer Use |
|---|---|---|---|---|---|
claude-3-7-sonnet@20250219 | |||||
claude-3-5-sonnet-v2@20241022 | |||||
claude-3-5-sonnet@20240620 | |||||
claude-3-5-haiku@20241022 | |||||
claude-3-sonnet@20240229 | |||||
claude-3-haiku@20240307 | |||||
claude-3-opus@20240229 |
The table above lists popular models. You can also pass any available provider model ID as a string if needed.