@ai-sdk/openai 3.0.129 → 3.0.131

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.
@@ -276,7 +276,7 @@ The following provider options are available:
276
276
  A cache key for manual prompt caching control. Used by OpenAI to cache responses for similar requests to optimize your cache hit rates.
277
277
 
278
278
  - **promptCacheOptions** _object_
279
- Configures prompt caching for GPT-5.6 and later models. `mode` can be `'implicit'` or `'explicit'`, and `ttl` currently only supports `'30m'`. In explicit mode, only content blocks marked with a prompt cache breakpoint are cached.
279
+ Configures prompt caching for GPT-5.6 and later models. `mode` can be `'implicit'` or `'explicit'`. `ttl` sets the minimum cache lifetime: `'30m'` (default) or `'12h'`. 12-hour writes are billed at a higher rate, and `usage.raw.input_tokens_details.cache_write_tokens_details` splits cache writes into `ttl_30m_tokens` and `ttl_12h_tokens`. Set `prewarm` to `true` to write the prompt to the cache without generating output. In explicit mode, only content blocks marked with a prompt cache breakpoint are cached.
280
280
 
281
281
  - **promptCacheRetention** _'in_memory' | '24h'_
282
282
  The legacy retention policy for models before GPT-5.6. Set to `'24h'` to enable extended prompt caching on supported models. For GPT-5.6 and later models, use `promptCacheOptions.ttl` instead.
@@ -2357,7 +2357,7 @@ including `gpt-4o` and `gpt-4o-mini`.
2357
2357
  not need to be explicitly enabled.
2358
2358
  - You can use the response `usage` to access the number of prompt tokens that were a cache hit via `usage.inputTokenDetails.cacheReadTokens`.
2359
2359
  - For GPT-5.6 and later models, `usage.inputTokenDetails.cacheWriteTokens` reports tokens written to the cache.
2360
- - For GPT-5.6 and later models, `promptCacheOptions.ttl` sets a minimum cache lifetime of 30 minutes. Earlier models generally retain in-memory prefixes for 5-10 minutes of inactivity, up to one hour.
2360
+ - For GPT-5.6 and later models, `promptCacheOptions.ttl` sets a minimum cache lifetime of 30 minutes. Chat models only support `'30m'`. The 12-hour lifetime is available on [Responses models](#responses-models). Earlier models generally retain in-memory prefixes for 5-10 minutes of inactivity, up to one hour.
2361
2361
 
2362
2362
  ```ts highlight="11"
2363
2363
  import { openai } from '@ai-sdk/openai';
@@ -2390,7 +2390,7 @@ const { text, usage } = await generateText({
2390
2390
  console.log('Cached tokens:', usage.inputTokenDetails.cacheReadTokens);
2391
2391
  ```
2392
2392
 
2393
- GPT-5.6 and later models support explicit cache breakpoints on text, image, and file content blocks. Set a request-level cache key and mark the end of each reusable prefix with `promptCacheBreakpoint`:
2393
+ GPT-5.6 and later models support explicit cache breakpoints on text, image, and file content blocks. Set a request-level cache key and mark the end of each reusable prefix with `promptCacheBreakpoint`. With Responses models, a breakpoint can set its own `ttl` (for example `{ mode: 'explicit', ttl: '12h' }`) to override the request-level `promptCacheOptions.ttl`:
2394
2394
 
2395
2395
  ```ts highlight="9-12,20-28"
2396
2396
  import {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/openai",
3
- "version": "3.0.129",
3
+ "version": "3.0.131",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@ai-sdk/provider": "3.0.19",
40
- "@ai-sdk/provider-utils": "4.0.60"
40
+ "@ai-sdk/provider-utils": "4.0.62"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "20.17.24",
@@ -199,7 +199,7 @@ export const openaiChatChunkSchema = lazySchema(() =>
199
199
  })
200
200
  .nullish(),
201
201
  }),
202
- openaiErrorDataSchema,
202
+ openaiErrorDataSchema(),
203
203
  ]),
204
204
  ),
205
205
  );
@@ -71,7 +71,7 @@ export const openaiCompletionChunkSchema = lazySchema(() =>
71
71
  })
72
72
  .nullish(),
73
73
  }),
74
- openaiErrorDataSchema,
74
+ openaiErrorDataSchema(),
75
75
  ]),
76
76
  ),
77
77
  );
@@ -49,45 +49,46 @@ export function getMaxImagesPerCall(modelId: OpenAIImageModelId): number {
49
49
  );
50
50
  }
51
51
 
52
- const baseImageModelOptionsObject = z.object({
53
- /**
54
- * Quality of the generated image(s).
55
- *
56
- * Valid values: `standard`, `hd`, `low`, `medium`, `high`, `xhigh`, `max`, `auto`.
57
- * `xhigh` and `max` are supported by GPT Image 2.5 models.
58
- */
59
- quality: z
60
- .enum(['standard', 'hd', 'low', 'medium', 'high', 'xhigh', 'max', 'auto'])
61
- .optional(),
62
-
63
- /**
64
- * Background behavior for the generated image(s).
65
- *
66
- * If `transparent`, the output format must support transparency
67
- * (i.e. `png` or `webp`).
68
- */
69
- background: z.enum(['transparent', 'opaque', 'auto']).optional(),
70
-
71
- /**
72
- * Format in which the generated image(s) are returned.
73
- */
74
- outputFormat: z.enum(['png', 'jpeg', 'webp']).optional(),
75
-
76
- /**
77
- * Compression level (0-100) for the generated image(s). Applies to the
78
- * `jpeg` and `webp` output formats.
79
- */
80
- outputCompression: z.number().int().min(0).max(100).optional(),
81
-
82
- /**
83
- * A unique identifier representing your end-user, which can help OpenAI
84
- * to monitor and detect abuse.
85
- */
86
- user: z.string().optional(),
87
- });
52
+ const baseImageModelOptionsObject = () =>
53
+ z.object({
54
+ /**
55
+ * Quality of the generated image(s).
56
+ *
57
+ * Valid values: `standard`, `hd`, `low`, `medium`, `high`, `xhigh`, `max`, `auto`.
58
+ * `xhigh` and `max` are supported by GPT Image 2.5 models.
59
+ */
60
+ quality: z
61
+ .enum(['standard', 'hd', 'low', 'medium', 'high', 'xhigh', 'max', 'auto'])
62
+ .optional(),
63
+
64
+ /**
65
+ * Background behavior for the generated image(s).
66
+ *
67
+ * If `transparent`, the output format must support transparency
68
+ * (i.e. `png` or `webp`).
69
+ */
70
+ background: z.enum(['transparent', 'opaque', 'auto']).optional(),
71
+
72
+ /**
73
+ * Format in which the generated image(s) are returned.
74
+ */
75
+ outputFormat: z.enum(['png', 'jpeg', 'webp']).optional(),
76
+
77
+ /**
78
+ * Compression level (0-100) for the generated image(s). Applies to the
79
+ * `jpeg` and `webp` output formats.
80
+ */
81
+ outputCompression: z.number().int().min(0).max(100).optional(),
82
+
83
+ /**
84
+ * A unique identifier representing your end-user, which can help OpenAI
85
+ * to monitor and detect abuse.
86
+ */
87
+ user: z.string().optional(),
88
+ });
88
89
 
89
90
  export const openaiImageModelOptions = lazySchema(() =>
90
- zodSchema(baseImageModelOptionsObject),
91
+ zodSchema(baseImageModelOptionsObject()),
91
92
  );
92
93
 
93
94
  export type OpenAIImageModelOptions = InferSchema<
@@ -96,7 +97,7 @@ export type OpenAIImageModelOptions = InferSchema<
96
97
 
97
98
  export const openaiImageModelGenerationOptions = lazySchema(() =>
98
99
  zodSchema(
99
- baseImageModelOptionsObject.extend({
100
+ baseImageModelOptionsObject().extend({
100
101
  /**
101
102
  * Style of the generated image. `vivid` produces hyper-real and
102
103
  * dramatic images; `natural` produces more subdued, less hyper-real
@@ -119,7 +120,7 @@ export type OpenAIImageModelGenerationOptions = InferSchema<
119
120
 
120
121
  export const openaiImageModelEditOptions = lazySchema(() =>
121
122
  zodSchema(
122
- baseImageModelOptionsObject.extend({
123
+ baseImageModelOptionsObject().extend({
123
124
  /**
124
125
  * Fidelity of the output image(s) to the input image(s).
125
126
  */
@@ -1,22 +1,30 @@
1
1
  import { z } from 'zod/v4';
2
- import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';
2
+ import {
3
+ lazySchema,
4
+ zodSchema,
5
+ createJsonErrorResponseHandler,
6
+ } from '@ai-sdk/provider-utils';
3
7
 
4
- export const openaiErrorDataSchema = z.object({
5
- error: z.object({
6
- message: z.string(),
8
+ export const openaiErrorDataSchema = () =>
9
+ z.object({
10
+ error: z.object({
11
+ message: z.string(),
7
12
 
8
- // The additional information below is handled loosely to support
9
- // OpenAI-compatible providers that have slightly different error
10
- // responses:
11
- type: z.string().nullish(),
12
- param: z.any().nullish(),
13
- code: z.union([z.string(), z.number()]).nullish(),
14
- }),
15
- });
13
+ // The additional information below is handled loosely to support
14
+ // OpenAI-compatible providers that have slightly different error
15
+ // responses:
16
+ type: z.string().nullish(),
17
+ param: z.any().nullish(),
18
+ code: z.union([z.string(), z.number()]).nullish(),
19
+ }),
20
+ });
21
+ export const openaiErrorDataSchemaValidator = lazySchema(() =>
22
+ zodSchema(openaiErrorDataSchema()),
23
+ );
16
24
 
17
- export type OpenAIErrorData = z.infer<typeof openaiErrorDataSchema>;
25
+ export type OpenAIErrorData = z.infer<ReturnType<typeof openaiErrorDataSchema>>;
18
26
 
19
27
  export const openaiFailedResponseHandler = createJsonErrorResponseHandler({
20
- errorSchema: openaiErrorDataSchema,
28
+ errorSchema: openaiErrorDataSchemaValidator,
21
29
  errorToMessage: data => data.error.message,
22
30
  });
@@ -8,6 +8,12 @@ export type OpenAIResponsesUsage = JSONObject & {
8
8
  | (JSONObject & {
9
9
  cached_tokens?: number | null;
10
10
  cache_write_tokens?: number | null;
11
+ cache_write_tokens_details?:
12
+ | (JSONObject & {
13
+ ttl_30m_tokens?: number | null;
14
+ ttl_12h_tokens?: number | null;
15
+ })
16
+ | null;
11
17
  orchestration_input_tokens?: number | null;
12
18
  orchestration_input_cached_tokens?: number | null;
13
19
  })
@@ -8,6 +8,9 @@ import {
8
8
  type SharedV3Warning,
9
9
  } from '@ai-sdk/provider';
10
10
  import {
11
+ lazySchema,
12
+ zodSchema,
13
+ type InferSchema,
11
14
  convertToBase64,
12
15
  isNonNullable,
13
16
  parseJSON,
@@ -36,6 +39,7 @@ import type {
36
39
  OpenAIResponsesCustomToolCallOutput,
37
40
  OpenAIResponsesFunctionCallOutput,
38
41
  OpenAIResponsesInput,
42
+ OpenAIResponsesPromptCacheBreakpoint,
39
43
  OpenAIResponsesReasoning,
40
44
  OpenAIResponsesWebSearchCall,
41
45
  } from './openai-responses-api';
@@ -114,7 +118,7 @@ async function convertFunctionToolResultOutput({
114
118
  warnings,
115
119
  }: {
116
120
  output: LanguageModelV3ToolResultOutput;
117
- promptCacheBreakpoint?: OpenAIPromptCacheBreakpoint;
121
+ promptCacheBreakpoint?: OpenAIResponsesPromptCacheBreakpoint;
118
122
  providerOptionsName: string;
119
123
  warnings: Array<SharedV3Warning>;
120
124
  }): Promise<OpenAIResponsesFunctionCallOutput['output']> {
@@ -344,14 +348,12 @@ function collectCompleteParallelToolResultGroups({
344
348
  return completeGroups;
345
349
  }
346
350
 
347
- type OpenAIPromptCacheBreakpoint = { mode: 'explicit' };
348
-
349
351
  function getPromptCacheBreakpoint(
350
352
  providerOptions: SharedV3ProviderOptions | undefined,
351
353
  providerOptionsName: string,
352
- ): OpenAIPromptCacheBreakpoint | undefined {
354
+ ): OpenAIResponsesPromptCacheBreakpoint | undefined {
353
355
  return providerOptions?.[providerOptionsName]?.promptCacheBreakpoint as
354
- | OpenAIPromptCacheBreakpoint
356
+ | OpenAIResponsesPromptCacheBreakpoint
355
357
  | undefined;
356
358
  }
357
359
 
@@ -363,7 +365,7 @@ function getScalarToolResultPromptCacheBreakpoint({
363
365
  output: LanguageModelV3ToolResultOutput;
364
366
  toolResultProviderOptions: SharedV3ProviderOptions | undefined;
365
367
  providerOptionsName: string;
366
- }): OpenAIPromptCacheBreakpoint | undefined {
368
+ }): OpenAIResponsesPromptCacheBreakpoint | undefined {
367
369
  return output.type === 'content'
368
370
  ? undefined
369
371
  : (getPromptCacheBreakpoint(output.providerOptions, providerOptionsName) ??
@@ -1524,11 +1526,15 @@ export async function convertToOpenAIResponsesInput({
1524
1526
  return { input, warnings };
1525
1527
  }
1526
1528
 
1527
- const openaiResponsesReasoningProviderOptionsSchema = z.object({
1528
- itemId: z.string().nullish(),
1529
- reasoningEncryptedContent: z.string().nullish(),
1530
- });
1529
+ const openaiResponsesReasoningProviderOptionsSchema = lazySchema(() =>
1530
+ zodSchema(
1531
+ z.object({
1532
+ itemId: z.string().nullish(),
1533
+ reasoningEncryptedContent: z.string().nullish(),
1534
+ }),
1535
+ ),
1536
+ );
1531
1537
 
1532
- export type OpenAIResponsesReasoningProviderOptions = z.infer<
1538
+ export type OpenAIResponsesReasoningProviderOptions = InferSchema<
1533
1539
  typeof openaiResponsesReasoningProviderOptionsSchema
1534
1540
  >;