@ai-sdk/xai 4.0.54 → 4.0.56

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 CHANGED
@@ -1,5 +1,25 @@
1
1
  # @ai-sdk/xai
2
2
 
3
+ ## 4.0.56
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [912fb01]
8
+ - @ai-sdk/provider@4.0.12
9
+ - @ai-sdk/provider-utils@5.0.38
10
+
11
+ ## 4.0.55
12
+
13
+ ### Patch Changes
14
+
15
+ - a4ba394: feat: support per-request models in batch
16
+ - Updated dependencies [a4ba394]
17
+ - Updated dependencies [45099da]
18
+ - Updated dependencies [9e1d1b2]
19
+ - Updated dependencies [a495511]
20
+ - @ai-sdk/provider@4.0.11
21
+ - @ai-sdk/provider-utils@5.0.37
22
+
3
23
  ## 4.0.54
4
24
 
5
25
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod/v4';
2
2
  import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';
3
3
  import { InferSchema, FetchFunction, WebSocketConstructor } from '@ai-sdk/provider-utils';
4
- import { ProviderV4, Experimental_BatchLanguageModelV4, LanguageModelV4, ImageModelV4, Experimental_VideoModelV4, Experimental_RealtimeFactoryV4, SpeechModelV4, TranscriptionModelV4, FilesV4, Experimental_RealtimeModelV4, Experimental_RealtimeModelV4ClientSecretOptions, Experimental_RealtimeModelV4ClientSecretResult, Experimental_RealtimeModelV4ServerEvent, Experimental_RealtimeModelV4ClientEvent, Experimental_RealtimeModelV4SessionConfig } from '@ai-sdk/provider';
4
+ import { ProviderV4, LanguageModelV4, Experimental_BatchV4, ImageModelV4, Experimental_VideoModelV4, Experimental_RealtimeFactoryV4, SpeechModelV4, TranscriptionModelV4, FilesV4, Experimental_RealtimeModelV4, Experimental_RealtimeModelV4ClientSecretOptions, Experimental_RealtimeModelV4ClientSecretResult, Experimental_RealtimeModelV4ServerEvent, Experimental_RealtimeModelV4ClientEvent, Experimental_RealtimeModelV4SessionConfig } from '@ai-sdk/provider';
5
5
 
6
6
  type XaiChatModelId = 'grok-4.20-non-reasoning' | 'grok-4.20-reasoning' | 'grok-4.3' | 'grok-4.5' | 'grok-4.6' | 'grok-latest' | (string & {});
7
7
  declare const xaiLanguageModelChatOptions: z.ZodObject<{
@@ -535,11 +535,11 @@ declare const xaiTools: {
535
535
  };
536
536
 
537
537
  interface XaiProvider extends ProviderV4 {
538
- (modelId: XaiResponsesModelId): Experimental_BatchLanguageModelV4;
538
+ (modelId: XaiResponsesModelId): LanguageModelV4;
539
539
  /**
540
540
  * Creates an Xai language model for text generation.
541
541
  */
542
- languageModel(modelId: XaiResponsesModelId): Experimental_BatchLanguageModelV4;
542
+ languageModel(modelId: XaiResponsesModelId): LanguageModelV4;
543
543
  /**
544
544
  * Creates an Xai chat model for text generation.
545
545
  */
@@ -547,7 +547,13 @@ interface XaiProvider extends ProviderV4 {
547
547
  /**
548
548
  * Creates an Xai responses model for text generation.
549
549
  */
550
- responses: (modelId: XaiResponsesModelId) => Experimental_BatchLanguageModelV4;
550
+ responses: (modelId: XaiResponsesModelId) => LanguageModelV4;
551
+ /**
552
+ * Returns a BatchV4 interface for processing batches with xAI.
553
+ */
554
+ experimental_batch(): Experimental_BatchV4<{
555
+ text: XaiResponsesModelId;
556
+ }>;
551
557
  /**
552
558
  * Creates an Xai image model for image generation.
553
559
  */
package/dist/index.js CHANGED
@@ -1239,7 +1239,7 @@ var xaiImageResponseSchema = z6.object({
1239
1239
  }).nullish()
1240
1240
  });
1241
1241
 
1242
- // src/responses/xai-responses-batch.ts
1242
+ // src/xai-batch.ts
1243
1243
  import {
1244
1244
  InvalidArgumentError
1245
1245
  } from "@ai-sdk/provider";
@@ -1255,8 +1255,6 @@ import {
1255
1255
  postFormDataToApi,
1256
1256
  postJsonToApi as postJsonToApi4,
1257
1257
  safeValidateTypes,
1258
- WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE4,
1259
- WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE4,
1260
1258
  zodSchema as zodSchema7
1261
1259
  } from "@ai-sdk/provider-utils";
1262
1260
  import { z as z15 } from "zod/v4";
@@ -2520,17 +2518,18 @@ function getHttpStatusCode(value) {
2520
2518
  function isRetryableStatusCode(statusCode) {
2521
2519
  return statusCode === 408 || statusCode === 409 || statusCode === 429 || statusCode >= 500;
2522
2520
  }
2521
+ var xaiResponsesSupportedUrls = {
2522
+ "image/*": [/^https?:\/\/.*$/],
2523
+ // xAI's Responses API accepts non-image documents (PDF, plain text, CSV, etc.) as
2524
+ // `{ type: 'input_file', file_url }`. Keeping these URLs intact here lets them pass
2525
+ // through to the converter instead of being downloaded to bytes by the SDK.
2526
+ "application/pdf": [/^https?:\/\/.*$/],
2527
+ "text/*": [/^https?:\/\/.*$/]
2528
+ };
2523
2529
  var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
2524
2530
  constructor(modelId, config) {
2525
2531
  this.specificationVersion = "v4";
2526
- this.supportedUrls = {
2527
- "image/*": [/^https?:\/\/.*$/],
2528
- // xAI's Responses API accepts non-image documents (PDF, plain text, CSV, etc.) as
2529
- // `{ type: 'input_file', file_url }`. Keeping these URLs intact here lets them pass
2530
- // through to the converter instead of being downloaded to bytes by the SDK.
2531
- "application/pdf": [/^https?:\/\/.*$/],
2532
- "text/*": [/^https?:\/\/.*$/]
2533
- };
2532
+ this.supportedUrls = xaiResponsesSupportedUrls;
2534
2533
  this.modelId = modelId;
2535
2534
  this.config = config;
2536
2535
  }
@@ -2546,21 +2545,24 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
2546
2545
  get provider() {
2547
2546
  return this.config.provider;
2548
2547
  }
2549
- async getArgs({
2550
- prompt,
2551
- maxOutputTokens,
2552
- temperature,
2553
- topP,
2554
- topK,
2555
- frequencyPenalty,
2556
- presencePenalty,
2557
- stopSequences,
2558
- seed,
2559
- responseFormat,
2560
- providerOptions,
2561
- tools,
2562
- toolChoice,
2563
- reasoning
2548
+ static async prepareRequest({
2549
+ modelId,
2550
+ options: {
2551
+ prompt,
2552
+ maxOutputTokens,
2553
+ temperature,
2554
+ topP,
2555
+ topK,
2556
+ frequencyPenalty,
2557
+ presencePenalty,
2558
+ stopSequences,
2559
+ seed,
2560
+ responseFormat,
2561
+ providerOptions,
2562
+ tools,
2563
+ toolChoice,
2564
+ reasoning
2565
+ }
2564
2566
  }) {
2565
2567
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2566
2568
  const warnings = [];
@@ -2623,7 +2625,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
2623
2625
  }
2624
2626
  let resolvedReasoningEffort = options.reasoningEffort;
2625
2627
  if (resolvedReasoningEffort == null && isCustomReasoning2(reasoning)) {
2626
- if (!supportsReasoningEffort(this.modelId)) {
2628
+ if (!supportsReasoningEffort(modelId)) {
2627
2629
  warnings.push({
2628
2630
  type: "unsupported",
2629
2631
  feature: "reasoning",
@@ -2639,14 +2641,14 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
2639
2641
  low: "low",
2640
2642
  medium: "medium",
2641
2643
  high: "high",
2642
- xhigh: this.modelId === "grok-4.6" ? "xhigh" : "high"
2644
+ xhigh: modelId === "grok-4.6" ? "xhigh" : "high"
2643
2645
  },
2644
2646
  warnings
2645
2647
  });
2646
2648
  }
2647
2649
  }
2648
2650
  const baseArgs = {
2649
- model: this.modelId,
2651
+ model: modelId,
2650
2652
  input,
2651
2653
  logprobs: options.logprobs === true || options.topLogprobs != null ? true : void 0,
2652
2654
  top_logprobs: options.topLogprobs,
@@ -2705,6 +2707,12 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
2705
2707
  imageGenerationToolName
2706
2708
  };
2707
2709
  }
2710
+ getArgs(options) {
2711
+ return _XaiResponsesLanguageModel.prepareRequest({
2712
+ modelId: this.modelId,
2713
+ options
2714
+ });
2715
+ }
2708
2716
  async doGenerate(options) {
2709
2717
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
2710
2718
  const {
@@ -3507,7 +3515,7 @@ function mapWebSearchAction(action) {
3507
3515
  }
3508
3516
  }
3509
3517
 
3510
- // src/responses/xai-responses-batch.ts
3518
+ // src/xai-batch.ts
3511
3519
  var xaiBatchEndpoint = "/v1/responses";
3512
3520
  var xaiBatchName = "ai-sdk-text-batch";
3513
3521
  var xaiBatchResultsPageSize = 1e3;
@@ -3564,11 +3572,14 @@ var xaiBatchResultsPageSchema = lazySchema7(
3564
3572
  })
3565
3573
  )
3566
3574
  );
3567
- var XaiResponsesBatch = class {
3575
+ var XaiBatch = class {
3568
3576
  constructor(options) {
3569
3577
  this.options = options;
3578
+ this.specificationVersion = "v4";
3579
+ this.supportedUrls = xaiResponsesSupportedUrls;
3580
+ this.provider = options.provider;
3570
3581
  }
3571
- async startBatch(options) {
3582
+ async doStartBatch(options) {
3572
3583
  var _a, _b;
3573
3584
  const fileParts = [];
3574
3585
  const warnings = options.webhookUrl == null ? [] : [
@@ -3586,7 +3597,7 @@ var XaiResponsesBatch = class {
3586
3597
  schema: xaiBatchProviderOptionsSchema
3587
3598
  });
3588
3599
  for (const request of options.requests) {
3589
- const preparedRequest = await this.options.prepareRequest(request);
3600
+ const preparedRequest = await this.prepareRequest(request);
3590
3601
  fileParts.push(
3591
3602
  JSON.stringify({
3592
3603
  custom_id: request.id,
@@ -3656,10 +3667,10 @@ var XaiResponsesBatch = class {
3656
3667
  warnings
3657
3668
  };
3658
3669
  }
3659
- async getBatchStatus(options) {
3670
+ async doGetBatchStatus(options) {
3660
3671
  return convertXaiBatchStatus(await this.retrieveBatch(options));
3661
3672
  }
3662
- async getBatchResults(options) {
3673
+ async doGetBatchResults(options) {
3663
3674
  const batch = await this.retrieveBatch(options);
3664
3675
  if (convertXaiBatchStatus(batch).status === "pending") {
3665
3676
  throw new InvalidArgumentError({
@@ -3724,6 +3735,7 @@ var XaiResponsesBatch = class {
3724
3735
  if (((_c = (_b = result.error_message) == null ? void 0 : _b.length) != null ? _c : 0) > 0 || (error == null ? void 0 : error.code) != null && error.code !== 0 && error.code !== "0" || (error == null ? void 0 : error.code) == null && ((_e = (_d = error == null ? void 0 : error.message) == null ? void 0 : _d.length) != null ? _e : 0) > 0) {
3725
3736
  const convertedError = convertXaiBatchError(result);
3726
3737
  return {
3738
+ type: "text",
3727
3739
  id: result.batch_request_id,
3728
3740
  status: isXaiCancellationError(error == null ? void 0 : error.code) ? "cancelled" : "failed",
3729
3741
  error: convertedError
@@ -3740,10 +3752,12 @@ var XaiResponsesBatch = class {
3740
3752
  }
3741
3753
  const conversion = convertXaiChatBatchResponse(validation.value);
3742
3754
  return conversion.success ? {
3755
+ type: "text",
3743
3756
  id: result.batch_request_id,
3744
3757
  status: "succeeded",
3745
3758
  result: conversion.result
3746
3759
  } : {
3760
+ type: "text",
3747
3761
  id: result.batch_request_id,
3748
3762
  status: "failed",
3749
3763
  error: conversion.error
@@ -3751,38 +3765,18 @@ var XaiResponsesBatch = class {
3751
3765
  }
3752
3766
  return invalidXaiBatchResult(result.batch_request_id);
3753
3767
  }
3768
+ async prepareRequest(request) {
3769
+ const { args: body, warnings } = await XaiResponsesLanguageModel.prepareRequest({
3770
+ modelId: request.modelId,
3771
+ options: request.options
3772
+ });
3773
+ return { body, warnings };
3774
+ }
3754
3775
  getUrl(path) {
3755
3776
  var _a;
3756
3777
  return `${(_a = this.options.config.baseURL) != null ? _a : "https://api.x.ai/v1"}${path}`;
3757
3778
  }
3758
3779
  };
3759
- var XaiResponsesBatchLanguageModel = class _XaiResponsesBatchLanguageModel extends XaiResponsesLanguageModel {
3760
- static [WORKFLOW_SERIALIZE4](model) {
3761
- return XaiResponsesLanguageModel[WORKFLOW_SERIALIZE4](model);
3762
- }
3763
- static [WORKFLOW_DESERIALIZE4](options) {
3764
- return new _XaiResponsesBatchLanguageModel(options.modelId, options.config);
3765
- }
3766
- constructor(modelId, config) {
3767
- super(modelId, config);
3768
- this.batch = new XaiResponsesBatch({
3769
- config,
3770
- prepareRequest: async (request) => {
3771
- const { args: body, warnings } = await this.getArgs(request.options);
3772
- return { body, warnings };
3773
- }
3774
- });
3775
- }
3776
- experimental_doStartBatch(options) {
3777
- return this.batch.startBatch(options);
3778
- }
3779
- experimental_doGetBatchStatus(options) {
3780
- return this.batch.getBatchStatus(options);
3781
- }
3782
- experimental_doGetBatchResults(options) {
3783
- return this.batch.getBatchResults(options);
3784
- }
3785
- };
3786
3780
  function convertXaiBatchStatus(batch) {
3787
3781
  var _a, _b, _c, _d, _e, _f;
3788
3782
  const requestCounts = normalizeBatchRequestCounts({
@@ -3833,6 +3827,7 @@ function isXaiCancellationError(code) {
3833
3827
  }
3834
3828
  function invalidXaiBatchResult(id) {
3835
3829
  return {
3830
+ type: "text",
3836
3831
  id,
3837
3832
  status: "failed",
3838
3833
  error: {
@@ -4363,7 +4358,7 @@ var xaiTools = {
4363
4358
  };
4364
4359
 
4365
4360
  // src/version.ts
4366
- var VERSION = true ? "4.0.54" : "0.0.0-test";
4361
+ var VERSION = true ? "4.0.56" : "0.0.0-test";
4367
4362
 
4368
4363
  // src/files/xai-files.ts
4369
4364
  import {
@@ -5164,8 +5159,8 @@ import {
5164
5159
  postJsonToApi as postJsonToApi6,
5165
5160
  resolve,
5166
5161
  serializeModelOptions as serializeModelOptions4,
5167
- WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE5,
5168
- WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE5
5162
+ WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE4,
5163
+ WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE4
5169
5164
  } from "@ai-sdk/provider-utils";
5170
5165
  import { z as z23 } from "zod/v4";
5171
5166
 
@@ -5230,13 +5225,13 @@ var XaiSpeechModel = class _XaiSpeechModel {
5230
5225
  this.config = config;
5231
5226
  this.specificationVersion = "v4";
5232
5227
  }
5233
- static [WORKFLOW_SERIALIZE5](model) {
5228
+ static [WORKFLOW_SERIALIZE4](model) {
5234
5229
  return serializeModelOptions4({
5235
5230
  modelId: model.modelId,
5236
5231
  config: model.config
5237
5232
  });
5238
5233
  }
5239
- static [WORKFLOW_DESERIALIZE5](options) {
5234
+ static [WORKFLOW_DESERIALIZE4](options) {
5240
5235
  return new _XaiSpeechModel(options.modelId, options.config);
5241
5236
  }
5242
5237
  get provider() {
@@ -5387,8 +5382,8 @@ import {
5387
5382
  serializeModelOptions as serializeModelOptions5,
5388
5383
  toWebSocketUrl,
5389
5384
  waitForWebSocketBufferDrain,
5390
- WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE6,
5391
- WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE6
5385
+ WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE5,
5386
+ WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE5
5392
5387
  } from "@ai-sdk/provider-utils";
5393
5388
  import { z as z25 } from "zod/v4";
5394
5389
 
@@ -5476,13 +5471,13 @@ var XaiTranscriptionModel = class _XaiTranscriptionModel {
5476
5471
  this.config = config;
5477
5472
  this.specificationVersion = "v4";
5478
5473
  }
5479
- static [WORKFLOW_SERIALIZE6](model) {
5474
+ static [WORKFLOW_SERIALIZE5](model) {
5480
5475
  return serializeModelOptions5({
5481
5476
  modelId: model.modelId,
5482
5477
  config: model.config
5483
5478
  });
5484
5479
  }
5485
- static [WORKFLOW_DESERIALIZE6](options) {
5480
+ static [WORKFLOW_DESERIALIZE5](options) {
5486
5481
  return new _XaiTranscriptionModel(options.modelId, options.config);
5487
5482
  }
5488
5483
  get provider() {
@@ -5902,7 +5897,7 @@ function createXai(options = {}) {
5902
5897
  });
5903
5898
  };
5904
5899
  const createResponsesLanguageModel = (modelId) => {
5905
- return new XaiResponsesBatchLanguageModel(modelId, {
5900
+ return new XaiResponsesLanguageModel(modelId, {
5906
5901
  provider: "xai.responses",
5907
5902
  baseURL,
5908
5903
  headers: getHeaders,
@@ -5974,6 +5969,16 @@ function createXai(options = {}) {
5974
5969
  headers: getHeaders,
5975
5970
  fetch: options.fetch
5976
5971
  });
5972
+ const createBatch = () => new XaiBatch({
5973
+ provider: "xai.batch",
5974
+ config: {
5975
+ provider: "xai.responses",
5976
+ baseURL,
5977
+ headers: getHeaders,
5978
+ generateId,
5979
+ fetch: options.fetch
5980
+ }
5981
+ });
5977
5982
  const provider = (modelId) => createResponsesLanguageModel(modelId);
5978
5983
  provider.specificationVersion = "v4";
5979
5984
  provider.languageModel = createResponsesLanguageModel;
@@ -5993,6 +5998,7 @@ function createXai(options = {}) {
5993
5998
  provider.transcriptionModel = createTranscriptionModel;
5994
5999
  provider.transcription = createTranscriptionModel;
5995
6000
  provider.files = createFiles;
6001
+ provider.experimental_batch = createBatch;
5996
6002
  provider.tools = xaiTools;
5997
6003
  return provider;
5998
6004
  }