@ai-sdk/xai 4.0.0-beta.4 → 4.0.0-beta.41
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.
- package/CHANGELOG.md +319 -8
- package/README.md +2 -0
- package/dist/index.d.ts +126 -50
- package/dist/index.js +1169 -749
- package/dist/index.js.map +1 -1
- package/docs/01-xai.mdx +173 -381
- package/package.json +8 -10
- package/src/convert-to-xai-chat-messages.ts +22 -6
- package/src/convert-xai-chat-usage.ts +2 -2
- package/src/files/xai-files-api.ts +16 -0
- package/src/files/xai-files-options.ts +15 -0
- package/src/files/xai-files.ts +93 -0
- package/src/index.ts +1 -0
- package/src/map-xai-finish-reason.ts +2 -2
- package/src/responses/convert-to-xai-responses-input.ts +63 -8
- package/src/responses/convert-xai-responses-usage.ts +2 -2
- package/src/responses/map-xai-responses-finish-reason.ts +3 -2
- package/src/responses/xai-responses-api.ts +31 -1
- package/src/responses/xai-responses-language-model.ts +134 -48
- package/src/responses/xai-responses-options.ts +6 -0
- package/src/responses/xai-responses-prepare-tools.ts +6 -6
- package/src/xai-chat-language-model.ts +61 -25
- package/src/xai-chat-options.ts +3 -6
- package/src/xai-chat-prompt.ts +2 -1
- package/src/xai-image-model.ts +25 -8
- package/src/xai-image-settings.ts +0 -2
- package/src/xai-prepare-tools.ts +6 -6
- package/src/xai-provider.ts +34 -21
- package/src/xai-video-model.ts +127 -20
- package/src/xai-video-options.ts +136 -14
- package/dist/index.d.mts +0 -375
- package/dist/index.mjs +0 -3061
- package/dist/index.mjs.map +0 -1
package/docs/01-xai.mdx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: xAI Grok
|
|
3
|
-
description: Learn how to use xAI Grok.
|
|
3
|
+
description: Learn how to use xAI Grok and Imagine.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# xAI Grok Provider
|
|
@@ -73,13 +73,13 @@ You can use the following optional settings to customize the xAI provider instan
|
|
|
73
73
|
## Language Models
|
|
74
74
|
|
|
75
75
|
You can create [xAI models](https://console.x.ai) using a provider instance. The
|
|
76
|
-
first argument is the model id, e.g. `grok-
|
|
76
|
+
first argument is the model id, e.g. `grok-4.20-non-reasoning`.
|
|
77
77
|
|
|
78
78
|
```ts
|
|
79
|
-
const model = xai('grok-
|
|
79
|
+
const model = xai('grok-4.20-non-reasoning');
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
By default, `xai(modelId)` uses the
|
|
82
|
+
By default, `xai(modelId)` uses the Responses API. To use the [Chat Completions API](https://docs.x.ai/docs/api-reference#chat-completions) (legacy), use `xai.chat(modelId)`.
|
|
83
83
|
|
|
84
84
|
### Example
|
|
85
85
|
|
|
@@ -90,7 +90,7 @@ import { xai } from '@ai-sdk/xai';
|
|
|
90
90
|
import { generateText } from 'ai';
|
|
91
91
|
|
|
92
92
|
const { text } = await generateText({
|
|
93
|
-
model: xai('grok-
|
|
93
|
+
model: xai('grok-4.20-non-reasoning'),
|
|
94
94
|
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
95
95
|
});
|
|
96
96
|
```
|
|
@@ -99,50 +99,12 @@ xAI language models can also be used in the `streamText` function
|
|
|
99
99
|
and support structured data generation with [`Output`](/docs/reference/ai-sdk-core/output)
|
|
100
100
|
(see [AI SDK Core](/docs/ai-sdk-core)).
|
|
101
101
|
|
|
102
|
-
### Provider Options
|
|
103
|
-
|
|
104
|
-
xAI chat models support additional provider options that are not part of
|
|
105
|
-
the [standard call settings](/docs/ai-sdk-core/settings). You can pass them in the `providerOptions` argument:
|
|
106
|
-
|
|
107
|
-
```ts
|
|
108
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
109
|
-
|
|
110
|
-
const model = xai('grok-3-mini');
|
|
111
|
-
|
|
112
|
-
await generateText({
|
|
113
|
-
model,
|
|
114
|
-
providerOptions: {
|
|
115
|
-
xai: {
|
|
116
|
-
reasoningEffort: 'high',
|
|
117
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
The following optional provider options are available for xAI chat models:
|
|
123
|
-
|
|
124
|
-
- **reasoningEffort** _'low' | 'high'_
|
|
125
|
-
|
|
126
|
-
Reasoning effort for reasoning models.
|
|
127
|
-
|
|
128
|
-
- **logprobs** _boolean_
|
|
129
|
-
|
|
130
|
-
Return log probabilities for output tokens.
|
|
131
|
-
|
|
132
|
-
- **topLogprobs** _number_
|
|
133
|
-
|
|
134
|
-
Number of most likely tokens to return per token position (0-8). When set, `logprobs` is automatically enabled.
|
|
135
|
-
|
|
136
|
-
- **parallel_function_calling** _boolean_
|
|
137
|
-
|
|
138
|
-
Whether to enable parallel function calling during tool use. When true, the model can call multiple functions in parallel. When false, the model will call functions sequentially. Defaults to `true`.
|
|
139
|
-
|
|
140
102
|
## Responses API (Agentic Tools)
|
|
141
103
|
|
|
142
|
-
|
|
104
|
+
The xAI Responses API is the default when using `xai(modelId)`. You can also use `xai.responses(modelId)` explicitly. This enables the model to autonomously orchestrate tool calls and research on xAI's servers.
|
|
143
105
|
|
|
144
106
|
```ts
|
|
145
|
-
const model = xai.responses('grok-4-
|
|
107
|
+
const model = xai.responses('grok-4.20-non-reasoning');
|
|
146
108
|
```
|
|
147
109
|
|
|
148
110
|
The Responses API provides server-side tools that the model can autonomously execute during its reasoning process:
|
|
@@ -164,7 +126,7 @@ import { xai } from '@ai-sdk/xai';
|
|
|
164
126
|
import { generateText } from 'ai';
|
|
165
127
|
|
|
166
128
|
const { text } = await generateText({
|
|
167
|
-
model: xai.responses('grok-
|
|
129
|
+
model: xai.responses('grok-3'),
|
|
168
130
|
messages: [
|
|
169
131
|
{
|
|
170
132
|
role: 'user',
|
|
@@ -186,7 +148,7 @@ import { xai } from '@ai-sdk/xai';
|
|
|
186
148
|
import { generateText } from 'ai';
|
|
187
149
|
|
|
188
150
|
const { text, sources } = await generateText({
|
|
189
|
-
model: xai.responses('grok-4-
|
|
151
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
190
152
|
prompt: 'What are the latest developments in AI?',
|
|
191
153
|
tools: {
|
|
192
154
|
web_search: xai.tools.webSearch({
|
|
@@ -220,7 +182,7 @@ The X search tool enables searching X (Twitter) for posts, with filtering by han
|
|
|
220
182
|
|
|
221
183
|
```ts
|
|
222
184
|
const { text, sources } = await generateText({
|
|
223
|
-
model: xai.responses('grok-4-
|
|
185
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
224
186
|
prompt: 'What are people saying about AI on X this week?',
|
|
225
187
|
tools: {
|
|
226
188
|
x_search: xai.tools.xSearch({
|
|
@@ -266,7 +228,7 @@ The code execution tool enables the model to write and execute Python code for c
|
|
|
266
228
|
|
|
267
229
|
```ts
|
|
268
230
|
const { text } = await generateText({
|
|
269
|
-
model: xai.responses('grok-4-
|
|
231
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
270
232
|
prompt:
|
|
271
233
|
'Calculate the compound interest for $10,000 at 5% annually for 10 years',
|
|
272
234
|
tools: {
|
|
@@ -281,7 +243,7 @@ The view image tool enables the model to view and analyze images:
|
|
|
281
243
|
|
|
282
244
|
```ts
|
|
283
245
|
const { text } = await generateText({
|
|
284
|
-
model: xai.responses('grok-4-
|
|
246
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
285
247
|
prompt: 'Describe what you see in the image',
|
|
286
248
|
tools: {
|
|
287
249
|
view_image: xai.tools.viewImage(),
|
|
@@ -295,7 +257,7 @@ The view X video tool enables the model to view and analyze videos from X (Twitt
|
|
|
295
257
|
|
|
296
258
|
```ts
|
|
297
259
|
const { text } = await generateText({
|
|
298
|
-
model: xai.responses('grok-4-
|
|
260
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
299
261
|
prompt: 'Summarize the content of this X video',
|
|
300
262
|
tools: {
|
|
301
263
|
view_x_video: xai.tools.viewXVideo(),
|
|
@@ -309,7 +271,7 @@ The MCP server tool enables the model to connect to remote [Model Context Protoc
|
|
|
309
271
|
|
|
310
272
|
```ts
|
|
311
273
|
const { text } = await generateText({
|
|
312
|
-
model: xai.responses('grok-4-
|
|
274
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
313
275
|
prompt: 'Use the weather tool to check conditions in San Francisco',
|
|
314
276
|
tools: {
|
|
315
277
|
weather_server: xai.tools.mcpServer({
|
|
@@ -357,7 +319,7 @@ import { xai, type XaiLanguageModelResponsesOptions } from '@ai-sdk/xai';
|
|
|
357
319
|
import { streamText } from 'ai';
|
|
358
320
|
|
|
359
321
|
const result = streamText({
|
|
360
|
-
model: xai.responses('grok-4-
|
|
322
|
+
model: xai.responses('grok-4.20-reasoning'),
|
|
361
323
|
prompt: 'What documents do you have access to?',
|
|
362
324
|
tools: {
|
|
363
325
|
file_search: xai.tools.fileSearch({
|
|
@@ -390,7 +352,7 @@ const result = streamText({
|
|
|
390
352
|
Include file search results in the response. When set to `['file_search_call.results']`, the response will contain the actual search results with file content and scores.
|
|
391
353
|
|
|
392
354
|
<Note>
|
|
393
|
-
File search requires grok-4 family models and the Responses API. Vector stores
|
|
355
|
+
File search requires grok-4 family models (including grok-4.20) and the Responses API. Vector stores
|
|
394
356
|
can be created using the [xAI
|
|
395
357
|
API](https://docs.x.ai/docs/guides/using-collections/api).
|
|
396
358
|
</Note>
|
|
@@ -404,7 +366,7 @@ import { xai } from '@ai-sdk/xai';
|
|
|
404
366
|
import { streamText } from 'ai';
|
|
405
367
|
|
|
406
368
|
const { fullStream } = streamText({
|
|
407
|
-
model: xai.responses('grok-4-
|
|
369
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
408
370
|
prompt: 'Research AI safety developments and calculate risk metrics',
|
|
409
371
|
tools: {
|
|
410
372
|
web_search: xai.tools.webSearch(),
|
|
@@ -438,7 +400,7 @@ import { xai, type XaiLanguageModelResponsesOptions } from '@ai-sdk/xai';
|
|
|
438
400
|
import { generateText } from 'ai';
|
|
439
401
|
|
|
440
402
|
const result = await generateText({
|
|
441
|
-
model: xai.responses('grok-4-
|
|
403
|
+
model: xai.responses('grok-4.20-non-reasoning'),
|
|
442
404
|
providerOptions: {
|
|
443
405
|
xai: {
|
|
444
406
|
reasoningEffort: 'high',
|
|
@@ -479,311 +441,20 @@ The following provider options are available:
|
|
|
479
441
|
tools with client-side function tools in the same request.
|
|
480
442
|
</Note>
|
|
481
443
|
|
|
482
|
-
## Live Search
|
|
483
|
-
|
|
484
|
-
xAI models support Live Search functionality, allowing them to query real-time data from various sources and include it in responses with citations.
|
|
485
|
-
|
|
486
|
-
### Basic Search
|
|
487
|
-
|
|
488
|
-
To enable search, specify `searchParameters` with a search mode:
|
|
489
|
-
|
|
490
|
-
```ts
|
|
491
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
492
|
-
import { generateText } from 'ai';
|
|
493
|
-
|
|
494
|
-
const { text, sources } = await generateText({
|
|
495
|
-
model: xai('grok-3-latest'),
|
|
496
|
-
prompt: 'What are the latest developments in AI?',
|
|
497
|
-
providerOptions: {
|
|
498
|
-
xai: {
|
|
499
|
-
searchParameters: {
|
|
500
|
-
mode: 'auto', // 'auto', 'on', or 'off'
|
|
501
|
-
returnCitations: true,
|
|
502
|
-
maxSearchResults: 5,
|
|
503
|
-
},
|
|
504
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
505
|
-
},
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
console.log(text);
|
|
509
|
-
console.log('Sources:', sources);
|
|
510
|
-
```
|
|
511
|
-
|
|
512
|
-
### Search Parameters
|
|
513
|
-
|
|
514
|
-
The following search parameters are available:
|
|
515
|
-
|
|
516
|
-
- **mode** _'auto' | 'on' | 'off'_
|
|
517
|
-
|
|
518
|
-
Search mode preference:
|
|
519
|
-
|
|
520
|
-
- `'auto'` (default): Model decides whether to search
|
|
521
|
-
- `'on'`: Always enables search
|
|
522
|
-
- `'off'`: Disables search completely
|
|
523
|
-
|
|
524
|
-
- **returnCitations** _boolean_
|
|
525
|
-
|
|
526
|
-
Whether to return citations in the response. Defaults to `true`.
|
|
527
|
-
|
|
528
|
-
- **fromDate** _string_
|
|
529
|
-
|
|
530
|
-
Start date for search data in ISO8601 format (`YYYY-MM-DD`).
|
|
531
|
-
|
|
532
|
-
- **toDate** _string_
|
|
533
|
-
|
|
534
|
-
End date for search data in ISO8601 format (`YYYY-MM-DD`).
|
|
535
|
-
|
|
536
|
-
- **maxSearchResults** _number_
|
|
537
|
-
|
|
538
|
-
Maximum number of search results to consider. Defaults to 20, max 50.
|
|
539
|
-
|
|
540
|
-
- **sources** _Array<SearchSource>_
|
|
541
|
-
|
|
542
|
-
Data sources to search from. Defaults to `["web", "x"]` if not specified.
|
|
543
|
-
|
|
544
|
-
### Search Sources
|
|
545
|
-
|
|
546
|
-
You can specify different types of data sources for search:
|
|
547
|
-
|
|
548
|
-
#### Web Search
|
|
549
|
-
|
|
550
|
-
```ts
|
|
551
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
552
|
-
|
|
553
|
-
const result = await generateText({
|
|
554
|
-
model: xai('grok-3-latest'),
|
|
555
|
-
prompt: 'Best ski resorts in Switzerland',
|
|
556
|
-
providerOptions: {
|
|
557
|
-
xai: {
|
|
558
|
-
searchParameters: {
|
|
559
|
-
mode: 'on',
|
|
560
|
-
sources: [
|
|
561
|
-
{
|
|
562
|
-
type: 'web',
|
|
563
|
-
country: 'CH', // ISO alpha-2 country code
|
|
564
|
-
allowedWebsites: ['ski.com', 'snow-forecast.com'],
|
|
565
|
-
safeSearch: true,
|
|
566
|
-
},
|
|
567
|
-
],
|
|
568
|
-
},
|
|
569
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
570
|
-
},
|
|
571
|
-
});
|
|
572
|
-
```
|
|
573
|
-
|
|
574
|
-
#### Web source parameters
|
|
575
|
-
|
|
576
|
-
- **country** _string_: ISO alpha-2 country code
|
|
577
|
-
- **allowedWebsites** _string[]_: Max 5 allowed websites
|
|
578
|
-
- **excludedWebsites** _string[]_: Max 5 excluded websites
|
|
579
|
-
- **safeSearch** _boolean_: Enable safe search (default: true)
|
|
580
|
-
|
|
581
|
-
#### X (Twitter) Search
|
|
582
|
-
|
|
583
|
-
```ts
|
|
584
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
585
|
-
|
|
586
|
-
const result = await generateText({
|
|
587
|
-
model: xai('grok-3-latest'),
|
|
588
|
-
prompt: 'Latest updates on Grok AI',
|
|
589
|
-
providerOptions: {
|
|
590
|
-
xai: {
|
|
591
|
-
searchParameters: {
|
|
592
|
-
mode: 'on',
|
|
593
|
-
sources: [
|
|
594
|
-
{
|
|
595
|
-
type: 'x',
|
|
596
|
-
includedXHandles: ['grok', 'xai'],
|
|
597
|
-
excludedXHandles: ['openai'],
|
|
598
|
-
postFavoriteCount: 10,
|
|
599
|
-
postViewCount: 100,
|
|
600
|
-
},
|
|
601
|
-
],
|
|
602
|
-
},
|
|
603
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
604
|
-
},
|
|
605
|
-
});
|
|
606
|
-
```
|
|
607
|
-
|
|
608
|
-
#### X source parameters
|
|
609
|
-
|
|
610
|
-
- **includedXHandles** _string[]_: Array of X handles to search (without @ symbol)
|
|
611
|
-
- **excludedXHandles** _string[]_: Array of X handles to exclude from search (without @ symbol)
|
|
612
|
-
- **postFavoriteCount** _number_: Minimum favorite count of the X posts to consider.
|
|
613
|
-
- **postViewCount** _number_: Minimum view count of the X posts to consider.
|
|
614
|
-
|
|
615
|
-
#### News Search
|
|
616
|
-
|
|
617
|
-
```ts
|
|
618
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
619
|
-
|
|
620
|
-
const result = await generateText({
|
|
621
|
-
model: xai('grok-3-latest'),
|
|
622
|
-
prompt: 'Recent tech industry news',
|
|
623
|
-
providerOptions: {
|
|
624
|
-
xai: {
|
|
625
|
-
searchParameters: {
|
|
626
|
-
mode: 'on',
|
|
627
|
-
sources: [
|
|
628
|
-
{
|
|
629
|
-
type: 'news',
|
|
630
|
-
country: 'US',
|
|
631
|
-
excludedWebsites: ['tabloid.com'],
|
|
632
|
-
safeSearch: true,
|
|
633
|
-
},
|
|
634
|
-
],
|
|
635
|
-
},
|
|
636
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
637
|
-
},
|
|
638
|
-
});
|
|
639
|
-
```
|
|
640
|
-
|
|
641
|
-
#### News source parameters
|
|
642
|
-
|
|
643
|
-
- **country** _string_: ISO alpha-2 country code
|
|
644
|
-
- **excludedWebsites** _string[]_: Max 5 excluded websites
|
|
645
|
-
- **safeSearch** _boolean_: Enable safe search (default: true)
|
|
646
|
-
|
|
647
|
-
#### RSS Feed Search
|
|
648
|
-
|
|
649
|
-
```ts
|
|
650
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
651
|
-
|
|
652
|
-
const result = await generateText({
|
|
653
|
-
model: xai('grok-3-latest'),
|
|
654
|
-
prompt: 'Latest status updates',
|
|
655
|
-
providerOptions: {
|
|
656
|
-
xai: {
|
|
657
|
-
searchParameters: {
|
|
658
|
-
mode: 'on',
|
|
659
|
-
sources: [
|
|
660
|
-
{
|
|
661
|
-
type: 'rss',
|
|
662
|
-
links: ['https://status.x.ai/feed.xml'],
|
|
663
|
-
},
|
|
664
|
-
],
|
|
665
|
-
},
|
|
666
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
667
|
-
},
|
|
668
|
-
});
|
|
669
|
-
```
|
|
670
|
-
|
|
671
|
-
#### RSS source parameters
|
|
672
|
-
|
|
673
|
-
- **links** _string[]_: Array of RSS feed URLs (max 1 currently supported)
|
|
674
|
-
|
|
675
|
-
### Multiple Sources
|
|
676
|
-
|
|
677
|
-
You can combine multiple data sources in a single search:
|
|
678
|
-
|
|
679
|
-
```ts
|
|
680
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
681
|
-
|
|
682
|
-
const result = await generateText({
|
|
683
|
-
model: xai('grok-3-latest'),
|
|
684
|
-
prompt: 'Comprehensive overview of recent AI breakthroughs',
|
|
685
|
-
providerOptions: {
|
|
686
|
-
xai: {
|
|
687
|
-
searchParameters: {
|
|
688
|
-
mode: 'on',
|
|
689
|
-
returnCitations: true,
|
|
690
|
-
maxSearchResults: 15,
|
|
691
|
-
sources: [
|
|
692
|
-
{
|
|
693
|
-
type: 'web',
|
|
694
|
-
allowedWebsites: ['arxiv.org', 'openai.com'],
|
|
695
|
-
},
|
|
696
|
-
{
|
|
697
|
-
type: 'news',
|
|
698
|
-
country: 'US',
|
|
699
|
-
},
|
|
700
|
-
{
|
|
701
|
-
type: 'x',
|
|
702
|
-
includedXHandles: ['openai', 'deepmind'],
|
|
703
|
-
},
|
|
704
|
-
],
|
|
705
|
-
},
|
|
706
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
707
|
-
},
|
|
708
|
-
});
|
|
709
|
-
```
|
|
710
|
-
|
|
711
|
-
### Sources and Citations
|
|
712
|
-
|
|
713
|
-
When search is enabled with `returnCitations: true`, the response includes sources that were used to generate the answer:
|
|
714
|
-
|
|
715
|
-
```ts
|
|
716
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
717
|
-
|
|
718
|
-
const { text, sources } = await generateText({
|
|
719
|
-
model: xai('grok-3-latest'),
|
|
720
|
-
prompt: 'What are the latest developments in AI?',
|
|
721
|
-
providerOptions: {
|
|
722
|
-
xai: {
|
|
723
|
-
searchParameters: {
|
|
724
|
-
mode: 'auto',
|
|
725
|
-
returnCitations: true,
|
|
726
|
-
},
|
|
727
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
728
|
-
},
|
|
729
|
-
});
|
|
730
|
-
|
|
731
|
-
// Access the sources used
|
|
732
|
-
for (const source of sources) {
|
|
733
|
-
if (source.sourceType === 'url') {
|
|
734
|
-
console.log('Source:', source.url);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
```
|
|
738
|
-
|
|
739
|
-
### Streaming with Search
|
|
740
|
-
|
|
741
|
-
Live Search works with streaming responses. Citations are included when the stream completes:
|
|
742
|
-
|
|
743
|
-
```ts
|
|
744
|
-
import { xai, type XaiLanguageModelChatOptions } from '@ai-sdk/xai';
|
|
745
|
-
import { streamText } from 'ai';
|
|
746
|
-
|
|
747
|
-
const result = streamText({
|
|
748
|
-
model: xai('grok-3-latest'),
|
|
749
|
-
prompt: 'What has happened in tech recently?',
|
|
750
|
-
providerOptions: {
|
|
751
|
-
xai: {
|
|
752
|
-
searchParameters: {
|
|
753
|
-
mode: 'auto',
|
|
754
|
-
returnCitations: true,
|
|
755
|
-
},
|
|
756
|
-
} satisfies XaiLanguageModelChatOptions,
|
|
757
|
-
},
|
|
758
|
-
});
|
|
759
|
-
|
|
760
|
-
for await (const textPart of result.textStream) {
|
|
761
|
-
process.stdout.write(textPart);
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
console.log('Sources:', await result.sources);
|
|
765
|
-
```
|
|
766
|
-
|
|
767
444
|
## Model Capabilities
|
|
768
445
|
|
|
769
446
|
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming | Reasoning |
|
|
770
447
|
| ----------------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
|
|
771
|
-
| `grok-4-
|
|
448
|
+
| `grok-4.20-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
449
|
+
| `grok-4.20-non-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
772
450
|
| `grok-4-1-fast-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
773
451
|
| `grok-4-1-fast-non-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
774
|
-
| `grok-4-
|
|
452
|
+
| `grok-4-1` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
775
453
|
| `grok-4-fast-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
454
|
+
| `grok-4-fast-non-reasoning` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
776
455
|
| `grok-code-fast-1` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
777
|
-
| `grok-4` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
778
|
-
| `grok-4-0709` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
779
|
-
| `grok-4-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
780
456
|
| `grok-3` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
781
|
-
| `grok-3-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
782
457
|
| `grok-3-mini` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
783
|
-
| `grok-3-mini-latest` | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
784
|
-
| `grok-2-vision` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
785
|
-
| `grok-2-vision-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
786
|
-
| `grok-2-vision-1212` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
787
458
|
|
|
788
459
|
<Note>
|
|
789
460
|
The table above lists popular models. Please see the [xAI
|
|
@@ -800,7 +471,7 @@ import { xai } from '@ai-sdk/xai';
|
|
|
800
471
|
import { generateImage } from 'ai';
|
|
801
472
|
|
|
802
473
|
const { image } = await generateImage({
|
|
803
|
-
model: xai.image('grok-
|
|
474
|
+
model: xai.image('grok-imagine-image'),
|
|
804
475
|
prompt: 'A futuristic cityscape at sunset',
|
|
805
476
|
});
|
|
806
477
|
```
|
|
@@ -813,7 +484,7 @@ const { image } = await generateImage({
|
|
|
813
484
|
|
|
814
485
|
### Image Editing
|
|
815
486
|
|
|
816
|
-
xAI supports image editing through the `grok-
|
|
487
|
+
xAI supports image editing through the `grok-imagine-image` model. Pass input images via `prompt.images` to transform or edit existing images.
|
|
817
488
|
|
|
818
489
|
<Note>
|
|
819
490
|
xAI image editing does not support masks. Editing is prompt-driven - describe
|
|
@@ -832,7 +503,7 @@ import { readFileSync } from 'fs';
|
|
|
832
503
|
const imageBuffer = readFileSync('./input-image.png');
|
|
833
504
|
|
|
834
505
|
const { images } = await generateImage({
|
|
835
|
-
model: xai.image('grok-
|
|
506
|
+
model: xai.image('grok-imagine-image'),
|
|
836
507
|
prompt: {
|
|
837
508
|
text: 'Turn the cat into a golden retriever dog',
|
|
838
509
|
images: [imageBuffer],
|
|
@@ -842,7 +513,7 @@ const { images } = await generateImage({
|
|
|
842
513
|
|
|
843
514
|
#### Multi-Image Editing
|
|
844
515
|
|
|
845
|
-
Combine or reference multiple input images
|
|
516
|
+
Combine or reference multiple input images in the prompt:
|
|
846
517
|
|
|
847
518
|
```ts
|
|
848
519
|
import { xai } from '@ai-sdk/xai';
|
|
@@ -869,7 +540,7 @@ Apply artistic styles to an image:
|
|
|
869
540
|
const imageBuffer = readFileSync('./input-image.png');
|
|
870
541
|
|
|
871
542
|
const { images } = await generateImage({
|
|
872
|
-
model: xai.image('grok-
|
|
543
|
+
model: xai.image('grok-imagine-image'),
|
|
873
544
|
prompt: {
|
|
874
545
|
text: 'Transform this into a watercolor painting style',
|
|
875
546
|
images: [imageBuffer],
|
|
@@ -880,38 +551,53 @@ const { images } = await generateImage({
|
|
|
880
551
|
|
|
881
552
|
<Note>
|
|
882
553
|
Input images can be provided as `Buffer`, `ArrayBuffer`, `Uint8Array`, or
|
|
883
|
-
base64-encoded strings.
|
|
554
|
+
base64-encoded strings.
|
|
884
555
|
</Note>
|
|
885
556
|
|
|
886
|
-
###
|
|
557
|
+
### Image Provider Options
|
|
887
558
|
|
|
888
|
-
You can customize the image generation behavior with
|
|
559
|
+
You can customize the image generation behavior with provider-specific settings via `providerOptions.xai`:
|
|
889
560
|
|
|
890
561
|
```ts
|
|
891
|
-
import { xai } from '@ai-sdk/xai';
|
|
562
|
+
import { xai, type XaiImageModelOptions } from '@ai-sdk/xai';
|
|
892
563
|
import { generateImage } from 'ai';
|
|
893
564
|
|
|
894
565
|
const { images } = await generateImage({
|
|
895
|
-
model: xai.image('grok-
|
|
566
|
+
model: xai.image('grok-imagine-image-pro'),
|
|
896
567
|
prompt: 'A futuristic cityscape at sunset',
|
|
897
568
|
aspectRatio: '16:9',
|
|
898
|
-
|
|
569
|
+
providerOptions: {
|
|
570
|
+
xai: {
|
|
571
|
+
resolution: '2k',
|
|
572
|
+
quality: 'high',
|
|
573
|
+
} satisfies XaiImageModelOptions,
|
|
574
|
+
},
|
|
899
575
|
});
|
|
900
576
|
```
|
|
901
577
|
|
|
902
|
-
|
|
578
|
+
- **resolution** _'1k' | '2k'_
|
|
579
|
+
|
|
580
|
+
Output resolution. `1k` produces ~1024×1024 images, `2k` produces ~2048×2048
|
|
581
|
+
images (actual dimensions vary based on aspect ratio). Available for
|
|
582
|
+
`grok-imagine-image-pro`.
|
|
583
|
+
|
|
584
|
+
- **quality** _'low' | 'medium' | 'high'_
|
|
585
|
+
|
|
586
|
+
Image quality level. Higher quality may increase generation time.
|
|
587
|
+
|
|
588
|
+
### Image Model Capabilities
|
|
903
589
|
|
|
904
|
-
| Model
|
|
905
|
-
|
|
|
906
|
-
| `grok-
|
|
907
|
-
| `grok-imagine-image` | `1:1`, `16:9`, `9:16`, `4:3`, `3:4`, `3:2`, `2:3`, `2:1`, `1:2`, `19.5:9`, `9:19.5`, `20:9`, `9:20`, `auto` | <Check size={18} /> |
|
|
590
|
+
| Model | Resolution | Aspect Ratios | Image Editing |
|
|
591
|
+
| ------------------------ | ------------ | ----------------------------------------------------------------------------------------------------------- | ------------------- |
|
|
592
|
+
| `grok-imagine-image-pro` | `1k`, `2k` | `1:1`, `16:9`, `9:16`, `4:3`, `3:4`, `3:2`, `2:3`, `2:1`, `1:2`, `19.5:9`, `9:19.5`, `20:9`, `9:20`, `auto` | <Check size={18} /> |
|
|
593
|
+
| `grok-imagine-image` | `1k` | `1:1`, `16:9`, `9:16`, `4:3`, `3:4`, `3:2`, `2:3`, `2:1`, `1:2`, `19.5:9`, `9:19.5`, `20:9`, `9:20`, `auto` | <Check size={18} /> |
|
|
908
594
|
|
|
909
595
|
## Video Models
|
|
910
596
|
|
|
911
597
|
You can create xAI video models using the `.video()` factory method.
|
|
912
598
|
For more on video generation with the AI SDK see [generateVideo()](/docs/reference/ai-sdk-core/generate-video).
|
|
913
599
|
|
|
914
|
-
This provider supports
|
|
600
|
+
This provider supports standard video generation from text prompts or image input, plus explicit video editing, video extension, and reference-to-video (R2V) operations.
|
|
915
601
|
|
|
916
602
|
### Text-to-Video
|
|
917
603
|
|
|
@@ -921,7 +607,7 @@ Generate videos from text prompts:
|
|
|
921
607
|
import { xai, type XaiVideoModelOptions } from '@ai-sdk/xai';
|
|
922
608
|
import { experimental_generateVideo as generateVideo } from 'ai';
|
|
923
609
|
|
|
924
|
-
const {
|
|
610
|
+
const { video } = await generateVideo({
|
|
925
611
|
model: xai.video('grok-imagine-video'),
|
|
926
612
|
prompt: 'A chicken flying into the sunset in the style of 90s anime.',
|
|
927
613
|
aspectRatio: '16:9',
|
|
@@ -934,15 +620,15 @@ const { videos } = await generateVideo({
|
|
|
934
620
|
});
|
|
935
621
|
```
|
|
936
622
|
|
|
937
|
-
### Image
|
|
623
|
+
### Generation with Image Input
|
|
938
624
|
|
|
939
|
-
Generate videos using an image as the starting frame with an optional text prompt:
|
|
625
|
+
Generate videos using an image as the starting frame with an optional text prompt. This uses the standard generation path rather than a separate provider mode:
|
|
940
626
|
|
|
941
627
|
```ts
|
|
942
628
|
import { xai, type XaiVideoModelOptions } from '@ai-sdk/xai';
|
|
943
629
|
import { experimental_generateVideo as generateVideo } from 'ai';
|
|
944
630
|
|
|
945
|
-
const {
|
|
631
|
+
const { video } = await generateVideo({
|
|
946
632
|
model: xai.video('grok-imagine-video'),
|
|
947
633
|
prompt: {
|
|
948
634
|
image: 'https://example.com/start-frame.png',
|
|
@@ -965,11 +651,12 @@ Edit an existing video using a text prompt by providing a source video URL via p
|
|
|
965
651
|
import { xai, type XaiVideoModelOptions } from '@ai-sdk/xai';
|
|
966
652
|
import { experimental_generateVideo as generateVideo } from 'ai';
|
|
967
653
|
|
|
968
|
-
const {
|
|
654
|
+
const { video } = await generateVideo({
|
|
969
655
|
model: xai.video('grok-imagine-video'),
|
|
970
656
|
prompt: 'Give the person sunglasses and a hat',
|
|
971
657
|
providerOptions: {
|
|
972
658
|
xai: {
|
|
659
|
+
mode: 'edit-video',
|
|
973
660
|
videoUrl: 'https://example.com/source-video.mp4',
|
|
974
661
|
pollTimeoutMs: 600000, // 10 minutes
|
|
975
662
|
} satisfies XaiVideoModelOptions,
|
|
@@ -995,6 +682,7 @@ import { experimental_generateVideo as generateVideo } from 'ai';
|
|
|
995
682
|
|
|
996
683
|
const providerOptions = {
|
|
997
684
|
xai: {
|
|
685
|
+
mode: 'edit-video',
|
|
998
686
|
videoUrl: 'https://example.com/source-video.mp4',
|
|
999
687
|
pollTimeoutMs: 600000,
|
|
1000
688
|
} satisfies XaiVideoModelOptions,
|
|
@@ -1016,19 +704,99 @@ const [withSunglasses, withScarf] = await Promise.all([
|
|
|
1016
704
|
model: xai.video('grok-imagine-video'),
|
|
1017
705
|
prompt: 'Add sunglasses',
|
|
1018
706
|
providerOptions: {
|
|
1019
|
-
xai: { videoUrl: step1VideoUrl, pollTimeoutMs: 600000 },
|
|
707
|
+
xai: { mode: 'edit-video', videoUrl: step1VideoUrl, pollTimeoutMs: 600000 },
|
|
1020
708
|
},
|
|
1021
709
|
}),
|
|
1022
710
|
generateVideo({
|
|
1023
711
|
model: xai.video('grok-imagine-video'),
|
|
1024
712
|
prompt: 'Add a scarf',
|
|
1025
713
|
providerOptions: {
|
|
1026
|
-
xai: { videoUrl: step1VideoUrl, pollTimeoutMs: 600000 },
|
|
714
|
+
xai: { mode: 'edit-video', videoUrl: step1VideoUrl, pollTimeoutMs: 600000 },
|
|
1027
715
|
},
|
|
1028
716
|
}),
|
|
1029
717
|
]);
|
|
1030
718
|
```
|
|
1031
719
|
|
|
720
|
+
### Video Extension
|
|
721
|
+
|
|
722
|
+
Extend an existing video from its last frame. The `duration` controls the length of the extension only, not the total output. The output inherits `aspectRatio` and `resolution` from the source video.
|
|
723
|
+
|
|
724
|
+
```ts
|
|
725
|
+
import { xai, type XaiVideoModelOptions } from '@ai-sdk/xai';
|
|
726
|
+
import { experimental_generateVideo as generateVideo } from 'ai';
|
|
727
|
+
|
|
728
|
+
// Step 1: Generate a source video
|
|
729
|
+
const source = await generateVideo({
|
|
730
|
+
model: xai.video('grok-imagine-video'),
|
|
731
|
+
prompt: 'A cat sitting on a sunlit windowsill, tail gently swishing.',
|
|
732
|
+
duration: 5,
|
|
733
|
+
aspectRatio: '16:9',
|
|
734
|
+
providerOptions: {
|
|
735
|
+
xai: {
|
|
736
|
+
pollTimeoutMs: 600000,
|
|
737
|
+
} satisfies XaiVideoModelOptions,
|
|
738
|
+
},
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
const sourceUrl = source.providerMetadata?.xai?.videoUrl as string;
|
|
742
|
+
|
|
743
|
+
// Step 2: Extend the video with a new scene
|
|
744
|
+
const extended = await generateVideo({
|
|
745
|
+
model: xai.video('grok-imagine-video'),
|
|
746
|
+
prompt: 'The cat turns its head, notices a butterfly, and leaps off.',
|
|
747
|
+
duration: 6,
|
|
748
|
+
providerOptions: {
|
|
749
|
+
xai: {
|
|
750
|
+
mode: 'extend-video',
|
|
751
|
+
videoUrl: sourceUrl,
|
|
752
|
+
pollTimeoutMs: 600000,
|
|
753
|
+
} satisfies XaiVideoModelOptions,
|
|
754
|
+
},
|
|
755
|
+
});
|
|
756
|
+
```
|
|
757
|
+
|
|
758
|
+
<Note>
|
|
759
|
+
Video extension does not support custom `aspectRatio` or `resolution` — the
|
|
760
|
+
output inherits those from the source video. `duration` is supported and
|
|
761
|
+
controls how long the extension is (not the total video length).
|
|
762
|
+
</Note>
|
|
763
|
+
|
|
764
|
+
### Reference-to-Video (R2V)
|
|
765
|
+
|
|
766
|
+
Provide reference images to guide the video's style and content. Unlike image-to-video, reference images are not used as the first frame — the model incorporates their visual elements into the generated video. Each reference image can be a public HTTPS URL or a base64 data URI.
|
|
767
|
+
|
|
768
|
+
```ts
|
|
769
|
+
import { xai, type XaiVideoModelOptions } from '@ai-sdk/xai';
|
|
770
|
+
import { experimental_generateVideo as generateVideo } from 'ai';
|
|
771
|
+
|
|
772
|
+
const { video } = await generateVideo({
|
|
773
|
+
model: xai.video('grok-imagine-video'),
|
|
774
|
+
prompt:
|
|
775
|
+
'The comic cat from <IMAGE_1> and the comic dog from <IMAGE_2> ' +
|
|
776
|
+
'are having a playful chase through a sunlit park. ' +
|
|
777
|
+
'Cinematic slow-motion, warm afternoon light.',
|
|
778
|
+
duration: 8,
|
|
779
|
+
aspectRatio: '16:9',
|
|
780
|
+
providerOptions: {
|
|
781
|
+
xai: {
|
|
782
|
+
mode: 'reference-to-video',
|
|
783
|
+
referenceImageUrls: [
|
|
784
|
+
'https://example.com/comic-cat.png',
|
|
785
|
+
'https://example.com/comic-dog.png',
|
|
786
|
+
],
|
|
787
|
+
pollTimeoutMs: 600000,
|
|
788
|
+
} satisfies XaiVideoModelOptions,
|
|
789
|
+
},
|
|
790
|
+
});
|
|
791
|
+
```
|
|
792
|
+
|
|
793
|
+
Use `<IMAGE_1>`, `<IMAGE_2>`, etc. in your prompt to reference specific images. Up to 7 reference images are supported per request.
|
|
794
|
+
|
|
795
|
+
<Note>
|
|
796
|
+
Reference-to-video supports `duration`, `aspectRatio`, and `resolution`. Use
|
|
797
|
+
`mode` to select the operation — each mode is mutually exclusive.
|
|
798
|
+
</Note>
|
|
799
|
+
|
|
1032
800
|
### Video Provider Options
|
|
1033
801
|
|
|
1034
802
|
The following provider options are available via `providerOptions.xai`.
|
|
@@ -1048,10 +816,27 @@ You can validate the provider options using the `XaiVideoModelOptions` type.
|
|
|
1048
816
|
`1280x720` maps to `720p` and `854x480` maps to `480p`.
|
|
1049
817
|
Use this provider option to pass the native format directly.
|
|
1050
818
|
|
|
819
|
+
- **mode** _'edit-video' | 'extend-video' | 'reference-to-video'_
|
|
820
|
+
|
|
821
|
+
Selects the explicit video operation. Each mode is mutually exclusive:
|
|
822
|
+
- `'edit-video'` — edit an existing video (requires `videoUrl`)
|
|
823
|
+
- `'extend-video'` — extend a video from its last frame (requires `videoUrl`)
|
|
824
|
+
- `'reference-to-video'` — generate from reference images (requires `referenceImageUrls`)
|
|
825
|
+
|
|
826
|
+
When omitted, standard generation is used. Legacy inputs are still auto-detected from fields for backward compatibility.
|
|
827
|
+
|
|
1051
828
|
- **videoUrl** _string_
|
|
1052
829
|
|
|
1053
|
-
URL of a source video
|
|
1054
|
-
|
|
830
|
+
URL of a source video. Used with `mode: 'edit-video'` for video editing
|
|
831
|
+
and `mode: 'extend-video'` for video extension.
|
|
832
|
+
|
|
833
|
+
- **referenceImageUrls** _string[]_
|
|
834
|
+
|
|
835
|
+
Array of reference image URLs (1–7 images) or base64 data URIs for
|
|
836
|
+
reference-to-video (R2V) generation. The model incorporates visual
|
|
837
|
+
elements from these images without using them as the first frame. Use
|
|
838
|
+
`<IMAGE_1>`, `<IMAGE_2>`, etc. in the prompt to reference specific
|
|
839
|
+
images. Used with `mode: 'reference-to-video'`.
|
|
1055
840
|
|
|
1056
841
|
<Note>
|
|
1057
842
|
Video generation is an asynchronous process that can take several minutes.
|
|
@@ -1071,14 +856,21 @@ desired ratio.
|
|
|
1071
856
|
|
|
1072
857
|
For **video editing**, the output matches the input video's aspect ratio and
|
|
1073
858
|
resolution. Custom `duration`, `aspectRatio`, and `resolution` are not
|
|
1074
|
-
supported
|
|
859
|
+
supported — the output resolution is capped at 720p (e.g., a 1080p input
|
|
1075
860
|
will be downsized to 720p).
|
|
1076
861
|
|
|
862
|
+
For **video extension**, the output inherits `aspectRatio` and `resolution`
|
|
863
|
+
from the source video. `duration` is supported and controls only the
|
|
864
|
+
extension length.
|
|
865
|
+
|
|
866
|
+
For **reference-to-video (R2V)**, you can specify `duration`, `aspectRatio`,
|
|
867
|
+
and `resolution` just like text-to-video.
|
|
868
|
+
|
|
1077
869
|
### Video Model Capabilities
|
|
1078
870
|
|
|
1079
|
-
| Model | Duration | Aspect Ratios | Resolution | Image-to-Video |
|
|
1080
|
-
| -------------------- | -------- | ------------------------------------------------- | -------------- | ------------------- | ------------------- |
|
|
1081
|
-
| `grok-imagine-video` | 1–15s | `1:1`, `16:9`, `9:16`, `4:3`, `3:4`, `3:2`, `2:3` | `480p`, `720p` | <Check size={18} /> | <Check size={18} /> |
|
|
871
|
+
| Model | Duration | Aspect Ratios | Resolution | Image-to-Video | Editing | Extension | R2V |
|
|
872
|
+
| -------------------- | -------- | ------------------------------------------------- | -------------- | ------------------- | ------------------- | ------------------- | ------------------- |
|
|
873
|
+
| `grok-imagine-video` | 1–15s | `1:1`, `16:9`, `9:16`, `4:3`, `3:4`, `3:2`, `2:3` | `480p`, `720p` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
1082
874
|
|
|
1083
875
|
<Note>
|
|
1084
876
|
You can also pass any available provider model ID as a string if needed.
|