@ai-sdk/google-vertex 4.0.116 → 4.0.117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ // src/xai/google-vertex-xai-provider-node.ts
2
+ import { resolve } from "@ai-sdk/provider-utils";
3
+
4
+ // src/google-vertex-auth-google-auth-library.ts
5
+ import { GoogleAuth } from "google-auth-library";
6
+ var authInstance = null;
7
+ var authOptions = null;
8
+ function getAuth(options) {
9
+ if (!authInstance || options !== authOptions) {
10
+ authInstance = new GoogleAuth({
11
+ scopes: ["https://www.googleapis.com/auth/cloud-platform"],
12
+ ...options
13
+ });
14
+ authOptions = options;
15
+ }
16
+ return authInstance;
17
+ }
18
+ async function generateAuthToken(options) {
19
+ const auth = getAuth(options || {});
20
+ const client = await auth.getClient();
21
+ const token = await client.getAccessToken();
22
+ return (token == null ? void 0 : token.token) || null;
23
+ }
24
+
25
+ // src/xai/google-vertex-xai-provider.ts
26
+ import {
27
+ NoSuchModelError
28
+ } from "@ai-sdk/provider";
29
+ import {
30
+ createOpenAICompatible
31
+ } from "@ai-sdk/openai-compatible";
32
+ import {
33
+ loadOptionalSetting,
34
+ loadSetting,
35
+ withoutTrailingSlash
36
+ } from "@ai-sdk/provider-utils";
37
+ function convertGoogleVertexXaiUsage(usage) {
38
+ var _a, _b, _c, _d, _e, _f;
39
+ if (usage == null) {
40
+ return {
41
+ inputTokens: {
42
+ total: void 0,
43
+ noCache: void 0,
44
+ cacheRead: void 0,
45
+ cacheWrite: void 0
46
+ },
47
+ outputTokens: {
48
+ total: void 0,
49
+ text: void 0,
50
+ reasoning: void 0
51
+ },
52
+ raw: void 0
53
+ };
54
+ }
55
+ const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
56
+ const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
57
+ const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
58
+ const reasoningTokens = (_f = (_e = usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0;
59
+ return {
60
+ inputTokens: {
61
+ total: promptTokens,
62
+ noCache: promptTokens - cacheReadTokens,
63
+ cacheRead: cacheReadTokens,
64
+ cacheWrite: void 0
65
+ },
66
+ outputTokens: {
67
+ total: completionTokens + reasoningTokens,
68
+ text: completionTokens,
69
+ reasoning: reasoningTokens
70
+ },
71
+ raw: usage
72
+ };
73
+ }
74
+ function transformGoogleVertexXaiRequestBody(args) {
75
+ const { reasoning_effort: _reasoningEffort, ...rest } = args;
76
+ return rest;
77
+ }
78
+ function createGoogleVertexXai(options = {}) {
79
+ const loadLocation = () => loadOptionalSetting({
80
+ settingValue: options.location,
81
+ environmentVariableName: "GOOGLE_VERTEX_LOCATION"
82
+ });
83
+ const loadProject = () => loadSetting({
84
+ settingValue: options.project,
85
+ settingName: "project",
86
+ environmentVariableName: "GOOGLE_VERTEX_PROJECT",
87
+ description: "Google Vertex project"
88
+ });
89
+ const constructBaseURL = () => {
90
+ var _a;
91
+ const projectId = loadProject();
92
+ const location = (_a = loadLocation()) != null ? _a : "global";
93
+ return `https://aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/endpoints/openapi`;
94
+ };
95
+ const loadBaseURL = () => {
96
+ var _a;
97
+ return withoutTrailingSlash((_a = options.baseURL) != null ? _a : "") || constructBaseURL();
98
+ };
99
+ let cachedProvider;
100
+ const getProvider = () => cachedProvider != null ? cachedProvider : cachedProvider = createOpenAICompatible({
101
+ name: "googleVertex.xai",
102
+ baseURL: loadBaseURL(),
103
+ fetch: options.fetch,
104
+ includeUsage: true,
105
+ supportsStructuredOutputs: true,
106
+ supportedUrls: () => ({
107
+ "image/*": [/^https?:\/\/.*$/]
108
+ }),
109
+ transformRequestBody: transformGoogleVertexXaiRequestBody,
110
+ convertUsage: convertGoogleVertexXaiUsage
111
+ });
112
+ const createChatModel = (modelId) => getProvider().languageModel(modelId);
113
+ const provider = function(modelId) {
114
+ if (new.target) {
115
+ throw new Error(
116
+ "The Google Vertex xAI model function cannot be called with the new keyword."
117
+ );
118
+ }
119
+ return createChatModel(modelId);
120
+ };
121
+ provider.specificationVersion = "v3";
122
+ provider.languageModel = createChatModel;
123
+ provider.chatModel = (modelId) => getProvider().chatModel(modelId);
124
+ provider.embeddingModel = (modelId) => {
125
+ throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
126
+ };
127
+ provider.textEmbeddingModel = provider.embeddingModel;
128
+ provider.imageModel = (modelId) => {
129
+ throw new NoSuchModelError({ modelId, modelType: "imageModel" });
130
+ };
131
+ return provider;
132
+ }
133
+
134
+ // src/xai/google-vertex-xai-provider-node.ts
135
+ function createGoogleVertexXai2(options = {}) {
136
+ const customFetch = async (url, init) => {
137
+ var _a;
138
+ const token = await generateAuthToken(options.googleAuthOptions);
139
+ const resolvedHeaders = await resolve(options.headers);
140
+ const authHeaders = {
141
+ ...resolvedHeaders,
142
+ Authorization: `Bearer ${token}`
143
+ };
144
+ const fetchInit = {
145
+ ...init,
146
+ headers: {
147
+ ...init == null ? void 0 : init.headers,
148
+ ...authHeaders
149
+ }
150
+ };
151
+ return ((_a = options.fetch) != null ? _a : fetch)(url, fetchInit);
152
+ };
153
+ return createGoogleVertexXai({
154
+ ...options,
155
+ fetch: customFetch,
156
+ headers: void 0
157
+ });
158
+ }
159
+ var googleVertexXai = createGoogleVertexXai2();
160
+ export {
161
+ createGoogleVertexXai2 as createGoogleVertexXai,
162
+ googleVertexXai
163
+ };
164
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/xai/google-vertex-xai-provider-node.ts","../../src/google-vertex-auth-google-auth-library.ts","../../src/xai/google-vertex-xai-provider.ts"],"sourcesContent":["import { resolve, type FetchFunction } from '@ai-sdk/provider-utils';\nimport type { GoogleAuthOptions } from 'google-auth-library';\nimport { generateAuthToken } from '../google-vertex-auth-google-auth-library';\nimport {\n createGoogleVertexXai as createGoogleVertexXaiOriginal,\n type GoogleVertexXaiProvider,\n type GoogleVertexXaiProviderSettings as GoogleVertexXaiProviderSettingsOriginal,\n} from './google-vertex-xai-provider';\nexport type { GoogleVertexXaiProvider };\n\nexport interface GoogleVertexXaiProviderSettings extends GoogleVertexXaiProviderSettingsOriginal {\n /**\n * Optional. The Authentication options provided by google-auth-library.\n * Complete list of authentication options is documented in the\n * GoogleAuthOptions interface:\n * https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.\n */\n googleAuthOptions?: GoogleAuthOptions;\n}\n\n/**\n * Create a Google Vertex AI xAI provider instance for Node.js.\n * Uses the OpenAI-compatible Chat Completions API for Grok partner models.\n * Automatically handles Google Cloud authentication.\n *\n * @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok\n */\nexport function createGoogleVertexXai(\n options: GoogleVertexXaiProviderSettings = {},\n): GoogleVertexXaiProvider {\n const customFetch: FetchFunction = async (url, init) => {\n const token = await generateAuthToken(options.googleAuthOptions);\n const resolvedHeaders = await resolve(options.headers);\n const authHeaders = {\n ...resolvedHeaders,\n Authorization: `Bearer ${token}`,\n };\n\n const fetchInit = {\n ...init,\n headers: {\n ...init?.headers,\n ...authHeaders,\n },\n };\n\n return (options.fetch ?? fetch)(url, fetchInit);\n };\n\n return createGoogleVertexXaiOriginal({\n ...options,\n fetch: customFetch,\n headers: undefined,\n });\n}\n\n/**\n * Default Google Vertex AI xAI provider instance for Node.js.\n */\nexport const googleVertexXai = createGoogleVertexXai();\n","import { GoogleAuth, type GoogleAuthOptions } from 'google-auth-library';\n\nlet authInstance: GoogleAuth | null = null;\nlet authOptions: GoogleAuthOptions | null = null;\n\nfunction getAuth(options: GoogleAuthOptions) {\n if (!authInstance || options !== authOptions) {\n authInstance = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n ...options,\n });\n authOptions = options;\n }\n return authInstance;\n}\n\nexport async function generateAuthToken(options?: GoogleAuthOptions) {\n const auth = getAuth(options || {});\n const client = await auth.getClient();\n const token = await client.getAccessToken();\n return token?.token || null;\n}\n\n// For testing purposes only\nexport function _resetAuthInstance() {\n authInstance = null;\n}\n","import {\n NoSuchModelError,\n type LanguageModelV3,\n type LanguageModelV3Usage,\n type ProviderV3,\n} from '@ai-sdk/provider';\nimport {\n createOpenAICompatible,\n type OpenAICompatibleProvider,\n} from '@ai-sdk/openai-compatible';\nimport {\n loadOptionalSetting,\n loadSetting,\n withoutTrailingSlash,\n type FetchFunction,\n type Resolvable,\n} from '@ai-sdk/provider-utils';\nimport type { GoogleVertexXaiModelId } from './google-vertex-xai-options';\n\nexport interface GoogleVertexXaiProvider extends ProviderV3 {\n /**\n * Creates a model for text generation.\n */\n (modelId: GoogleVertexXaiModelId): LanguageModelV3;\n\n /**\n * Creates a model for text generation.\n */\n languageModel(modelId: GoogleVertexXaiModelId): LanguageModelV3;\n\n /**\n * Creates a chat model for text generation.\n */\n chatModel(modelId: GoogleVertexXaiModelId): LanguageModelV3;\n\n /**\n * @deprecated Use `embeddingModel` instead.\n */\n textEmbeddingModel(modelId: string): never;\n}\n\nexport interface GoogleVertexXaiProviderSettings {\n /**\n * Google Cloud project ID. Defaults to the value of the `GOOGLE_VERTEX_PROJECT` environment variable.\n */\n project?: string;\n\n /**\n * Google Cloud location/region. Defaults to the value of the `GOOGLE_VERTEX_LOCATION` environment variable.\n * Use 'global' for the global endpoint.\n */\n location?: string;\n\n /**\n * Base URL for the API calls. If not provided, will be constructed from project and location.\n */\n baseURL?: string;\n\n /**\n * Headers to use for requests. Can be:\n * - A headers object\n * - A Promise that resolves to a headers object\n * - A function that returns a headers object\n * - A function that returns a Promise of a headers object\n */\n headers?: Resolvable<Record<string, string | undefined>>;\n\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\ntype GoogleVertexXaiUsage =\n | {\n prompt_tokens?: number | null;\n completion_tokens?: number | null;\n prompt_tokens_details?: {\n cached_tokens?: number | null;\n } | null;\n completion_tokens_details?: {\n reasoning_tokens?: number | null;\n } | null;\n }\n | undefined\n | null;\n\nfunction convertGoogleVertexXaiUsage(\n usage: GoogleVertexXaiUsage,\n): LanguageModelV3Usage {\n if (usage == null) {\n return {\n inputTokens: {\n total: undefined,\n noCache: undefined,\n cacheRead: undefined,\n cacheWrite: undefined,\n },\n outputTokens: {\n total: undefined,\n text: undefined,\n reasoning: undefined,\n },\n raw: undefined,\n };\n }\n\n const promptTokens = usage.prompt_tokens ?? 0;\n const completionTokens = usage.completion_tokens ?? 0;\n const cacheReadTokens = usage.prompt_tokens_details?.cached_tokens ?? 0;\n const reasoningTokens =\n usage.completion_tokens_details?.reasoning_tokens ?? 0;\n\n return {\n inputTokens: {\n total: promptTokens,\n noCache: promptTokens - cacheReadTokens,\n cacheRead: cacheReadTokens,\n cacheWrite: undefined,\n },\n outputTokens: {\n total: completionTokens + reasoningTokens,\n text: completionTokens,\n reasoning: reasoningTokens,\n },\n raw: usage,\n };\n}\n\nfunction transformGoogleVertexXaiRequestBody(args: Record<string, any>) {\n const { reasoning_effort: _reasoningEffort, ...rest } = args;\n return rest;\n}\n\n/**\n * Create a Google Vertex AI xAI provider instance.\n * Uses the OpenAI-compatible Chat Completions API for Grok partner models.\n *\n * @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok\n */\nexport function createGoogleVertexXai(\n options: GoogleVertexXaiProviderSettings = {},\n): GoogleVertexXaiProvider {\n const loadLocation = () =>\n loadOptionalSetting({\n settingValue: options.location,\n environmentVariableName: 'GOOGLE_VERTEX_LOCATION',\n });\n\n const loadProject = () =>\n loadSetting({\n settingValue: options.project,\n settingName: 'project',\n environmentVariableName: 'GOOGLE_VERTEX_PROJECT',\n description: 'Google Vertex project',\n });\n\n const constructBaseURL = () => {\n const projectId = loadProject();\n const location = loadLocation() ?? 'global';\n\n return `https://aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/endpoints/openapi`;\n };\n\n const loadBaseURL = () =>\n withoutTrailingSlash(options.baseURL ?? '') || constructBaseURL();\n\n let cachedProvider:\n | OpenAICompatibleProvider<GoogleVertexXaiModelId, string, string, string>\n | undefined;\n const getProvider = () =>\n (cachedProvider ??= createOpenAICompatible({\n name: 'googleVertex.xai',\n baseURL: loadBaseURL(),\n fetch: options.fetch,\n includeUsage: true,\n supportsStructuredOutputs: true,\n supportedUrls: () => ({\n 'image/*': [/^https?:\\/\\/.*$/],\n }),\n transformRequestBody: transformGoogleVertexXaiRequestBody,\n convertUsage: convertGoogleVertexXaiUsage,\n }));\n\n const createChatModel = (modelId: GoogleVertexXaiModelId) =>\n getProvider().languageModel(modelId);\n\n const provider = function (modelId: GoogleVertexXaiModelId) {\n if (new.target) {\n throw new Error(\n 'The Google Vertex xAI model function cannot be called with the new keyword.',\n );\n }\n\n return createChatModel(modelId);\n };\n\n provider.specificationVersion = 'v3' as const;\n provider.languageModel = createChatModel;\n provider.chatModel = (modelId: GoogleVertexXaiModelId) =>\n getProvider().chatModel(modelId);\n provider.embeddingModel = (modelId: string): never => {\n throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n };\n provider.textEmbeddingModel = provider.embeddingModel;\n provider.imageModel = (modelId: string): never => {\n throw new NoSuchModelError({ modelId, modelType: 'imageModel' });\n };\n\n return provider;\n}\n"],"mappings":";AAAA,SAAS,eAAmC;;;ACA5C,SAAS,kBAA0C;AAEnD,IAAI,eAAkC;AACtC,IAAI,cAAwC;AAE5C,SAAS,QAAQ,SAA4B;AAC3C,MAAI,CAAC,gBAAgB,YAAY,aAAa;AAC5C,mBAAe,IAAI,WAAW;AAAA,MAC5B,QAAQ,CAAC,gDAAgD;AAAA,MACzD,GAAG;AAAA,IACL,CAAC;AACD,kBAAc;AAAA,EAChB;AACA,SAAO;AACT;AAEA,eAAsB,kBAAkB,SAA6B;AACnE,QAAM,OAAO,QAAQ,WAAW,CAAC,CAAC;AAClC,QAAM,SAAS,MAAM,KAAK,UAAU;A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/D,UAAM,kBAAkB,MAAM,QAAQ,QAAQ,OAAO;AACrD,UAAM,cAAc;AAAA,MAClB,GAAG;AAAA,MACH,eAAe,UAAU,KAAK;AAAA,IAChC;AAEA,UAAM,YAAY;AAAA,MAChB,GAAG;AAAA,MACH,SAAS;AAAA,QACP,GAAG,6BAAM;AAAA,QACT,GAAG;AAAA,MACL;AAAA,IACF;AAEA,aAAQ,aAAQ,UAAR,YAAiB,OAAO,KAAK,SAAS;AAAA,EAChD;AAEA,SAAO,sBAA8B;AAAA,IACnC,GAAG;AAAA,IACH,OAAO;AAAA,IACP,SAAS;AAAA,EACX,CAAC;AACH;AAKO,IAAM,kBAAkBA,uBAAsB;","names":["createGoogleVertexXai"]}
@@ -5,7 +5,7 @@ description: Learn how to use the Google Vertex AI provider.
5
5
 
6
6
  # Google Vertex Provider
7
7
 
8
- The Google Vertex provider for the [AI SDK](/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs. This includes support for [Google's Gemini models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models), [Anthropic's Claude partner models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude), and [MaaS (Model as a Service) open models](https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models).
8
+ The Google Vertex provider for the [AI SDK](/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs. This includes support for [Google's Gemini models](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models), [Anthropic's Claude partner models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude), [xAI's Grok partner models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok), and [MaaS (Model as a Service) open models](https://cloud.google.com/vertex-ai/generative-ai/docs/maas/use-open-models).
9
9
 
10
10
  <Note>
11
11
  The Google Vertex provider is compatible with both Node.js and Edge runtimes.
@@ -18,7 +18,7 @@ The Google Vertex provider for the [AI SDK](/docs) contains language model suppo
18
18
 
19
19
  ## Setup
20
20
 
21
- The Google Vertex and Google Vertex Anthropic providers are both available in the `@ai-sdk/google-vertex` module. You can install it with
21
+ The Google Vertex, Google Vertex Anthropic, Google Vertex xAI, and Google Vertex MaaS providers are available in the `@ai-sdk/google-vertex` module. You can install it with
22
22
 
23
23
  <Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
24
24
  <Tab>
@@ -1780,6 +1780,185 @@ See also [Anthropic Model Comparison](https://docs.anthropic.com/en/docs/about-c
1780
1780
  model ID as a string if needed.
1781
1781
  </Note>
1782
1782
 
1783
+ ## Google Vertex xAI Provider Usage
1784
+
1785
+ The Google Vertex xAI provider offers support for xAI's Grok partner models through the Google Vertex AI OpenAI-compatible Chat Completions API.
1786
+
1787
+ For more information, see the [Vertex AI Grok documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok).
1788
+
1789
+ ### Provider Instance
1790
+
1791
+ You can import the default provider instance `googleVertexXai` from `@ai-sdk/google-vertex/xai`:
1792
+
1793
+ ```typescript
1794
+ import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
1795
+ ```
1796
+
1797
+ If you need a customized setup, you can import `createGoogleVertexXai` from `@ai-sdk/google-vertex/xai` and create a provider instance with your settings:
1798
+
1799
+ ```typescript
1800
+ import { createGoogleVertexXai } from '@ai-sdk/google-vertex/xai';
1801
+
1802
+ const googleVertexXai = createGoogleVertexXai({
1803
+ project: 'my-project', // optional
1804
+ location: 'global', // optional, defaults to 'global'
1805
+ });
1806
+ ```
1807
+
1808
+ #### Node.js Runtime
1809
+
1810
+ For Node.js environments, the Google Vertex xAI provider supports all standard Google Cloud authentication options through the `google-auth-library`:
1811
+
1812
+ ```typescript
1813
+ import { createGoogleVertexXai } from '@ai-sdk/google-vertex/xai';
1814
+
1815
+ const googleVertexXai = createGoogleVertexXai({
1816
+ googleAuthOptions: {
1817
+ credentials: {
1818
+ client_email: 'my-email',
1819
+ private_key: 'my-private-key',
1820
+ },
1821
+ },
1822
+ });
1823
+ ```
1824
+
1825
+ ##### Optional Provider Settings
1826
+
1827
+ - **project** _string_
1828
+
1829
+ The Google Cloud project ID. Defaults to the `GOOGLE_VERTEX_PROJECT` environment variable.
1830
+
1831
+ - **location** _string_
1832
+
1833
+ The Google Cloud location. Grok models are available on the global endpoint. Defaults to the `GOOGLE_VERTEX_LOCATION` environment variable. If not set, defaults to `global`.
1834
+
1835
+ - **googleAuthOptions** _object_
1836
+
1837
+ Optional. The Authentication options used by the [Google Auth Library](https://github.com/googleapis/google-auth-library-nodejs/).
1838
+
1839
+ - **headers** _Resolvable&lt;Record&lt;string, string | undefined&gt;&gt;_
1840
+
1841
+ Headers to include in requests.
1842
+
1843
+ - **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_
1844
+
1845
+ Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
1846
+
1847
+ <a id="google-vertex-xai-edge-runtime"></a>
1848
+
1849
+ #### Edge Runtime
1850
+
1851
+ For Edge runtimes, import from `@ai-sdk/google-vertex/xai/edge`:
1852
+
1853
+ ```typescript
1854
+ import { googleVertexXai } from '@ai-sdk/google-vertex/xai/edge';
1855
+ ```
1856
+
1857
+ ```typescript
1858
+ import { createGoogleVertexXai } from '@ai-sdk/google-vertex/xai/edge';
1859
+
1860
+ const googleVertexXai = createGoogleVertexXai({
1861
+ project: 'my-project',
1862
+ location: 'global',
1863
+ });
1864
+ ```
1865
+
1866
+ For Edge runtime authentication, set these environment variables:
1867
+
1868
+ - `GOOGLE_CLIENT_EMAIL`
1869
+ - `GOOGLE_PRIVATE_KEY`
1870
+ - `GOOGLE_PRIVATE_KEY_ID` (optional)
1871
+
1872
+ ### Language Models
1873
+
1874
+ You can create models using the provider instance. The first argument is the model ID:
1875
+
1876
+ ```ts
1877
+ import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
1878
+ import { generateText } from 'ai';
1879
+
1880
+ const { text } = await generateText({
1881
+ model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
1882
+ prompt: 'Invent a new holiday and describe its traditions.',
1883
+ });
1884
+ ```
1885
+
1886
+ Streaming is also supported:
1887
+
1888
+ ```ts
1889
+ import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
1890
+ import { streamText } from 'ai';
1891
+
1892
+ const result = streamText({
1893
+ model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
1894
+ prompt: 'Invent a new holiday and describe its traditions.',
1895
+ });
1896
+
1897
+ for await (const textPart of result.textStream) {
1898
+ process.stdout.write(textPart);
1899
+ }
1900
+ ```
1901
+
1902
+ ### Function Calling
1903
+
1904
+ Grok models on Vertex support OpenAI-compatible function calling. You can use AI SDK tools as usual:
1905
+
1906
+ ```ts
1907
+ import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
1908
+ import { generateText, tool } from 'ai';
1909
+ import { z } from 'zod';
1910
+
1911
+ const result = await generateText({
1912
+ model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
1913
+ tools: {
1914
+ weather: tool({
1915
+ description: 'Get the weather in a city',
1916
+ inputSchema: z.object({ city: z.string() }),
1917
+ execute: async ({ city }) => `The weather in ${city} is sunny.`,
1918
+ }),
1919
+ },
1920
+ prompt: 'What is the weather in San Francisco?',
1921
+ });
1922
+ ```
1923
+
1924
+ ### Structured Outputs
1925
+
1926
+ Grok models on Vertex support JSON mode and schema-backed structured outputs:
1927
+
1928
+ ```ts
1929
+ import { googleVertexXai } from '@ai-sdk/google-vertex/xai';
1930
+ import { generateText, Output } from 'ai';
1931
+ import { z } from 'zod';
1932
+
1933
+ const result = await generateText({
1934
+ model: googleVertexXai('xai/grok-4.1-fast-reasoning'),
1935
+ output: Output.object({
1936
+ schema: z.object({
1937
+ name: z.string(),
1938
+ date: z.string(),
1939
+ participants: z.array(z.string()),
1940
+ }),
1941
+ }),
1942
+ prompt: 'Alice and Bob are going to a science fair on Friday.',
1943
+ });
1944
+ ```
1945
+
1946
+ ### Available Models
1947
+
1948
+ The following models are available through the Google Vertex xAI provider. You can also pass any valid model ID as a string.
1949
+
1950
+ | Model ID | Reasoning |
1951
+ | ----------------------------------- | --------- |
1952
+ | `xai/grok-4.20-reasoning` | Yes |
1953
+ | `xai/grok-4.20-non-reasoning` | No |
1954
+ | `xai/grok-4.1-fast-reasoning` | Yes |
1955
+ | `xai/grok-4.1-fast-non-reasoning` | No |
1956
+
1957
+ <Note>
1958
+ Grok reasoning models on Vertex report reasoning token counts in usage
1959
+ metadata. They do not support the `reasoning_effort` request parameter.
1960
+ </Note>
1961
+
1783
1962
  ## Google Vertex MaaS Provider Usage
1784
1963
 
1785
1964
  The Google Vertex MaaS (Model as a Service) provider offers access to partner and open models hosted on Vertex AI through an OpenAI-compatible Chat Completions API. This includes models from DeepSeek, Qwen, Meta, MiniMax, Moonshot, and OpenAI.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/google-vertex",
3
- "version": "4.0.116",
3
+ "version": "4.0.117",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -20,7 +20,9 @@
20
20
  "anthropic/edge.d.ts",
21
21
  "anthropic/index.d.ts",
22
22
  "maas/edge.d.ts",
23
- "maas/index.d.ts"
23
+ "maas/index.d.ts",
24
+ "xai/edge.d.ts",
25
+ "xai/index.d.ts"
24
26
  ],
25
27
  "directories": {
26
28
  "doc": "./docs"
@@ -54,16 +56,26 @@
54
56
  },
55
57
  "./maas/edge": {
56
58
  "types": "./dist/maas/edge/index.d.ts",
57
- "import": "./dist/maas/edge/index.mjs",
58
- "require": "./dist/maas/edge/index.js"
59
+ "import": "./dist/maas/edge/index.js",
60
+ "default": "./dist/maas/edge/index.js"
61
+ },
62
+ "./xai": {
63
+ "types": "./dist/xai/index.d.ts",
64
+ "import": "./dist/xai/index.js",
65
+ "default": "./dist/xai/index.js"
66
+ },
67
+ "./xai/edge": {
68
+ "types": "./dist/xai/edge/index.d.ts",
69
+ "import": "./dist/xai/edge/index.js",
70
+ "default": "./dist/xai/edge/index.js"
59
71
  }
60
72
  },
61
73
  "dependencies": {
62
74
  "google-auth-library": "^10.5.0",
63
75
  "@ai-sdk/anthropic": "3.0.74",
64
76
  "@ai-sdk/google": "3.0.67",
77
+ "@ai-sdk/openai-compatible": "2.0.45",
65
78
  "@ai-sdk/provider": "3.0.10",
66
- "@ai-sdk/openai-compatible": "2.0.44",
67
79
  "@ai-sdk/provider-utils": "4.0.26"
68
80
  },
69
81
  "devDependencies": {
@@ -71,8 +83,8 @@
71
83
  "tsup": "^8",
72
84
  "typescript": "5.8.3",
73
85
  "zod": "3.25.76",
74
- "@vercel/ai-tsconfig": "0.0.0",
75
- "@ai-sdk/test-server": "1.0.5"
86
+ "@ai-sdk/test-server": "1.0.5",
87
+ "@vercel/ai-tsconfig": "0.0.0"
76
88
  },
77
89
  "peerDependencies": {
78
90
  "zod": "^3.25.76 || ^4.1.8"
@@ -0,0 +1,61 @@
1
+ import { resolve, type FetchFunction } from '@ai-sdk/provider-utils';
2
+ import {
3
+ generateAuthToken,
4
+ type GoogleCredentials,
5
+ } from '../../edge/google-vertex-auth-edge';
6
+ import {
7
+ createGoogleVertexXai as createGoogleVertexXaiOriginal,
8
+ type GoogleVertexXaiProvider,
9
+ type GoogleVertexXaiProviderSettings as GoogleVertexXaiProviderSettingsOriginal,
10
+ } from '../google-vertex-xai-provider';
11
+ export type { GoogleVertexXaiProvider };
12
+
13
+ export interface GoogleVertexXaiProviderSettings extends GoogleVertexXaiProviderSettingsOriginal {
14
+ /**
15
+ * Optional. The Google credentials for the Google Cloud service account. If
16
+ * not provided, the Google Vertex provider will use environment variables to
17
+ * load the credentials.
18
+ */
19
+ googleCredentials?: GoogleCredentials;
20
+ }
21
+
22
+ /**
23
+ * Create a Google Vertex AI xAI provider instance for Edge runtimes.
24
+ * Uses the OpenAI-compatible Chat Completions API for Grok partner models.
25
+ * Automatically handles Google Cloud authentication.
26
+ *
27
+ * @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok
28
+ */
29
+ export function createGoogleVertexXai(
30
+ options: GoogleVertexXaiProviderSettings = {},
31
+ ): GoogleVertexXaiProvider {
32
+ const customFetch: FetchFunction = async (url, init) => {
33
+ const token = await generateAuthToken(options.googleCredentials);
34
+ const resolvedHeaders = await resolve(options.headers);
35
+ const authHeaders = {
36
+ ...resolvedHeaders,
37
+ Authorization: `Bearer ${token}`,
38
+ };
39
+
40
+ const fetchInit = {
41
+ ...init,
42
+ headers: {
43
+ ...init?.headers,
44
+ ...authHeaders,
45
+ },
46
+ };
47
+
48
+ return (options.fetch ?? fetch)(url, fetchInit);
49
+ };
50
+
51
+ return createGoogleVertexXaiOriginal({
52
+ ...options,
53
+ fetch: customFetch,
54
+ headers: undefined,
55
+ });
56
+ }
57
+
58
+ /**
59
+ * Default Google Vertex AI xAI provider instance for Edge runtimes.
60
+ */
61
+ export const googleVertexXai = createGoogleVertexXai();
@@ -0,0 +1,9 @@
1
+ export {
2
+ createGoogleVertexXai,
3
+ googleVertexXai,
4
+ } from './google-vertex-xai-provider-edge';
5
+ export type {
6
+ GoogleVertexXaiProvider,
7
+ GoogleVertexXaiProviderSettings,
8
+ } from './google-vertex-xai-provider-edge';
9
+ export type { GoogleVertexXaiModelId } from '../google-vertex-xai-options';
@@ -0,0 +1,7 @@
1
+ // https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok
2
+ export type GoogleVertexXaiModelId =
3
+ | 'xai/grok-4.20-reasoning'
4
+ | 'xai/grok-4.20-non-reasoning'
5
+ | 'xai/grok-4.1-fast-reasoning'
6
+ | 'xai/grok-4.1-fast-non-reasoning'
7
+ | (string & {});
@@ -0,0 +1,60 @@
1
+ import { resolve, type FetchFunction } from '@ai-sdk/provider-utils';
2
+ import type { GoogleAuthOptions } from 'google-auth-library';
3
+ import { generateAuthToken } from '../google-vertex-auth-google-auth-library';
4
+ import {
5
+ createGoogleVertexXai as createGoogleVertexXaiOriginal,
6
+ type GoogleVertexXaiProvider,
7
+ type GoogleVertexXaiProviderSettings as GoogleVertexXaiProviderSettingsOriginal,
8
+ } from './google-vertex-xai-provider';
9
+ export type { GoogleVertexXaiProvider };
10
+
11
+ export interface GoogleVertexXaiProviderSettings extends GoogleVertexXaiProviderSettingsOriginal {
12
+ /**
13
+ * Optional. The Authentication options provided by google-auth-library.
14
+ * Complete list of authentication options is documented in the
15
+ * GoogleAuthOptions interface:
16
+ * https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.
17
+ */
18
+ googleAuthOptions?: GoogleAuthOptions;
19
+ }
20
+
21
+ /**
22
+ * Create a Google Vertex AI xAI provider instance for Node.js.
23
+ * Uses the OpenAI-compatible Chat Completions API for Grok partner models.
24
+ * Automatically handles Google Cloud authentication.
25
+ *
26
+ * @see https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/grok
27
+ */
28
+ export function createGoogleVertexXai(
29
+ options: GoogleVertexXaiProviderSettings = {},
30
+ ): GoogleVertexXaiProvider {
31
+ const customFetch: FetchFunction = async (url, init) => {
32
+ const token = await generateAuthToken(options.googleAuthOptions);
33
+ const resolvedHeaders = await resolve(options.headers);
34
+ const authHeaders = {
35
+ ...resolvedHeaders,
36
+ Authorization: `Bearer ${token}`,
37
+ };
38
+
39
+ const fetchInit = {
40
+ ...init,
41
+ headers: {
42
+ ...init?.headers,
43
+ ...authHeaders,
44
+ },
45
+ };
46
+
47
+ return (options.fetch ?? fetch)(url, fetchInit);
48
+ };
49
+
50
+ return createGoogleVertexXaiOriginal({
51
+ ...options,
52
+ fetch: customFetch,
53
+ headers: undefined,
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Default Google Vertex AI xAI provider instance for Node.js.
59
+ */
60
+ export const googleVertexXai = createGoogleVertexXai();