@ai-sdk/openai 4.0.0-beta.42 → 4.0.0-beta.44

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.
@@ -2442,6 +2442,9 @@ const { images } = await generateImage({
2442
2442
  Remove the background from an image by setting `background` to `transparent`:
2443
2443
 
2444
2444
  ```ts
2445
+ import { openai, type OpenAIImageModelEditOptions } from '@ai-sdk/openai';
2446
+ import { generateImage } from 'ai';
2447
+
2445
2448
  const imageBuffer = readFileSync('./input-image.png');
2446
2449
 
2447
2450
  const { images } = await generateImage({
@@ -2453,8 +2456,8 @@ const { images } = await generateImage({
2453
2456
  providerOptions: {
2454
2457
  openai: {
2455
2458
  background: 'transparent',
2456
- output_format: 'png',
2457
- },
2459
+ outputFormat: 'png',
2460
+ } satisfies OpenAIImageModelEditOptions,
2458
2461
  },
2459
2462
  });
2460
2463
  ```
@@ -2497,11 +2500,17 @@ const { images } = await generateImage({
2497
2500
  You can pass optional `providerOptions` to the image model. These are prone to change by OpenAI and are model dependent. For example, the `gpt-image-1` model supports the `quality` option:
2498
2501
 
2499
2502
  ```ts
2503
+ import {
2504
+ openai,
2505
+ type OpenAIImageModelGenerationOptions,
2506
+ } from '@ai-sdk/openai';
2507
+ import { generateImage } from 'ai';
2508
+
2500
2509
  const { image, providerMetadata } = await generateImage({
2501
2510
  model: openai.image('gpt-image-1.5'),
2502
2511
  prompt: 'A salamander at sunrise in a forest pond in the Seychelles.',
2503
2512
  providerOptions: {
2504
- openai: { quality: 'high' },
2513
+ openai: { quality: 'high' } satisfies OpenAIImageModelGenerationOptions,
2505
2514
  },
2506
2515
  });
2507
2516
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/openai",
3
- "version": "4.0.0-beta.42",
3
+ "version": "4.0.0-beta.44",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -0,0 +1,123 @@
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type InferSchema,
5
+ } from '@ai-sdk/provider-utils';
6
+ import { z } from 'zod/v4';
7
+
8
+ export type OpenAIImageModelId =
9
+ | 'dall-e-3'
10
+ | 'dall-e-2'
11
+ | 'gpt-image-1'
12
+ | 'gpt-image-1-mini'
13
+ | 'gpt-image-1.5'
14
+ | 'gpt-image-2'
15
+ | 'chatgpt-image-latest'
16
+ | (string & {});
17
+
18
+ // https://platform.openai.com/docs/guides/images
19
+ export const modelMaxImagesPerCall: Record<OpenAIImageModelId, number> = {
20
+ 'dall-e-3': 1,
21
+ 'dall-e-2': 10,
22
+ 'gpt-image-1': 10,
23
+ 'gpt-image-1-mini': 10,
24
+ 'gpt-image-1.5': 10,
25
+ 'gpt-image-2': 10,
26
+ 'chatgpt-image-latest': 10,
27
+ };
28
+
29
+ const defaultResponseFormatPrefixes = [
30
+ 'chatgpt-image-',
31
+ 'gpt-image-1-mini',
32
+ 'gpt-image-1.5',
33
+ 'gpt-image-1',
34
+ 'gpt-image-2',
35
+ ];
36
+
37
+ export function hasDefaultResponseFormat(modelId: string): boolean {
38
+ return defaultResponseFormatPrefixes.some(prefix =>
39
+ modelId.startsWith(prefix),
40
+ );
41
+ }
42
+
43
+ const baseImageModelOptionsObject = z.object({
44
+ /**
45
+ * Quality of the generated image(s).
46
+ *
47
+ * Valid values: `standard`, `hd`, `low`, `medium`, `high`, `auto`.
48
+ */
49
+ quality: z
50
+ .enum(['standard', 'hd', 'low', 'medium', 'high', 'auto'])
51
+ .optional(),
52
+
53
+ /**
54
+ * Background behavior for the generated image(s).
55
+ *
56
+ * If `transparent`, the output format must support transparency
57
+ * (i.e. `png` or `webp`).
58
+ */
59
+ background: z.enum(['transparent', 'opaque', 'auto']).optional(),
60
+
61
+ /**
62
+ * Format in which the generated image(s) are returned.
63
+ */
64
+ outputFormat: z.enum(['png', 'jpeg', 'webp']).optional(),
65
+
66
+ /**
67
+ * Compression level (0-100) for the generated image(s). Applies to the
68
+ * `jpeg` and `webp` output formats.
69
+ */
70
+ outputCompression: z.number().int().min(0).max(100).optional(),
71
+
72
+ /**
73
+ * A unique identifier representing your end-user, which can help OpenAI
74
+ * to monitor and detect abuse.
75
+ */
76
+ user: z.string().optional(),
77
+ });
78
+
79
+ export const openaiImageModelOptions = lazySchema(() =>
80
+ zodSchema(baseImageModelOptionsObject),
81
+ );
82
+
83
+ export type OpenAIImageModelOptions = InferSchema<
84
+ typeof openaiImageModelOptions
85
+ >;
86
+
87
+ export const openaiImageModelGenerationOptions = lazySchema(() =>
88
+ zodSchema(
89
+ baseImageModelOptionsObject.extend({
90
+ /**
91
+ * Style of the generated image. `vivid` produces hyper-real and
92
+ * dramatic images; `natural` produces more subdued, less hyper-real
93
+ * looking images.
94
+ */
95
+ style: z.enum(['vivid', 'natural']).optional(),
96
+
97
+ /**
98
+ * Content moderation level for the generated image(s). `low` applies
99
+ * less restrictive filtering.
100
+ */
101
+ moderation: z.enum(['auto', 'low']).optional(),
102
+ }),
103
+ ),
104
+ );
105
+
106
+ export type OpenAIImageModelGenerationOptions = InferSchema<
107
+ typeof openaiImageModelGenerationOptions
108
+ >;
109
+
110
+ export const openaiImageModelEditOptions = lazySchema(() =>
111
+ zodSchema(
112
+ baseImageModelOptionsObject.extend({
113
+ /**
114
+ * Fidelity of the output image(s) to the input image(s).
115
+ */
116
+ inputFidelity: z.enum(['high', 'low']).optional(),
117
+ }),
118
+ ),
119
+ );
120
+
121
+ export type OpenAIImageModelEditOptions = InferSchema<
122
+ typeof openaiImageModelEditOptions
123
+ >;
@@ -9,6 +9,7 @@ import {
9
9
  convertToFormData,
10
10
  createJsonResponseHandler,
11
11
  downloadBlob,
12
+ parseProviderOptions,
12
13
  postFormDataToApi,
13
14
  postJsonToApi,
14
15
  serializeModelOptions,
@@ -21,8 +22,11 @@ import { openaiImageResponseSchema } from './openai-image-api';
21
22
  import {
22
23
  hasDefaultResponseFormat,
23
24
  modelMaxImagesPerCall,
25
+ openaiImageModelEditOptions,
26
+ openaiImageModelGenerationOptions,
27
+ type OpenAIImageModelEditOptions,
24
28
  type OpenAIImageModelId,
25
- } from './openai-image-options';
29
+ } from './openai-image-model-options';
26
30
  interface OpenAIImageModelConfig extends OpenAIConfig {
27
31
  _internal?: {
28
32
  currentDate?: () => Date;
@@ -91,6 +95,13 @@ export class OpenAIImageModel implements ImageModelV4 {
91
95
  const currentDate = this.config._internal?.currentDate?.() ?? new Date();
92
96
 
93
97
  if (files != null) {
98
+ const openaiOptions =
99
+ (await parseProviderOptions({
100
+ provider: 'openai',
101
+ providerOptions,
102
+ schema: openaiImageModelEditOptions,
103
+ })) ?? {};
104
+
94
105
  const { value: response, responseHeaders } = await postFormDataToApi({
95
106
  url: this.config.url({
96
107
  path: '/images/edits',
@@ -121,7 +132,12 @@ export class OpenAIImageModel implements ImageModelV4 {
121
132
  mask: mask != null ? await fileToBlob(mask) : undefined,
122
133
  n,
123
134
  size,
124
- ...(providerOptions.openai ?? {}),
135
+ quality: openaiOptions.quality,
136
+ background: openaiOptions.background,
137
+ output_format: openaiOptions.outputFormat,
138
+ output_compression: openaiOptions.outputCompression,
139
+ input_fidelity: openaiOptions.inputFidelity,
140
+ user: openaiOptions.user,
125
141
  }),
126
142
  failedResponseHandler: openaiFailedResponseHandler,
127
143
  successfulResponseHandler: createJsonResponseHandler(
@@ -169,6 +185,13 @@ export class OpenAIImageModel implements ImageModelV4 {
169
185
  };
170
186
  }
171
187
 
188
+ const openaiOptions =
189
+ (await parseProviderOptions({
190
+ provider: 'openai',
191
+ providerOptions,
192
+ schema: openaiImageModelGenerationOptions,
193
+ })) ?? {};
194
+
172
195
  const { value: response, responseHeaders } = await postJsonToApi({
173
196
  url: this.config.url({
174
197
  path: '/images/generations',
@@ -180,7 +203,13 @@ export class OpenAIImageModel implements ImageModelV4 {
180
203
  prompt,
181
204
  n,
182
205
  size,
183
- ...(providerOptions.openai ?? {}),
206
+ quality: openaiOptions.quality,
207
+ style: openaiOptions.style,
208
+ background: openaiOptions.background,
209
+ moderation: openaiOptions.moderation,
210
+ output_format: openaiOptions.outputFormat,
211
+ output_compression: openaiOptions.outputCompression,
212
+ user: openaiOptions.user,
184
213
  ...(!hasDefaultResponseFormat(this.modelId)
185
214
  ? { response_format: 'b64_json' }
186
215
  : {}),
@@ -267,84 +296,18 @@ function distributeTokenDetails(
267
296
  }
268
297
 
269
298
  type OpenAIImageEditInput = {
270
- /**
271
- * Allows to set transparency for the background of the generated image(s).
272
- * This parameter is only supported for `gpt-image-1`. Must be one of
273
- * `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
274
- * model will automatically determine the best background for the image.
275
- *
276
- * If `transparent`, the output format needs to support transparency, so it
277
- * should be set to either `png` (default value) or `webp`.
278
- *
279
- */
280
- background?: 'transparent' | 'opaque' | 'auto';
281
- /**
282
- * The image(s) to edit. Must be a supported image file or an array of images.
283
- *
284
- * For `gpt-image-1`, each image should be a `png`, `webp`, or `jpg` file less
285
- * than 50MB. You can provide up to 16 images.
286
- *
287
- * For `dall-e-2`, you can only provide one image, and it should be a square
288
- * `png` file less than 4MB.
289
- *
290
- */
299
+ model: OpenAIImageModelId;
300
+ prompt?: string;
291
301
  image: Blob | Blob[];
292
- input_fidelity?: ('high' | 'low') | null;
293
- /**
294
- * An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where `image` should be edited. If there are multiple images provided, the mask will be applied on the first image. Must be a valid PNG file, less than 4MB, and have the same dimensions as `image`.
295
- */
296
302
  mask?: Blob;
297
- /**
298
- * The model to use for image generation. Only `dall-e-2` and `gpt-image-1` are supported. Defaults to `dall-e-2` unless a parameter specific to `gpt-image-1` is used.
299
- */
300
- model?: 'dall-e-2' | 'gpt-image-1' | 'gpt-image-1-mini' | (string & {});
301
- /**
302
- * The number of images to generate. Must be between 1 and 10.
303
- */
304
303
  n?: number;
305
- /**
306
- * The compression level (0-100%) for the generated images. This parameter
307
- * is only supported for `gpt-image-1` with the `webp` or `jpeg` output
308
- * formats, and defaults to 100.
309
- *
310
- */
311
- output_compression?: number;
312
- /**
313
- * The format in which the generated images are returned. This parameter is
314
- * only supported for `gpt-image-1`. Must be one of `png`, `jpeg`, or `webp`.
315
- * The default value is `png`.
316
- *
317
- */
318
- output_format?: 'png' | 'jpeg' | 'webp';
319
- partial_images?: number | null;
320
- /**
321
- * A text description of the desired image(s). The maximum length is 1000 characters for `dall-e-2`, and 32000 characters for `gpt-image-1`.
322
- */
323
- prompt?: string;
324
- /**
325
- * The quality of the image that will be generated. `high`, `medium` and `low` are only supported for `gpt-image-1`. `dall-e-2` only supports `standard` quality. Defaults to `auto`.
326
- *
327
- */
328
- quality?: 'standard' | 'low' | 'medium' | 'high' | 'auto';
329
- /**
330
- * The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated. This parameter is only supported for `dall-e-2`, as `gpt-image-1` will always return base64-encoded images.
331
- */
332
- response_format?: 'url' | 'b64_json';
333
- /**
334
- * The size of the generated images. Must be one of `1024x1024`, `1536x1024` (landscape), `1024x1536` (portrait), or `auto` (default value) for `gpt-image-1`, and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
335
- */
336
304
  size?: `${number}x${number}`;
337
- /**
338
- * Edit the image in streaming mode. Defaults to `false`. See the
339
- * [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more information.
340
- *
341
- */
342
- stream?: boolean;
343
- /**
344
- * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
345
- *
346
- */
347
- user?: string;
305
+ quality?: OpenAIImageModelEditOptions['quality'];
306
+ background?: OpenAIImageModelEditOptions['background'];
307
+ output_format?: OpenAIImageModelEditOptions['outputFormat'];
308
+ output_compression?: OpenAIImageModelEditOptions['outputCompression'];
309
+ input_fidelity?: OpenAIImageModelEditOptions['inputFidelity'];
310
+ user?: OpenAIImageModelEditOptions['user'];
348
311
  };
349
312
 
350
313
  async function fileToBlob(
package/src/index.ts CHANGED
@@ -10,6 +10,11 @@ export type {
10
10
  /** @deprecated Use `OpenAILanguageModelChatOptions` instead. */
11
11
  OpenAILanguageModelChatOptions as OpenAIChatLanguageModelOptions,
12
12
  } from './chat/openai-chat-language-model-options';
13
+ export type {
14
+ OpenAIImageModelOptions,
15
+ OpenAIImageModelGenerationOptions,
16
+ OpenAIImageModelEditOptions,
17
+ } from './image/openai-image-model-options';
13
18
  export type { OpenAILanguageModelCompletionOptions } from './completion/openai-completion-language-model-options';
14
19
  export type { OpenAIEmbeddingModelOptions } from './embedding/openai-embedding-model-options';
15
20
  export type { OpenAISpeechModelOptions } from './speech/openai-speech-model-options';
@@ -5,7 +5,7 @@ export * from '../completion/openai-completion-language-model-options';
5
5
  export * from '../embedding/openai-embedding-model';
6
6
  export * from '../embedding/openai-embedding-model-options';
7
7
  export * from '../image/openai-image-model';
8
- export * from '../image/openai-image-options';
8
+ export * from '../image/openai-image-model-options';
9
9
  export * from '../transcription/openai-transcription-model';
10
10
  export * from '../transcription/openai-transcription-model-options';
11
11
  export * from '../speech/openai-speech-model';
@@ -23,7 +23,7 @@ import { OpenAIEmbeddingModel } from './embedding/openai-embedding-model';
23
23
  import { OpenAIFiles } from './files/openai-files';
24
24
  import type { OpenAIEmbeddingModelId } from './embedding/openai-embedding-model-options';
25
25
  import { OpenAIImageModel } from './image/openai-image-model';
26
- import type { OpenAIImageModelId } from './image/openai-image-options';
26
+ import type { OpenAIImageModelId } from './image/openai-image-model-options';
27
27
  import { openaiTools } from './openai-tools';
28
28
  import { OpenAIResponsesLanguageModel } from './responses/openai-responses-language-model';
29
29
  import type { OpenAIResponsesModelId } from './responses/openai-responses-language-model-options';
@@ -562,6 +562,7 @@ export const openaiResponsesChunkSchema = lazySchema(() =>
562
562
  call_id: z.string(),
563
563
  name: z.string(),
564
564
  arguments: z.string(),
565
+ namespace: z.string().nullish(),
565
566
  }),
566
567
  z.object({
567
568
  type: z.literal('web_search_call'),
@@ -711,6 +712,7 @@ export const openaiResponsesChunkSchema = lazySchema(() =>
711
712
  name: z.string(),
712
713
  arguments: z.string(),
713
714
  status: z.literal('completed'),
715
+ namespace: z.string().nullish(),
714
716
  }),
715
717
  z.object({
716
718
  type: z.literal('custom_tool_call'),
@@ -1226,6 +1228,7 @@ export const openaiResponsesResponseSchema = lazySchema(() =>
1226
1228
  name: z.string(),
1227
1229
  arguments: z.string(),
1228
1230
  id: z.string(),
1231
+ namespace: z.string().nullish(),
1229
1232
  }),
1230
1233
  z.object({
1231
1234
  type: z.literal('custom_tool_call'),
@@ -803,6 +803,7 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
803
803
  providerMetadata: {
804
804
  [providerOptionsName]: {
805
805
  itemId: part.id,
806
+ ...(part.namespace != null && { namespace: part.namespace }),
806
807
  },
807
808
  },
808
809
  });
@@ -1426,6 +1427,13 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
1426
1427
  controller.enqueue({
1427
1428
  type: 'tool-input-end',
1428
1429
  id: value.item.call_id,
1430
+ ...(value.item.namespace != null && {
1431
+ providerMetadata: {
1432
+ [providerOptionsName]: {
1433
+ namespace: value.item.namespace,
1434
+ },
1435
+ },
1436
+ }),
1429
1437
  });
1430
1438
 
1431
1439
  controller.enqueue({
@@ -1436,6 +1444,9 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
1436
1444
  providerMetadata: {
1437
1445
  [providerOptionsName]: {
1438
1446
  itemId: value.item.id,
1447
+ ...(value.item.namespace != null && {
1448
+ namespace: value.item.namespace,
1449
+ }),
1439
1450
  },
1440
1451
  },
1441
1452
  });
@@ -1,34 +0,0 @@
1
- export type OpenAIImageModelId =
2
- | 'dall-e-3'
3
- | 'dall-e-2'
4
- | 'gpt-image-1'
5
- | 'gpt-image-1-mini'
6
- | 'gpt-image-1.5'
7
- | 'gpt-image-2'
8
- | 'chatgpt-image-latest'
9
- | (string & {});
10
-
11
- // https://platform.openai.com/docs/guides/images
12
- export const modelMaxImagesPerCall: Record<OpenAIImageModelId, number> = {
13
- 'dall-e-3': 1,
14
- 'dall-e-2': 10,
15
- 'gpt-image-1': 10,
16
- 'gpt-image-1-mini': 10,
17
- 'gpt-image-1.5': 10,
18
- 'gpt-image-2': 10,
19
- 'chatgpt-image-latest': 10,
20
- };
21
-
22
- const defaultResponseFormatPrefixes = [
23
- 'chatgpt-image-',
24
- 'gpt-image-1-mini',
25
- 'gpt-image-1.5',
26
- 'gpt-image-1',
27
- 'gpt-image-2',
28
- ];
29
-
30
- export function hasDefaultResponseFormat(modelId: string): boolean {
31
- return defaultResponseFormatPrefixes.some(prefix =>
32
- modelId.startsWith(prefix),
33
- );
34
- }