@ai-sdk/openai 4.0.0-beta.1 → 4.0.0-beta.10
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 +137 -0
- package/dist/index.d.mts +69 -22
- package/dist/index.d.ts +69 -22
- package/dist/index.js +1169 -873
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1123 -822
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +56 -28
- package/dist/internal/index.d.ts +56 -28
- package/dist/internal/index.js +1198 -912
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1180 -889
- package/dist/internal/index.mjs.map +1 -1
- package/docs/03-openai.mdx +142 -3
- package/package.json +3 -3
- package/src/chat/convert-openai-chat-usage.ts +2 -2
- package/src/chat/convert-to-openai-chat-messages.ts +5 -5
- package/src/chat/map-openai-finish-reason.ts +2 -2
- package/src/chat/openai-chat-language-model.ts +22 -22
- package/src/chat/openai-chat-options.ts +1 -0
- package/src/chat/openai-chat-prepare-tools.ts +6 -6
- package/src/completion/convert-openai-completion-usage.ts +2 -2
- package/src/completion/convert-to-openai-completion-prompt.ts +2 -2
- package/src/completion/map-openai-finish-reason.ts +2 -2
- package/src/completion/openai-completion-language-model.ts +20 -20
- package/src/embedding/openai-embedding-model.ts +5 -5
- package/src/image/openai-image-model.ts +9 -9
- package/src/openai-language-model-capabilities.ts +1 -0
- package/src/openai-provider.ts +21 -21
- package/src/openai-tools.ts +12 -1
- package/src/responses/convert-openai-responses-usage.ts +2 -2
- package/src/responses/convert-to-openai-responses-input.ts +116 -12
- package/src/responses/map-openai-responses-finish-reason.ts +2 -2
- package/src/responses/openai-responses-api.ts +87 -1
- package/src/responses/openai-responses-language-model.ts +168 -33
- package/src/responses/openai-responses-options.ts +4 -2
- package/src/responses/openai-responses-prepare-tools.ts +34 -9
- package/src/speech/openai-speech-model.ts +7 -7
- package/src/tool/custom.ts +0 -6
- package/src/tool/tool-search.ts +98 -0
- package/src/transcription/openai-transcription-model.ts +8 -8
package/docs/03-openai.mdx
CHANGED
|
@@ -958,6 +958,145 @@ Your execute function must return:
|
|
|
958
958
|
- **status** _'completed' | 'failed'_ - Whether the patch was applied successfully
|
|
959
959
|
- **output** _string_ (optional) - Human-readable log text (e.g., results or error messages)
|
|
960
960
|
|
|
961
|
+
#### Tool Search
|
|
962
|
+
|
|
963
|
+
Tool search allows the model to dynamically search for and load tools into context as needed,
|
|
964
|
+
rather than loading all tool definitions up front. This can reduce token usage, cost, and latency
|
|
965
|
+
when you have many tools. Mark the tools you want to make searchable with `deferLoading: true`
|
|
966
|
+
in their `providerOptions`.
|
|
967
|
+
|
|
968
|
+
There are two execution modes:
|
|
969
|
+
|
|
970
|
+
- **Server-executed (hosted):** OpenAI searches across the deferred tools declared in the request and returns the loaded subset in the same response. No extra round-trip is needed.
|
|
971
|
+
- **Client-executed:** The model emits a `tool_search_call`, your application performs the lookup, and you return the matching tools via the `execute` callback.
|
|
972
|
+
|
|
973
|
+
##### Server-Executed (Hosted) Tool Search
|
|
974
|
+
|
|
975
|
+
Use hosted tool search when the candidate tools are already known at request time.
|
|
976
|
+
Add `openai.tools.toolSearch()` with no arguments and mark your tools with `deferLoading: true`:
|
|
977
|
+
|
|
978
|
+
```ts
|
|
979
|
+
import { openai } from '@ai-sdk/openai';
|
|
980
|
+
import { generateText, tool, stepCountIs } from 'ai';
|
|
981
|
+
import { z } from 'zod';
|
|
982
|
+
|
|
983
|
+
const result = await generateText({
|
|
984
|
+
model: openai.responses('gpt-5.4'),
|
|
985
|
+
prompt: 'What is the weather in San Francisco?',
|
|
986
|
+
stopWhen: stepCountIs(10),
|
|
987
|
+
tools: {
|
|
988
|
+
toolSearch: openai.tools.toolSearch(),
|
|
989
|
+
|
|
990
|
+
get_weather: tool({
|
|
991
|
+
description: 'Get the current weather at a specific location',
|
|
992
|
+
inputSchema: z.object({
|
|
993
|
+
location: z.string(),
|
|
994
|
+
unit: z.enum(['celsius', 'fahrenheit']),
|
|
995
|
+
}),
|
|
996
|
+
execute: async ({ location, unit }) => ({
|
|
997
|
+
location,
|
|
998
|
+
temperature: unit === 'celsius' ? 18 : 64,
|
|
999
|
+
}),
|
|
1000
|
+
providerOptions: {
|
|
1001
|
+
openai: { deferLoading: true },
|
|
1002
|
+
},
|
|
1003
|
+
}),
|
|
1004
|
+
|
|
1005
|
+
search_files: tool({
|
|
1006
|
+
description: 'Search through files in the workspace',
|
|
1007
|
+
inputSchema: z.object({ query: z.string() }),
|
|
1008
|
+
execute: async ({ query }) => ({
|
|
1009
|
+
results: [`Found 3 files matching "${query}"`],
|
|
1010
|
+
}),
|
|
1011
|
+
providerOptions: {
|
|
1012
|
+
openai: { deferLoading: true },
|
|
1013
|
+
},
|
|
1014
|
+
}),
|
|
1015
|
+
},
|
|
1016
|
+
});
|
|
1017
|
+
```
|
|
1018
|
+
|
|
1019
|
+
In hosted mode, the model internally searches the deferred tools, loads the relevant ones, and
|
|
1020
|
+
proceeds to call them — all within a single response. The `tool_search_call` and
|
|
1021
|
+
`tool_search_output` items appear in the response with `execution: 'server'` and `call_id: null`.
|
|
1022
|
+
|
|
1023
|
+
##### Client-Executed Tool Search
|
|
1024
|
+
|
|
1025
|
+
Use client-executed tool search when tool discovery depends on runtime state — for example,
|
|
1026
|
+
tools that vary per tenant, project, or external system. Pass `execution: 'client'` along with
|
|
1027
|
+
a `description`, `parameters` schema, and an `execute` callback:
|
|
1028
|
+
|
|
1029
|
+
```ts
|
|
1030
|
+
import { openai } from '@ai-sdk/openai';
|
|
1031
|
+
import { generateText, tool, stepCountIs } from 'ai';
|
|
1032
|
+
import { z } from 'zod';
|
|
1033
|
+
|
|
1034
|
+
const result = await generateText({
|
|
1035
|
+
model: openai.responses('gpt-5.4'),
|
|
1036
|
+
prompt: 'What is the weather in San Francisco?',
|
|
1037
|
+
stopWhen: stepCountIs(10),
|
|
1038
|
+
tools: {
|
|
1039
|
+
toolSearch: openai.tools.toolSearch({
|
|
1040
|
+
execution: 'client',
|
|
1041
|
+
description: 'Search for available tools based on what the user needs.',
|
|
1042
|
+
parameters: {
|
|
1043
|
+
type: 'object',
|
|
1044
|
+
properties: {
|
|
1045
|
+
goal: {
|
|
1046
|
+
type: 'string',
|
|
1047
|
+
description: 'What the user is trying to accomplish',
|
|
1048
|
+
},
|
|
1049
|
+
},
|
|
1050
|
+
required: ['goal'],
|
|
1051
|
+
additionalProperties: false,
|
|
1052
|
+
},
|
|
1053
|
+
execute: async ({ arguments: args }) => {
|
|
1054
|
+
// Your custom tool discovery logic here.
|
|
1055
|
+
// Return the tools that match the search goal.
|
|
1056
|
+
return {
|
|
1057
|
+
tools: [
|
|
1058
|
+
{
|
|
1059
|
+
type: 'function',
|
|
1060
|
+
name: 'get_weather',
|
|
1061
|
+
description: 'Get the current weather at a specific location',
|
|
1062
|
+
deferLoading: true,
|
|
1063
|
+
parameters: {
|
|
1064
|
+
type: 'object',
|
|
1065
|
+
properties: {
|
|
1066
|
+
location: { type: 'string' },
|
|
1067
|
+
},
|
|
1068
|
+
required: ['location'],
|
|
1069
|
+
additionalProperties: false,
|
|
1070
|
+
},
|
|
1071
|
+
},
|
|
1072
|
+
],
|
|
1073
|
+
};
|
|
1074
|
+
},
|
|
1075
|
+
}),
|
|
1076
|
+
|
|
1077
|
+
get_weather: tool({
|
|
1078
|
+
description: 'Get the current weather at a specific location',
|
|
1079
|
+
inputSchema: z.object({ location: z.string() }),
|
|
1080
|
+
execute: async ({ location }) => ({
|
|
1081
|
+
location,
|
|
1082
|
+
temperature: 64,
|
|
1083
|
+
condition: 'Partly cloudy',
|
|
1084
|
+
}),
|
|
1085
|
+
providerOptions: {
|
|
1086
|
+
openai: { deferLoading: true },
|
|
1087
|
+
},
|
|
1088
|
+
}),
|
|
1089
|
+
},
|
|
1090
|
+
});
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
In client mode, the flow spans two steps:
|
|
1094
|
+
|
|
1095
|
+
1. **Step 1:** The model emits a `tool_search_call` with `execution: 'client'` and a non-null `call_id`. The SDK calls your `execute` callback with the search arguments. Your callback returns the discovered tools.
|
|
1096
|
+
2. **Step 2:** The SDK sends the `tool_search_output` (with the matching `call_id`) back to the model. The model can now call the loaded tools as normal function calls.
|
|
1097
|
+
|
|
1098
|
+
For more details, see the [OpenAI Tool Search documentation](https://platform.openai.com/docs/guides/tools-tool-search).
|
|
1099
|
+
|
|
961
1100
|
#### Custom Tool
|
|
962
1101
|
|
|
963
1102
|
The OpenAI Responses API supports
|
|
@@ -975,7 +1114,6 @@ const result = await generateText({
|
|
|
975
1114
|
model: openai.responses('gpt-5.2-codex'),
|
|
976
1115
|
tools: {
|
|
977
1116
|
write_sql: openai.tools.customTool({
|
|
978
|
-
name: 'write_sql',
|
|
979
1117
|
description: 'Write a SQL SELECT query to answer the user question.',
|
|
980
1118
|
format: {
|
|
981
1119
|
type: 'grammar',
|
|
@@ -1005,7 +1143,6 @@ const result = streamText({
|
|
|
1005
1143
|
model: openai.responses('gpt-5.2-codex'),
|
|
1006
1144
|
tools: {
|
|
1007
1145
|
write_sql: openai.tools.customTool({
|
|
1008
|
-
name: 'write_sql',
|
|
1009
1146
|
description: 'Write a SQL SELECT query to answer the user question.',
|
|
1010
1147
|
format: {
|
|
1011
1148
|
type: 'grammar',
|
|
@@ -1028,7 +1165,6 @@ for await (const chunk of result.fullStream) {
|
|
|
1028
1165
|
|
|
1029
1166
|
The custom tool can be configured with:
|
|
1030
1167
|
|
|
1031
|
-
- **name** _string_ (required) - The name of the custom tool. Used to identify the tool in tool calls.
|
|
1032
1168
|
- **description** _string_ (optional) - A description of what the tool does, to help the model understand when to use it.
|
|
1033
1169
|
- **format** _object_ (optional) - The output format constraint. Omit for unconstrained text output.
|
|
1034
1170
|
- **type** _'grammar' | 'text'_ - The format type. Use `'grammar'` for constrained output or `'text'` for explicit unconstrained text.
|
|
@@ -2041,6 +2177,9 @@ The following optional provider options are available for OpenAI completion mode
|
|
|
2041
2177
|
|
|
2042
2178
|
| Model | Image Input | Audio Input | Object Generation | Tool Usage |
|
|
2043
2179
|
| --------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
|
|
2180
|
+
| `gpt-5.4-pro` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
2181
|
+
| `gpt-5.4` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
2182
|
+
| `gpt-5.3-chat-latest` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
2044
2183
|
| `gpt-5.2-pro` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
2045
2184
|
| `gpt-5.2-chat-latest` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
2046
2185
|
| `gpt-5.2` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/openai",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.10",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@ai-sdk/provider": "4.0.0-beta.
|
|
40
|
-
"@ai-sdk/provider-utils": "5.0.0-beta.
|
|
39
|
+
"@ai-sdk/provider": "4.0.0-beta.2",
|
|
40
|
+
"@ai-sdk/provider-utils": "5.0.0-beta.4"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "20.17.24",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export type OpenAIChatUsage = {
|
|
4
4
|
prompt_tokens?: number | null;
|
|
@@ -16,7 +16,7 @@ export type OpenAIChatUsage = {
|
|
|
16
16
|
|
|
17
17
|
export function convertOpenAIChatUsage(
|
|
18
18
|
usage: OpenAIChatUsage | undefined | null,
|
|
19
|
-
):
|
|
19
|
+
): LanguageModelV4Usage {
|
|
20
20
|
if (usage == null) {
|
|
21
21
|
return {
|
|
22
22
|
inputTokens: {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
SharedV4Warning,
|
|
3
|
+
LanguageModelV4Prompt,
|
|
4
4
|
UnsupportedFunctionalityError,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import { OpenAIChatPrompt } from './openai-chat-prompt';
|
|
@@ -10,14 +10,14 @@ export function convertToOpenAIChatMessages({
|
|
|
10
10
|
prompt,
|
|
11
11
|
systemMessageMode = 'system',
|
|
12
12
|
}: {
|
|
13
|
-
prompt:
|
|
13
|
+
prompt: LanguageModelV4Prompt;
|
|
14
14
|
systemMessageMode?: 'system' | 'developer' | 'remove';
|
|
15
15
|
}): {
|
|
16
16
|
messages: OpenAIChatPrompt;
|
|
17
|
-
warnings: Array<
|
|
17
|
+
warnings: Array<SharedV4Warning>;
|
|
18
18
|
} {
|
|
19
19
|
const messages: OpenAIChatPrompt = [];
|
|
20
|
-
const warnings: Array<
|
|
20
|
+
const warnings: Array<SharedV4Warning> = [];
|
|
21
21
|
|
|
22
22
|
for (const { role, content } of prompt) {
|
|
23
23
|
switch (role) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export function mapOpenAIFinishReason(
|
|
4
4
|
finishReason: string | null | undefined,
|
|
5
|
-
):
|
|
5
|
+
): LanguageModelV4FinishReason['unified'] {
|
|
6
6
|
switch (finishReason) {
|
|
7
7
|
case 'stop':
|
|
8
8
|
return 'stop';
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
InvalidResponseDataError,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
LanguageModelV4,
|
|
4
|
+
LanguageModelV4CallOptions,
|
|
5
|
+
LanguageModelV4Content,
|
|
6
|
+
LanguageModelV4FinishReason,
|
|
7
|
+
LanguageModelV4GenerateResult,
|
|
8
|
+
LanguageModelV4StreamPart,
|
|
9
|
+
LanguageModelV4StreamResult,
|
|
10
|
+
SharedV4ProviderMetadata,
|
|
11
|
+
SharedV4Warning,
|
|
12
12
|
} from '@ai-sdk/provider';
|
|
13
13
|
import {
|
|
14
14
|
FetchFunction,
|
|
@@ -48,8 +48,8 @@ type OpenAIChatConfig = {
|
|
|
48
48
|
fetch?: FetchFunction;
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
-
export class OpenAIChatLanguageModel implements
|
|
52
|
-
readonly specificationVersion = '
|
|
51
|
+
export class OpenAIChatLanguageModel implements LanguageModelV4 {
|
|
52
|
+
readonly specificationVersion = 'v4';
|
|
53
53
|
|
|
54
54
|
readonly modelId: OpenAIChatModelId;
|
|
55
55
|
|
|
@@ -82,8 +82,8 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
|
|
82
82
|
tools,
|
|
83
83
|
toolChoice,
|
|
84
84
|
providerOptions,
|
|
85
|
-
}:
|
|
86
|
-
const warnings:
|
|
85
|
+
}: LanguageModelV4CallOptions) {
|
|
86
|
+
const warnings: SharedV4Warning[] = [];
|
|
87
87
|
|
|
88
88
|
// Parse provider options
|
|
89
89
|
const openaiOptions =
|
|
@@ -314,8 +314,8 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
|
|
314
314
|
}
|
|
315
315
|
|
|
316
316
|
async doGenerate(
|
|
317
|
-
options:
|
|
318
|
-
): Promise<
|
|
317
|
+
options: LanguageModelV4CallOptions,
|
|
318
|
+
): Promise<LanguageModelV4GenerateResult> {
|
|
319
319
|
const { args: body, warnings } = await this.getArgs(options);
|
|
320
320
|
|
|
321
321
|
const {
|
|
@@ -338,7 +338,7 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
|
|
338
338
|
});
|
|
339
339
|
|
|
340
340
|
const choice = response.choices[0];
|
|
341
|
-
const content: Array<
|
|
341
|
+
const content: Array<LanguageModelV4Content> = [];
|
|
342
342
|
|
|
343
343
|
// text content:
|
|
344
344
|
const text = choice.message.content;
|
|
@@ -370,7 +370,7 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
|
|
370
370
|
// provider metadata:
|
|
371
371
|
const completionTokenDetails = response.usage?.completion_tokens_details;
|
|
372
372
|
const promptTokenDetails = response.usage?.prompt_tokens_details;
|
|
373
|
-
const providerMetadata:
|
|
373
|
+
const providerMetadata: SharedV4ProviderMetadata = { openai: {} };
|
|
374
374
|
if (completionTokenDetails?.accepted_prediction_tokens != null) {
|
|
375
375
|
providerMetadata.openai.acceptedPredictionTokens =
|
|
376
376
|
completionTokenDetails?.accepted_prediction_tokens;
|
|
@@ -402,8 +402,8 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
|
|
402
402
|
}
|
|
403
403
|
|
|
404
404
|
async doStream(
|
|
405
|
-
options:
|
|
406
|
-
): Promise<
|
|
405
|
+
options: LanguageModelV4CallOptions,
|
|
406
|
+
): Promise<LanguageModelV4StreamResult> {
|
|
407
407
|
const { args, warnings } = await this.getArgs(options);
|
|
408
408
|
|
|
409
409
|
const body = {
|
|
@@ -439,7 +439,7 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
|
|
439
439
|
hasFinished: boolean;
|
|
440
440
|
}> = [];
|
|
441
441
|
|
|
442
|
-
let finishReason:
|
|
442
|
+
let finishReason: LanguageModelV4FinishReason = {
|
|
443
443
|
unified: 'other',
|
|
444
444
|
raw: undefined,
|
|
445
445
|
};
|
|
@@ -447,13 +447,13 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
|
|
|
447
447
|
let metadataExtracted = false;
|
|
448
448
|
let isActiveText = false;
|
|
449
449
|
|
|
450
|
-
const providerMetadata:
|
|
450
|
+
const providerMetadata: SharedV4ProviderMetadata = { openai: {} };
|
|
451
451
|
|
|
452
452
|
return {
|
|
453
453
|
stream: response.pipeThrough(
|
|
454
454
|
new TransformStream<
|
|
455
455
|
ParseResult<OpenAIChatChunk>,
|
|
456
|
-
|
|
456
|
+
LanguageModelV4StreamPart
|
|
457
457
|
>({
|
|
458
458
|
start(controller) {
|
|
459
459
|
controller.enqueue({ type: 'stream-start', warnings });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
LanguageModelV4CallOptions,
|
|
3
|
+
SharedV4Warning,
|
|
4
4
|
UnsupportedFunctionalityError,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import {
|
|
@@ -12,17 +12,17 @@ export function prepareChatTools({
|
|
|
12
12
|
tools,
|
|
13
13
|
toolChoice,
|
|
14
14
|
}: {
|
|
15
|
-
tools:
|
|
16
|
-
toolChoice?:
|
|
15
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
16
|
+
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
17
17
|
}): {
|
|
18
18
|
tools?: OpenAIChatFunctionTool[];
|
|
19
19
|
toolChoice?: OpenAIChatToolChoice;
|
|
20
|
-
toolWarnings: Array<
|
|
20
|
+
toolWarnings: Array<SharedV4Warning>;
|
|
21
21
|
} {
|
|
22
22
|
// when the tools array is empty, change it to undefined to prevent errors:
|
|
23
23
|
tools = tools?.length ? tools : undefined;
|
|
24
24
|
|
|
25
|
-
const toolWarnings:
|
|
25
|
+
const toolWarnings: SharedV4Warning[] = [];
|
|
26
26
|
|
|
27
27
|
if (tools == null) {
|
|
28
28
|
return { tools: undefined, toolChoice: undefined, toolWarnings };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export type OpenAICompletionUsage = {
|
|
4
4
|
prompt_tokens?: number | null;
|
|
@@ -8,7 +8,7 @@ export type OpenAICompletionUsage = {
|
|
|
8
8
|
|
|
9
9
|
export function convertOpenAICompletionUsage(
|
|
10
10
|
usage: OpenAICompletionUsage | undefined | null,
|
|
11
|
-
):
|
|
11
|
+
): LanguageModelV4Usage {
|
|
12
12
|
if (usage == null) {
|
|
13
13
|
return {
|
|
14
14
|
inputTokens: {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
InvalidPromptError,
|
|
3
|
-
|
|
3
|
+
LanguageModelV4Prompt,
|
|
4
4
|
UnsupportedFunctionalityError,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ export function convertToOpenAICompletionPrompt({
|
|
|
9
9
|
user = 'user',
|
|
10
10
|
assistant = 'assistant',
|
|
11
11
|
}: {
|
|
12
|
-
prompt:
|
|
12
|
+
prompt: LanguageModelV4Prompt;
|
|
13
13
|
user?: string;
|
|
14
14
|
assistant?: string;
|
|
15
15
|
}): {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export function mapOpenAIFinishReason(
|
|
4
4
|
finishReason: string | null | undefined,
|
|
5
|
-
):
|
|
5
|
+
): LanguageModelV4FinishReason['unified'] {
|
|
6
6
|
switch (finishReason) {
|
|
7
7
|
case 'stop':
|
|
8
8
|
return 'stop';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
LanguageModelV4,
|
|
3
|
+
LanguageModelV4CallOptions,
|
|
4
|
+
LanguageModelV4FinishReason,
|
|
5
|
+
LanguageModelV4GenerateResult,
|
|
6
|
+
LanguageModelV4StreamPart,
|
|
7
|
+
LanguageModelV4StreamResult,
|
|
8
|
+
SharedV4ProviderMetadata,
|
|
9
|
+
SharedV4Warning,
|
|
10
10
|
} from '@ai-sdk/provider';
|
|
11
11
|
import {
|
|
12
12
|
combineHeaders,
|
|
@@ -42,8 +42,8 @@ type OpenAICompletionConfig = {
|
|
|
42
42
|
fetch?: FetchFunction;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
export class OpenAICompletionLanguageModel implements
|
|
46
|
-
readonly specificationVersion = '
|
|
45
|
+
export class OpenAICompletionLanguageModel implements LanguageModelV4 {
|
|
46
|
+
readonly specificationVersion = 'v4';
|
|
47
47
|
|
|
48
48
|
readonly modelId: OpenAICompletionModelId;
|
|
49
49
|
|
|
@@ -83,8 +83,8 @@ export class OpenAICompletionLanguageModel implements LanguageModelV3 {
|
|
|
83
83
|
toolChoice,
|
|
84
84
|
seed,
|
|
85
85
|
providerOptions,
|
|
86
|
-
}:
|
|
87
|
-
const warnings:
|
|
86
|
+
}: LanguageModelV4CallOptions) {
|
|
87
|
+
const warnings: SharedV4Warning[] = [];
|
|
88
88
|
|
|
89
89
|
// Parse provider options
|
|
90
90
|
const openaiOptions = {
|
|
@@ -161,8 +161,8 @@ export class OpenAICompletionLanguageModel implements LanguageModelV3 {
|
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
async doGenerate(
|
|
164
|
-
options:
|
|
165
|
-
): Promise<
|
|
164
|
+
options: LanguageModelV4CallOptions,
|
|
165
|
+
): Promise<LanguageModelV4GenerateResult> {
|
|
166
166
|
const { args, warnings } = await this.getArgs(options);
|
|
167
167
|
|
|
168
168
|
const {
|
|
@@ -186,7 +186,7 @@ export class OpenAICompletionLanguageModel implements LanguageModelV3 {
|
|
|
186
186
|
|
|
187
187
|
const choice = response.choices[0];
|
|
188
188
|
|
|
189
|
-
const providerMetadata:
|
|
189
|
+
const providerMetadata: SharedV4ProviderMetadata = { openai: {} };
|
|
190
190
|
|
|
191
191
|
if (choice.logprobs != null) {
|
|
192
192
|
providerMetadata.openai.logprobs = choice.logprobs;
|
|
@@ -211,8 +211,8 @@ export class OpenAICompletionLanguageModel implements LanguageModelV3 {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
async doStream(
|
|
214
|
-
options:
|
|
215
|
-
): Promise<
|
|
214
|
+
options: LanguageModelV4CallOptions,
|
|
215
|
+
): Promise<LanguageModelV4StreamResult> {
|
|
216
216
|
const { args, warnings } = await this.getArgs(options);
|
|
217
217
|
|
|
218
218
|
const body = {
|
|
@@ -239,11 +239,11 @@ export class OpenAICompletionLanguageModel implements LanguageModelV3 {
|
|
|
239
239
|
fetch: this.config.fetch,
|
|
240
240
|
});
|
|
241
241
|
|
|
242
|
-
let finishReason:
|
|
242
|
+
let finishReason: LanguageModelV4FinishReason = {
|
|
243
243
|
unified: 'other',
|
|
244
244
|
raw: undefined,
|
|
245
245
|
};
|
|
246
|
-
const providerMetadata:
|
|
246
|
+
const providerMetadata: SharedV4ProviderMetadata = { openai: {} };
|
|
247
247
|
let usage: OpenAICompletionUsage | undefined = undefined;
|
|
248
248
|
let isFirstChunk = true;
|
|
249
249
|
|
|
@@ -251,7 +251,7 @@ export class OpenAICompletionLanguageModel implements LanguageModelV3 {
|
|
|
251
251
|
stream: response.pipeThrough(
|
|
252
252
|
new TransformStream<
|
|
253
253
|
ParseResult<OpenAICompletionChunk>,
|
|
254
|
-
|
|
254
|
+
LanguageModelV4StreamPart
|
|
255
255
|
>({
|
|
256
256
|
start(controller) {
|
|
257
257
|
controller.enqueue({ type: 'stream-start', warnings });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
EmbeddingModelV4,
|
|
3
3
|
TooManyEmbeddingValuesForCallError,
|
|
4
4
|
} from '@ai-sdk/provider';
|
|
5
5
|
import {
|
|
@@ -16,8 +16,8 @@ import {
|
|
|
16
16
|
} from './openai-embedding-options';
|
|
17
17
|
import { openaiTextEmbeddingResponseSchema } from './openai-embedding-api';
|
|
18
18
|
|
|
19
|
-
export class OpenAIEmbeddingModel implements
|
|
20
|
-
readonly specificationVersion = '
|
|
19
|
+
export class OpenAIEmbeddingModel implements EmbeddingModelV4 {
|
|
20
|
+
readonly specificationVersion = 'v4';
|
|
21
21
|
readonly modelId: OpenAIEmbeddingModelId;
|
|
22
22
|
readonly maxEmbeddingsPerCall = 2048;
|
|
23
23
|
readonly supportsParallelCalls = true;
|
|
@@ -38,8 +38,8 @@ export class OpenAIEmbeddingModel implements EmbeddingModelV3 {
|
|
|
38
38
|
headers,
|
|
39
39
|
abortSignal,
|
|
40
40
|
providerOptions,
|
|
41
|
-
}: Parameters<
|
|
42
|
-
Awaited<ReturnType<
|
|
41
|
+
}: Parameters<EmbeddingModelV4['doEmbed']>[0]): Promise<
|
|
42
|
+
Awaited<ReturnType<EmbeddingModelV4['doEmbed']>>
|
|
43
43
|
> {
|
|
44
44
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
45
45
|
throw new TooManyEmbeddingValuesForCallError({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
ImageModelV4,
|
|
3
|
+
ImageModelV4File,
|
|
4
|
+
SharedV4Warning,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import {
|
|
7
7
|
combineHeaders,
|
|
@@ -27,8 +27,8 @@ interface OpenAIImageModelConfig extends OpenAIConfig {
|
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
export class OpenAIImageModel implements
|
|
31
|
-
readonly specificationVersion = '
|
|
30
|
+
export class OpenAIImageModel implements ImageModelV4 {
|
|
31
|
+
readonly specificationVersion = 'v4';
|
|
32
32
|
|
|
33
33
|
get maxImagesPerCall(): number {
|
|
34
34
|
return modelMaxImagesPerCall[this.modelId] ?? 1;
|
|
@@ -54,10 +54,10 @@ export class OpenAIImageModel implements ImageModelV3 {
|
|
|
54
54
|
providerOptions,
|
|
55
55
|
headers,
|
|
56
56
|
abortSignal,
|
|
57
|
-
}: Parameters<
|
|
58
|
-
Awaited<ReturnType<
|
|
57
|
+
}: Parameters<ImageModelV4['doGenerate']>[0]): Promise<
|
|
58
|
+
Awaited<ReturnType<ImageModelV4['doGenerate']>>
|
|
59
59
|
> {
|
|
60
|
-
const warnings: Array<
|
|
60
|
+
const warnings: Array<SharedV4Warning> = [];
|
|
61
61
|
|
|
62
62
|
if (aspectRatio != null) {
|
|
63
63
|
warnings.push({
|
|
@@ -332,7 +332,7 @@ type OpenAIImageEditInput = {
|
|
|
332
332
|
};
|
|
333
333
|
|
|
334
334
|
async function fileToBlob(
|
|
335
|
-
file:
|
|
335
|
+
file: ImageModelV4File | undefined,
|
|
336
336
|
): Promise<Blob | undefined> {
|
|
337
337
|
if (!file) return undefined;
|
|
338
338
|
|
|
@@ -40,6 +40,7 @@ export function getOpenAILanguageModelCapabilities(
|
|
|
40
40
|
const supportsNonReasoningParameters =
|
|
41
41
|
modelId.startsWith('gpt-5.1') ||
|
|
42
42
|
modelId.startsWith('gpt-5.2') ||
|
|
43
|
+
modelId.startsWith('gpt-5.3') ||
|
|
43
44
|
modelId.startsWith('gpt-5.4');
|
|
44
45
|
|
|
45
46
|
const systemMessageMode = isReasoningModel ? 'developer' : 'system';
|