@ai-sdk/openai 4.0.98 → 4.0.100
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 +27 -0
- package/dist/index.d.ts +14 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +95 -89
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +31 -44
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +82 -72
- package/dist/internal/index.js.map +1 -1
- package/docs/03-openai.mdx +52 -8
- package/package.json +3 -3
- package/src/chat/openai-chat-api.ts +1 -1
- package/src/chat/openai-chat-language-model.ts +2 -1
- package/src/completion/openai-completion-api.ts +1 -1
- package/src/completion/openai-completion-language-model.ts +2 -1
- package/src/files/openai-files.ts +1 -11
- package/src/image/openai-image-model-options.ts +40 -39
- package/src/live/openai-realtime-model-live.ts +13 -4
- package/src/openai-batch.ts +2 -2
- package/src/openai-decision-model.ts +55 -44
- 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 +221 -192
- package/src/responses/openai-responses-language-model-options.ts +4 -2
- package/src/responses/openai-responses-language-model.ts +2 -1
- package/src/tool/computer.ts +64 -58
- 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
|
@@ -1,17 +1,61 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: OpenAI
|
|
3
|
-
description: Learn how to use
|
|
3
|
+
description: Learn how to use OpenAI with AI Gateway and the direct provider.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# OpenAI Provider
|
|
7
7
|
|
|
8
8
|
The [OpenAI](https://openai.com/) provider contains language model support for the OpenAI responses, chat, and completion APIs, as well as embedding model support for the OpenAI embeddings API.
|
|
9
|
+
You can use OpenAI models through [AI Gateway](/providers/ai-sdk-providers/ai-gateway) (recommended) or connect directly with `@ai-sdk/openai`.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
```ts provider="openai" model="openai/gpt-6-astra"
|
|
12
|
+
import { generateText } from 'ai';
|
|
13
|
+
__PROVIDER_IMPORT__;
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
const { text } = await generateText({
|
|
16
|
+
model: __MODEL__,
|
|
17
|
+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
console.log(text);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Using AI Gateway
|
|
24
|
+
|
|
25
|
+
The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install `ai`:
|
|
26
|
+
|
|
27
|
+
<InstallPackages packages="ai" />
|
|
28
|
+
|
|
29
|
+
Set an [AI Gateway API key](https://vercel.com/docs/ai-gateway/getting-started) in your environment:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
export AI_GATEWAY_API_KEY=your_api_key_here
|
|
33
|
+
```
|
|
13
34
|
|
|
14
|
-
|
|
35
|
+
Vercel deployments can also use [OIDC authentication](/providers/ai-sdk-providers/ai-gateway#oidc-authentication-vercel-deployments).
|
|
36
|
+
See the [AI Gateway provider documentation](/providers/ai-sdk-providers/ai-gateway) for configuration and the [model catalog](https://vercel.com/ai-gateway/models) for available models.
|
|
37
|
+
|
|
38
|
+
Gateway feature and provider-option support depends on the model and the provider serving the request. See [Gateway provider options](https://vercel.com/docs/ai-gateway/models-and-providers/provider-options).
|
|
39
|
+
|
|
40
|
+
## Using `@ai-sdk/openai`
|
|
41
|
+
|
|
42
|
+
For direct access to OpenAI, install `ai` and `@ai-sdk/openai`:
|
|
43
|
+
|
|
44
|
+
<InstallPackages packages="ai @ai-sdk/openai" />
|
|
45
|
+
|
|
46
|
+
Set your OpenAI API credential in the `OPENAI_API_KEY` environment variable:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
export OPENAI_API_KEY=your_api_key_here
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
AI Gateway does not expose every API in `@ai-sdk/openai`. Use the direct provider for provider-specific APIs or custom connection settings.
|
|
53
|
+
|
|
54
|
+
<Note>
|
|
55
|
+
The following sections document the direct `@ai-sdk/openai` provider,
|
|
56
|
+
including its model factories and settings. Gateway model and feature
|
|
57
|
+
availability can differ.
|
|
58
|
+
</Note>
|
|
15
59
|
|
|
16
60
|
## Provider Instance
|
|
17
61
|
|
|
@@ -239,7 +283,7 @@ The following provider options are available:
|
|
|
239
283
|
OpenAI structured outputs have several
|
|
240
284
|
[limitations](https://openai.com/index/introducing-structured-outputs-in-the-api),
|
|
241
285
|
in particular around the [supported
|
|
242
|
-
schemas](https://
|
|
286
|
+
schemas](https://developers.openai.com/api/docs/guides/structured-outputs#supported-schemas),
|
|
243
287
|
and are therefore opt-in. For example, optional schema properties are not
|
|
244
288
|
supported. You need to change Zod `.nullish()` and `.optional()` to
|
|
245
289
|
`.nullable()`.
|
|
@@ -274,7 +318,7 @@ The following provider options are available:
|
|
|
274
318
|
A cache key for manual prompt caching control. Used by OpenAI to cache responses for similar requests to optimize your cache hit rates.
|
|
275
319
|
|
|
276
320
|
- **promptCacheOptions** _object_
|
|
277
|
-
Configures prompt caching for GPT-5.6 and later models. `mode` can be `'implicit'` or `'explicit'
|
|
321
|
+
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.
|
|
278
322
|
|
|
279
323
|
- **promptCacheRetention** _'in_memory' | '24h'_
|
|
280
324
|
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.
|
|
@@ -2814,7 +2858,7 @@ including `gpt-4o` and `gpt-4o-mini`.
|
|
|
2814
2858
|
not need to be explicitly enabled.
|
|
2815
2859
|
- You can use the response `usage` to access the number of prompt tokens that were a cache hit via `usage.inputTokenDetails.cacheReadTokens`.
|
|
2816
2860
|
- For GPT-5.6 and later models, `usage.inputTokenDetails.cacheWriteTokens` reports tokens written to the cache.
|
|
2817
|
-
- 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.
|
|
2861
|
+
- 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.
|
|
2818
2862
|
|
|
2819
2863
|
```ts highlight="9"
|
|
2820
2864
|
import { openai } from '@ai-sdk/openai';
|
|
@@ -2847,7 +2891,7 @@ const { text, usage } = await generateText({
|
|
|
2847
2891
|
console.log('Cached tokens:', usage.inputTokenDetails.cacheReadTokens);
|
|
2848
2892
|
```
|
|
2849
2893
|
|
|
2850
|
-
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`:
|
|
2894
|
+
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`:
|
|
2851
2895
|
|
|
2852
2896
|
```ts highlight="9-12,20-28"
|
|
2853
2897
|
import {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/openai",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.100",
|
|
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.
|
|
39
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
38
|
+
"@ai-sdk/provider": "4.0.32",
|
|
39
|
+
"@ai-sdk/provider-utils": "5.0.66"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@ai-sdk/test-server": "2.0.4",
|
|
@@ -532,8 +532,9 @@ export class OpenAIChatLanguageModel implements LanguageModelV4 {
|
|
|
532
532
|
|
|
533
533
|
let toolCallTracker: StreamingToolCallTracker;
|
|
534
534
|
|
|
535
|
+
// A clean end without the provider's completion signal is interrupted.
|
|
535
536
|
let finishReason: LanguageModelV4FinishReason = {
|
|
536
|
-
unified: '
|
|
537
|
+
unified: 'interrupted',
|
|
537
538
|
raw: undefined,
|
|
538
539
|
};
|
|
539
540
|
let usage: OpenAIChatUsage | undefined = undefined;
|
|
@@ -272,8 +272,9 @@ export class OpenAICompletionLanguageModel implements LanguageModelV4 {
|
|
|
272
272
|
responseHeaders,
|
|
273
273
|
});
|
|
274
274
|
|
|
275
|
+
// A clean end without the provider's completion signal is interrupted.
|
|
275
276
|
let finishReason: LanguageModelV4FinishReason = {
|
|
276
|
-
unified: '
|
|
277
|
+
unified: 'interrupted',
|
|
277
278
|
raw: undefined,
|
|
278
279
|
};
|
|
279
280
|
const providerMetadata: SharedV4ProviderMetadata = { openai: {} };
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
createBinaryStreamResponseHandler,
|
|
18
18
|
createJsonResponseHandler,
|
|
19
19
|
deleteFromApi,
|
|
20
|
+
encodePathSegment,
|
|
20
21
|
getFromApi,
|
|
21
22
|
parseProviderOptions,
|
|
22
23
|
postFormDataToApi,
|
|
@@ -44,17 +45,6 @@ interface OpenAIFilesConfig {
|
|
|
44
45
|
|
|
45
46
|
type OpenAIFilesResponse = InferSchema<typeof openaiFilesResponseSchema>;
|
|
46
47
|
|
|
47
|
-
function encodePathSegment(value: string): string {
|
|
48
|
-
const encodedValue = encodeURIComponent(value);
|
|
49
|
-
|
|
50
|
-
// URL parsing normalizes both literal and percent-encoded dot segments.
|
|
51
|
-
return encodedValue === '.'
|
|
52
|
-
? '%252E'
|
|
53
|
-
: encodedValue === '..'
|
|
54
|
-
? '%252E%252E'
|
|
55
|
-
: encodedValue;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
48
|
export class OpenAIFiles implements FilesV4 {
|
|
59
49
|
readonly specificationVersion = 'v4';
|
|
60
50
|
|
|
@@ -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
|
*/
|
|
@@ -4,6 +4,8 @@ import {
|
|
|
4
4
|
type Experimental_RealtimeModelV4SessionConfig as RealtimeModelV4SessionConfig,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import {
|
|
7
|
+
lazySchema,
|
|
8
|
+
zodSchema,
|
|
7
9
|
createJsonResponseHandler,
|
|
8
10
|
postJsonToApi,
|
|
9
11
|
} from '@ai-sdk/provider-utils';
|
|
@@ -20,10 +22,17 @@ import type { OpenAIRealtimeModelConfig } from '../realtime/openai-realtime-mode
|
|
|
20
22
|
|
|
21
23
|
export type OpenAIRealtimeModelLiveConfig = OpenAIRealtimeModelConfig;
|
|
22
24
|
|
|
23
|
-
const webRTCSessionSchema =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
})
|
|
25
|
+
const webRTCSessionSchema = lazySchema(() =>
|
|
26
|
+
zodSchema(
|
|
27
|
+
z.object({
|
|
28
|
+
session: z.object({ id: z.string().min(1) }),
|
|
29
|
+
transport: z.object({
|
|
30
|
+
type: z.literal('webrtc'),
|
|
31
|
+
sdp: z.string().min(1),
|
|
32
|
+
}),
|
|
33
|
+
}),
|
|
34
|
+
),
|
|
35
|
+
);
|
|
27
36
|
|
|
28
37
|
export class OpenAIRealtimeModelLive implements RealtimeModelV4 {
|
|
29
38
|
readonly specificationVersion = 'v4' as const;
|
package/src/openai-batch.ts
CHANGED
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
} from '@ai-sdk/provider-utils';
|
|
36
36
|
import { z } from 'zod/v4';
|
|
37
37
|
import {
|
|
38
|
-
|
|
38
|
+
openaiErrorDataSchemaValidator,
|
|
39
39
|
openaiFailedResponseHandler,
|
|
40
40
|
} from './openai-error';
|
|
41
41
|
import type { OpenAIConfig } from './openai-config';
|
|
@@ -663,7 +663,7 @@ async function convertOpenAIErrorResponse({
|
|
|
663
663
|
}): Promise<BatchV4Error> {
|
|
664
664
|
const result = await safeValidateTypes({
|
|
665
665
|
value: body,
|
|
666
|
-
schema:
|
|
666
|
+
schema: openaiErrorDataSchemaValidator,
|
|
667
667
|
});
|
|
668
668
|
|
|
669
669
|
if (!result.success) {
|
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
type Experimental_DecisionModelV4Input as DecisionModelV4Input,
|
|
7
7
|
} from '@ai-sdk/provider';
|
|
8
8
|
import {
|
|
9
|
+
lazySchema,
|
|
10
|
+
zodSchema,
|
|
9
11
|
combineHeaders,
|
|
10
12
|
convertUint8ArrayToBase64,
|
|
11
13
|
detectMediaType,
|
|
@@ -27,54 +29,63 @@ import { openaiDecisionModelOptions } from './openai-decision-model-options';
|
|
|
27
29
|
|
|
28
30
|
export type OpenAIDecisionModelId = 'gpt-6-luna' | (string & {});
|
|
29
31
|
|
|
30
|
-
const probability = z.number().min(0).max(1);
|
|
31
|
-
const responseSchema =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
input_tokens_details: z
|
|
32
|
+
const probability = () => z.number().min(0).max(1);
|
|
33
|
+
const responseSchema = lazySchema(() =>
|
|
34
|
+
zodSchema(
|
|
35
|
+
z.object({
|
|
36
|
+
model: z.string().nullish(),
|
|
37
|
+
usage: z
|
|
37
38
|
.object({
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
input_tokens: z.number().nullish(),
|
|
40
|
+
input_tokens_details: z
|
|
41
|
+
.object({
|
|
42
|
+
cached_tokens: z.number().nullish(),
|
|
43
|
+
cache_write_tokens: z.number().nullish(),
|
|
44
|
+
})
|
|
45
|
+
.nullish(),
|
|
46
|
+
output_tokens: z.number().nullish(),
|
|
47
|
+
output_tokens_details: z
|
|
48
|
+
.object({
|
|
49
|
+
reasoning_tokens: z.number().nullish(),
|
|
50
|
+
})
|
|
51
|
+
.nullish(),
|
|
52
|
+
total_tokens: z.number().nullish(),
|
|
40
53
|
})
|
|
41
54
|
.nullish(),
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}),
|
|
75
|
-
]),
|
|
55
|
+
answers: z.array(
|
|
56
|
+
z.discriminatedUnion('type', [
|
|
57
|
+
z.object({ type: z.literal('refusal'), name: z.string().nullable() }),
|
|
58
|
+
z.object({
|
|
59
|
+
type: z.literal('predicate'),
|
|
60
|
+
name: z.string(),
|
|
61
|
+
probability: probability(),
|
|
62
|
+
}),
|
|
63
|
+
z.object({
|
|
64
|
+
type: z.literal('choice'),
|
|
65
|
+
name: z.string(),
|
|
66
|
+
choice: z.string(),
|
|
67
|
+
confidence: probability().nullish(),
|
|
68
|
+
probabilities: z.array(
|
|
69
|
+
z.object({ value: z.string(), probability: probability() }),
|
|
70
|
+
),
|
|
71
|
+
}),
|
|
72
|
+
z.object({
|
|
73
|
+
type: z.literal('score'),
|
|
74
|
+
name: z.string(),
|
|
75
|
+
score: z.number(),
|
|
76
|
+
confidence: probability().nullish(),
|
|
77
|
+
probabilities: z.array(
|
|
78
|
+
z.object({
|
|
79
|
+
value: z.number().int().nonnegative(),
|
|
80
|
+
probability: probability(),
|
|
81
|
+
}),
|
|
82
|
+
),
|
|
83
|
+
}),
|
|
84
|
+
]),
|
|
85
|
+
),
|
|
86
|
+
}),
|
|
76
87
|
),
|
|
77
|
-
|
|
88
|
+
);
|
|
78
89
|
|
|
79
90
|
export class DecisionOpenAIModel implements DecisionModelV4 {
|
|
80
91
|
readonly specificationVersion = 'v4';
|
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
|
});
|
|
@@ -9,6 +9,12 @@ export type OpenAIResponsesUsage = JSONObject & {
|
|
|
9
9
|
| (JSONObject & {
|
|
10
10
|
cached_tokens?: number | null;
|
|
11
11
|
cache_write_tokens?: number | null;
|
|
12
|
+
cache_write_tokens_details?:
|
|
13
|
+
| (JSONObject & {
|
|
14
|
+
ttl_30m_tokens?: number | null;
|
|
15
|
+
ttl_12h_tokens?: number | null;
|
|
16
|
+
})
|
|
17
|
+
| null;
|
|
12
18
|
orchestration_input_tokens?: number | null;
|
|
13
19
|
orchestration_input_cached_tokens?: number | null;
|
|
14
20
|
})
|
|
@@ -8,6 +8,9 @@ import {
|
|
|
8
8
|
type SharedV4Warning,
|
|
9
9
|
} from '@ai-sdk/provider';
|
|
10
10
|
import {
|
|
11
|
+
lazySchema,
|
|
12
|
+
zodSchema,
|
|
13
|
+
type InferSchema,
|
|
11
14
|
convertToBase64,
|
|
12
15
|
getTopLevelMediaType,
|
|
13
16
|
isNonNullable,
|
|
@@ -39,6 +42,7 @@ import type {
|
|
|
39
42
|
OpenAIResponsesCustomToolCallOutput,
|
|
40
43
|
OpenAIResponsesFunctionCallOutput,
|
|
41
44
|
OpenAIResponsesInput,
|
|
45
|
+
OpenAIResponsesPromptCacheBreakpoint,
|
|
42
46
|
OpenAIResponsesReasoning,
|
|
43
47
|
OpenAIResponsesToolCaller,
|
|
44
48
|
OpenAIResponsesWebSearchCall,
|
|
@@ -144,7 +148,7 @@ async function convertFunctionToolResultOutput({
|
|
|
144
148
|
output: LanguageModelV4ToolResultOutput;
|
|
145
149
|
toolName: string;
|
|
146
150
|
outputSchemaToolNames: Set<string> | undefined;
|
|
147
|
-
promptCacheBreakpoint?:
|
|
151
|
+
promptCacheBreakpoint?: OpenAIResponsesPromptCacheBreakpoint;
|
|
148
152
|
providerOptionsName: string;
|
|
149
153
|
warnings: Array<SharedV4Warning>;
|
|
150
154
|
}): Promise<OpenAIResponsesFunctionCallOutput['output']> {
|
|
@@ -389,14 +393,12 @@ function collectCompleteParallelToolResultGroups({
|
|
|
389
393
|
return completeGroups;
|
|
390
394
|
}
|
|
391
395
|
|
|
392
|
-
type OpenAIPromptCacheBreakpoint = { mode: 'explicit' };
|
|
393
|
-
|
|
394
396
|
function getPromptCacheBreakpoint(
|
|
395
397
|
providerOptions: SharedV4ProviderOptions | undefined,
|
|
396
398
|
providerOptionsName: string,
|
|
397
|
-
):
|
|
399
|
+
): OpenAIResponsesPromptCacheBreakpoint | undefined {
|
|
398
400
|
return providerOptions?.[providerOptionsName]?.promptCacheBreakpoint as
|
|
399
|
-
|
|
|
401
|
+
| OpenAIResponsesPromptCacheBreakpoint
|
|
400
402
|
| undefined;
|
|
401
403
|
}
|
|
402
404
|
|
|
@@ -408,7 +410,7 @@ function getScalarToolResultPromptCacheBreakpoint({
|
|
|
408
410
|
output: LanguageModelV4ToolResultOutput;
|
|
409
411
|
toolResultProviderOptions: SharedV4ProviderOptions | undefined;
|
|
410
412
|
providerOptionsName: string;
|
|
411
|
-
}):
|
|
413
|
+
}): OpenAIResponsesPromptCacheBreakpoint | undefined {
|
|
412
414
|
return output.type === 'content'
|
|
413
415
|
? undefined
|
|
414
416
|
: (getPromptCacheBreakpoint(output.providerOptions, providerOptionsName) ??
|
|
@@ -1854,11 +1856,15 @@ export async function convertToOpenAIResponsesInput({
|
|
|
1854
1856
|
return { input, warnings };
|
|
1855
1857
|
}
|
|
1856
1858
|
|
|
1857
|
-
const openaiResponsesReasoningProviderOptionsSchema =
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1859
|
+
const openaiResponsesReasoningProviderOptionsSchema = lazySchema(() =>
|
|
1860
|
+
zodSchema(
|
|
1861
|
+
z.object({
|
|
1862
|
+
itemId: z.string().nullish(),
|
|
1863
|
+
reasoningEncryptedContent: z.string().nullish(),
|
|
1864
|
+
}),
|
|
1865
|
+
),
|
|
1866
|
+
);
|
|
1861
1867
|
|
|
1862
|
-
export type OpenAIResponsesReasoningProviderOptions =
|
|
1868
|
+
export type OpenAIResponsesReasoningProviderOptions = InferSchema<
|
|
1863
1869
|
typeof openaiResponsesReasoningProviderOptionsSchema
|
|
1864
1870
|
>;
|