@ai-sdk/xai 4.0.44 → 4.0.48

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/docs/01-xai.mdx CHANGED
@@ -625,6 +625,66 @@ The following provider options are available:
625
625
  tools with client-side function tools in the same request.
626
626
  </Note>
627
627
 
628
+ ## Text Batches
629
+
630
+ <Note type="warning">
631
+ Text batch support is experimental and the API may change in patch releases.
632
+ </Note>
633
+
634
+ The xAI provider supports asynchronous text generation through the
635
+ [Batch API](https://docs.x.ai/developers/advanced-api-usage/batch-api). Use the
636
+ experimental text batch APIs to start a batch, poll its status, and stream its
637
+ results:
638
+
639
+ ```ts
640
+ import { xai } from '@ai-sdk/xai';
641
+ import {
642
+ experimental_getBatchResults as getBatchResults,
643
+ experimental_getBatchStatus as getBatchStatus,
644
+ experimental_startTextBatch as startTextBatch,
645
+ } from 'ai';
646
+ import { setTimeout } from 'node:timers/promises';
647
+
648
+ const model = xai('grok-4.3');
649
+
650
+ const batch = await startTextBatch({
651
+ model,
652
+ requests: [
653
+ { id: 'capital-france', prompt: 'What is the capital of France?' },
654
+ { id: 'capital-germany', prompt: 'What is the capital of Germany?' },
655
+ ],
656
+ });
657
+
658
+ let status = batch.status;
659
+ while (status === 'pending') {
660
+ await setTimeout(60_000);
661
+ ({ status } = await getBatchStatus({ model, batch }));
662
+ }
663
+
664
+ for await (const item of getBatchResults({ model, batch })) {
665
+ if (item.status === 'succeeded') {
666
+ console.log(item.id, item.text);
667
+ } else {
668
+ console.error(item.id, item.error);
669
+ }
670
+ }
671
+ ```
672
+
673
+ `startTextBatch` returns a serializable batch reference. Persist this reference
674
+ to check the batch status or retrieve its results from another process. Results
675
+ can arrive in a different order from the input requests, so match each result by
676
+ its `id`.
677
+
678
+ Text batches are available through `xai(modelId)` and
679
+ `xai.responses(modelId)`. The legacy `xai.chat(modelId)` models do not expose
680
+ the batch APIs.
681
+
682
+ <Note>
683
+ The xAI Batch API does not support per-batch webhooks. When you provide a
684
+ `webhookUrl`, the provider returns an unsupported warning and starts the batch
685
+ without a webhook.
686
+ </Note>
687
+
628
688
  ## Model Capabilities
629
689
 
630
690
  | Model | Image Input | Object Generation | Tool Usage | Tool Streaming | Reasoning |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/xai",
3
- "version": "4.0.44",
3
+ "version": "4.0.48",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@ai-sdk/provider": "4.0.8",
33
- "@ai-sdk/provider-utils": "5.0.30"
33
+ "@ai-sdk/provider-utils": "5.0.32"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@ai-sdk/test-server": "2.0.1",
@@ -168,6 +168,31 @@ const toolCallSchema = z.object({
168
168
  action: z.any().optional(),
169
169
  });
170
170
 
171
+ export const webSearchWireSourceSchema = z.object({
172
+ type: z.literal('url'),
173
+ url: z.string(),
174
+ });
175
+
176
+ export const webSearchWireActionSchema = z.discriminatedUnion('type', [
177
+ z.object({
178
+ type: z.literal('search'),
179
+ query: z.string().nullish(),
180
+ queries: z.array(z.string()).nullish(),
181
+ sources: z.array(z.unknown()).nullish(),
182
+ }),
183
+ z.object({
184
+ type: z.literal('open_page'),
185
+ url: z.string().nullish(),
186
+ sources: z.array(z.unknown()).nullish(),
187
+ }),
188
+ z.object({
189
+ type: z.literal('find_in_page'),
190
+ url: z.string().nullish(),
191
+ pattern: z.string().nullish(),
192
+ sources: z.array(z.unknown()).nullish(),
193
+ }),
194
+ ]);
195
+
171
196
  const mcpCallSchema = z.object({
172
197
  name: z.string().optional(),
173
198
  arguments: z.string().optional(),
@@ -260,24 +285,28 @@ const outputItemSchema = z.discriminatedUnion('type', [
260
285
  }),
261
286
  ]);
262
287
 
263
- export const xaiResponsesUsageSchema = z.object({
264
- input_tokens: z.number(),
265
- output_tokens: z.number(),
266
- total_tokens: z.number().optional(),
267
- input_tokens_details: z
268
- .object({
269
- cached_tokens: z.number().optional(),
270
- })
271
- .optional(),
272
- output_tokens_details: z
273
- .object({
274
- reasoning_tokens: z.number().optional(),
275
- })
276
- .optional(),
277
- num_sources_used: z.number().optional(),
278
- num_server_side_tools_used: z.number().optional(),
279
- cost_in_usd_ticks: z.number().nullish(),
280
- });
288
+ export const xaiResponsesUsageSchema = z
289
+ .object({
290
+ input_tokens: z.number(),
291
+ output_tokens: z.number(),
292
+ total_tokens: z.number().optional(),
293
+ input_tokens_details: z
294
+ .object({
295
+ cached_tokens: z.number().optional(),
296
+ })
297
+ .catchall(z.json())
298
+ .optional(),
299
+ output_tokens_details: z
300
+ .object({
301
+ reasoning_tokens: z.number().optional(),
302
+ })
303
+ .catchall(z.json())
304
+ .optional(),
305
+ num_sources_used: z.number().optional(),
306
+ num_server_side_tools_used: z.number().optional(),
307
+ cost_in_usd_ticks: z.number().nullish(),
308
+ })
309
+ .catchall(z.json());
281
310
 
282
311
  export const xaiResponsesResponseSchema = z.object({
283
312
  id: z.string().nullish(),