@ai-sdk/openai 4.0.63 → 4.0.65

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/openai",
3
- "version": "4.0.63",
3
+ "version": "4.0.65",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -35,8 +35,8 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@ai-sdk/provider": "4.0.11",
39
- "@ai-sdk/provider-utils": "5.0.37"
38
+ "@ai-sdk/provider": "4.0.13",
39
+ "@ai-sdk/provider-utils": "5.0.39"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ai-sdk/test-server": "2.0.1",
@@ -1,13 +1,14 @@
1
- import type {
2
- LanguageModelV4,
3
- LanguageModelV4CallOptions,
4
- LanguageModelV4Content,
5
- LanguageModelV4FinishReason,
6
- LanguageModelV4GenerateResult,
7
- LanguageModelV4StreamPart,
8
- LanguageModelV4StreamResult,
9
- SharedV4ProviderMetadata,
10
- SharedV4Warning,
1
+ import {
2
+ InvalidResponseDataError,
3
+ type LanguageModelV4,
4
+ type LanguageModelV4CallOptions,
5
+ type LanguageModelV4Content,
6
+ type LanguageModelV4FinishReason,
7
+ type LanguageModelV4GenerateResult,
8
+ type LanguageModelV4StreamPart,
9
+ type LanguageModelV4StreamResult,
10
+ type SharedV4ProviderMetadata,
11
+ type SharedV4Warning,
11
12
  } from '@ai-sdk/provider';
12
13
  import {
13
14
  StreamingToolCallTracker,
@@ -396,6 +397,13 @@ export class OpenAIChatLanguageModel implements LanguageModelV4 {
396
397
  });
397
398
 
398
399
  const choice = response.choices[0];
400
+ if (choice == null) {
401
+ throw new InvalidResponseDataError({
402
+ data: rawResponse,
403
+ message: 'Response did not contain any choices.',
404
+ });
405
+ }
406
+
399
407
  const content: Array<LanguageModelV4Content> = [];
400
408
 
401
409
  // text content:
@@ -2,10 +2,13 @@ import {
2
2
  InvalidArgumentError,
3
3
  InvalidResponseDataError,
4
4
  type Experimental_BatchV4 as BatchV4,
5
+ type Experimental_BatchV4CancelResult as BatchV4CancelResult,
5
6
  type Experimental_BatchV4StartResult as BatchV4StartResult,
6
7
  type Experimental_BatchV4Error as BatchV4Error,
7
8
  type Experimental_BatchV4ItemResult as BatchV4ItemResult,
8
9
  type Experimental_BatchV4OperationOptions as BatchV4OperationOptions,
10
+ type Experimental_BatchV4ListOptions as BatchV4ListOptions,
11
+ type Experimental_BatchV4ListResult as BatchV4ListResult,
9
12
  type Experimental_BatchV4Status as BatchV4Status,
10
13
  type Experimental_TextBatchV4ItemResult as TextBatchV4ItemResult,
11
14
  type Experimental_BatchV4StartOptions as BatchV4StartOptions,
@@ -86,36 +89,37 @@ type OpenAIBatchResultConversion =
86
89
  | { success: true; result: LanguageModelV4GenerateResult }
87
90
  | { success: false; error: BatchV4Error };
88
91
 
92
+ const openaiBatchResponseZodSchema = () =>
93
+ z.object({
94
+ id: z.string(),
95
+ status: z.string(),
96
+ output_file_id: z.string().nullish(),
97
+ error_file_id: z.string().nullish(),
98
+ created_at: z.number().nullish(),
99
+ expires_at: z.number().nullish(),
100
+ request_counts: z
101
+ .object({
102
+ total: z.number().nullish(),
103
+ completed: z.number().nullish(),
104
+ failed: z.number().nullish(),
105
+ })
106
+ .nullish(),
107
+ errors: z
108
+ .object({
109
+ data: z
110
+ .array(
111
+ z.object({
112
+ code: z.string().nullish(),
113
+ message: z.string().nullish(),
114
+ }),
115
+ )
116
+ .nullish(),
117
+ })
118
+ .nullish(),
119
+ });
120
+
89
121
  const openaiBatchResponseSchema = lazySchema(() =>
90
- zodSchema(
91
- z.object({
92
- id: z.string(),
93
- status: z.string(),
94
- output_file_id: z.string().nullish(),
95
- error_file_id: z.string().nullish(),
96
- created_at: z.number().nullish(),
97
- expires_at: z.number().nullish(),
98
- request_counts: z
99
- .object({
100
- total: z.number().nullish(),
101
- completed: z.number().nullish(),
102
- failed: z.number().nullish(),
103
- })
104
- .nullish(),
105
- errors: z
106
- .object({
107
- data: z
108
- .array(
109
- z.object({
110
- code: z.string().nullish(),
111
- message: z.string().nullish(),
112
- }),
113
- )
114
- .nullish(),
115
- })
116
- .nullish(),
117
- }),
118
- ),
122
+ zodSchema(openaiBatchResponseZodSchema()),
119
123
  );
120
124
 
121
125
  type OpenAIBatchResponse = InferSchema<typeof openaiBatchResponseSchema>;
@@ -141,6 +145,16 @@ const openaiBatchResultLineSchema = lazySchema(() =>
141
145
  ),
142
146
  );
143
147
 
148
+ const openaiBatchListResponseSchema = lazySchema(() =>
149
+ zodSchema(
150
+ z.object({
151
+ data: z.array(openaiBatchResponseZodSchema()),
152
+ has_more: z.boolean(),
153
+ last_id: z.string().nullish(),
154
+ }),
155
+ ),
156
+ );
157
+
144
158
  type OpenAIBatchResultLine = InferSchema<typeof openaiBatchResultLineSchema>;
145
159
 
146
160
  export class OpenAIBatch implements BatchV4<OpenAIBatchModelIds> {
@@ -319,6 +333,58 @@ export class OpenAIBatch implements BatchV4<OpenAIBatchModelIds> {
319
333
  return convertOpenAIBatchStatus(batch);
320
334
  }
321
335
 
336
+ async doCancelBatch(
337
+ options: BatchV4OperationOptions,
338
+ ): Promise<BatchV4CancelResult> {
339
+ await postJsonToApi({
340
+ url: this.getUrl(
341
+ `/batches/${encodeURIComponent(options.batchId)}/cancel`,
342
+ ),
343
+ headers: combineHeaders(this.options.config.headers?.(), options.headers),
344
+ body: {},
345
+ failedResponseHandler: openaiFailedResponseHandler,
346
+ successfulResponseHandler: createJsonResponseHandler(
347
+ openaiBatchResponseSchema,
348
+ ),
349
+ abortSignal: options.abortSignal,
350
+ fetch: this.options.config.fetch,
351
+ });
352
+
353
+ return {};
354
+ }
355
+
356
+ async doListBatches(options: BatchV4ListOptions): Promise<BatchV4ListResult> {
357
+ const url = new URL(this.getUrl('/batches'));
358
+ if (options.limit != null) {
359
+ url.searchParams.set('limit', String(options.limit));
360
+ }
361
+ if (options.cursor != null) {
362
+ url.searchParams.set('after', options.cursor);
363
+ }
364
+
365
+ const { value: page } = await getFromApi({
366
+ url: url.toString(),
367
+ headers: combineHeaders(this.options.config.headers?.(), options.headers),
368
+ failedResponseHandler: openaiFailedResponseHandler,
369
+ successfulResponseHandler: createJsonResponseHandler(
370
+ openaiBatchListResponseSchema,
371
+ ),
372
+ abortSignal: options.abortSignal,
373
+ fetch: this.options.config.fetch,
374
+ validateUrl: false,
375
+ });
376
+
377
+ return {
378
+ batches: page.data.map(batch => ({
379
+ batchId: batch.id,
380
+ ...convertOpenAIBatchStatus(batch),
381
+ })),
382
+ ...(page.has_more && page.last_id != null
383
+ ? { nextCursor: page.last_id }
384
+ : {}),
385
+ };
386
+ }
387
+
322
388
  async doGetBatchResults(
323
389
  options: BatchV4OperationOptions,
324
390
  ): Promise<ReadableStream<BatchV4ItemResult>> {