@ai-sdk/openai 3.0.130 → 3.0.132
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 +16 -0
- package/dist/index.d.mts +10 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.js +77 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +221 -192
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +96 -3
- package/dist/internal/index.d.ts +96 -3
- package/dist/internal/index.js +85 -53
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +225 -191
- package/dist/internal/index.mjs.map +1 -1
- package/docs/03-openai.mdx +3 -3
- package/package.json +5 -5
- package/src/chat/openai-chat-api.ts +1 -1
- package/src/completion/openai-completion-api.ts +1 -1
- package/src/image/openai-image-model-options.ts +40 -39
- package/src/internal/index.ts +1 -0
- package/src/openai-error.ts +22 -14
- package/src/responses/convert-openai-responses-usage.ts +6 -0
- package/src/responses/convert-to-openai-responses-input.ts +17 -11
- package/src/responses/openai-responses-api.ts +117 -94
- package/src/responses/openai-responses-language-model.ts +1 -0
- package/src/responses/openai-responses-options.ts +4 -2
- package/src/responses/openai-responses-prepare-tools.ts +4 -3
- package/src/tool/file-search.ts +15 -12
- package/src/tool/mcp.ts +13 -11
- package/src/tool/shell.ts +22 -21
package/docs/03-openai.mdx
CHANGED
|
@@ -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'
|
|
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.
|
|
3
|
+
"version": "3.0.132",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@ai-sdk/provider": "
|
|
40
|
-
"@ai-sdk/provider
|
|
39
|
+
"@ai-sdk/provider-utils": "4.0.62",
|
|
40
|
+
"@ai-sdk/provider": "3.0.19"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "20.17.24",
|
|
44
44
|
"tsup": "^8",
|
|
45
45
|
"typescript": "5.8.3",
|
|
46
46
|
"zod": "3.25.76",
|
|
47
|
-
"@ai-
|
|
48
|
-
"@
|
|
47
|
+
"@vercel/ai-tsconfig": "0.0.0",
|
|
48
|
+
"@ai-sdk/test-server": "1.0.6"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -49,45 +49,46 @@ export function getMaxImagesPerCall(modelId: OpenAIImageModelId): number {
|
|
|
49
49
|
);
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
const baseImageModelOptionsObject =
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
*/
|
package/src/internal/index.ts
CHANGED
|
@@ -16,5 +16,6 @@ export * from '../tool/apply-patch';
|
|
|
16
16
|
export * from '../tool/code-interpreter';
|
|
17
17
|
export * from '../tool/file-search';
|
|
18
18
|
export * from '../tool/image-generation';
|
|
19
|
+
export * from '../tool/tool-search';
|
|
19
20
|
export * from '../tool/web-search';
|
|
20
21
|
export * from '../tool/web-search-preview';
|
package/src/openai-error.ts
CHANGED
|
@@ -1,22 +1,30 @@
|
|
|
1
1
|
import { z } from 'zod/v4';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
createJsonErrorResponseHandler,
|
|
6
|
+
} from '@ai-sdk/provider-utils';
|
|
3
7
|
|
|
4
|
-
export const openaiErrorDataSchema =
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
export const openaiErrorDataSchema = () =>
|
|
9
|
+
z.object({
|
|
10
|
+
error: z.object({
|
|
11
|
+
message: z.string(),
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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:
|
|
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?:
|
|
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
|
-
):
|
|
354
|
+
): OpenAIResponsesPromptCacheBreakpoint | undefined {
|
|
353
355
|
return providerOptions?.[providerOptionsName]?.promptCacheBreakpoint as
|
|
354
|
-
|
|
|
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
|
-
}):
|
|
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 =
|
|
1528
|
-
|
|
1529
|
-
|
|
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 =
|
|
1538
|
+
export type OpenAIResponsesReasoningProviderOptions = InferSchema<
|
|
1533
1539
|
typeof openaiResponsesReasoningProviderOptionsSchema
|
|
1534
1540
|
>;
|