@ai-sdk/openai 3.0.123 → 3.0.125

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/openai",
3
- "version": "3.0.123",
3
+ "version": "3.0.125",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -23,6 +23,7 @@ import {
23
23
  } from '@ai-sdk/provider-utils';
24
24
  import { openaiFailedResponseHandler } from '../openai-error';
25
25
  import { getOpenAILanguageModelCapabilities } from '../openai-language-model-capabilities';
26
+ import { normalizeOpenAIJsonSchema } from '../normalize-openai-json-schema';
26
27
  import { throwIfOpenAIStreamErrorBeforeOutput } from '../openai-stream-error';
27
28
  import {
28
29
  convertOpenAIChatUsage,
@@ -168,7 +169,7 @@ export class OpenAIChatLanguageModel implements LanguageModelV3 {
168
169
  ? {
169
170
  type: 'json_schema',
170
171
  json_schema: {
171
- schema: responseFormat.schema,
172
+ schema: normalizeOpenAIJsonSchema(responseFormat.schema),
172
173
  strict: strictJsonSchema,
173
174
  name: responseFormat.name ?? 'response',
174
175
  description: responseFormat.description,
@@ -0,0 +1,142 @@
1
+ import type { JSONSchema7, JSONSchema7Definition } from '@ai-sdk/provider';
2
+
3
+ /**
4
+ * OpenAI does not support `allOf`. Zod 4 uses a singleton `allOf` wrapper for
5
+ * recursive references, which can be rewritten to a direct reference.
6
+ */
7
+ export function normalizeOpenAIJsonSchema(schema: JSONSchema7): JSONSchema7 {
8
+ const normalizedSchema = normalizeSchema(schema, true) as JSONSchema7;
9
+ const reference = getSingletonReference(normalizedSchema);
10
+
11
+ if (reference == null) {
12
+ return normalizedSchema;
13
+ }
14
+
15
+ const { allOf: _allOf, ...schemaWithoutAllOf } = normalizedSchema;
16
+ const referencedSchema = getLocalReferenceSchema(reference, normalizedSchema);
17
+
18
+ // OpenAI requires an object at the schema root, so expand local root refs.
19
+ return typeof referencedSchema !== 'object' || referencedSchema == null
20
+ ? normalizedSchema
21
+ : { ...referencedSchema, ...schemaWithoutAllOf };
22
+
23
+ function normalizeSchema(
24
+ schema: JSONSchema7Definition,
25
+ isRoot = false,
26
+ ): JSONSchema7Definition {
27
+ if (typeof schema !== 'object' || schema == null) {
28
+ return schema;
29
+ }
30
+
31
+ const normalizedSchema: JSONSchema7 = { ...schema };
32
+
33
+ if (normalizedSchema.properties != null) {
34
+ normalizedSchema.properties = normalizeSchemaRecord(
35
+ normalizedSchema.properties,
36
+ );
37
+ }
38
+
39
+ if (normalizedSchema.patternProperties != null) {
40
+ normalizedSchema.patternProperties = normalizeSchemaRecord(
41
+ normalizedSchema.patternProperties,
42
+ );
43
+ }
44
+
45
+ if (normalizedSchema.definitions != null) {
46
+ normalizedSchema.definitions = normalizeSchemaRecord(
47
+ normalizedSchema.definitions,
48
+ );
49
+ }
50
+
51
+ if (normalizedSchema.$defs != null) {
52
+ normalizedSchema.$defs = normalizeSchemaRecord(normalizedSchema.$defs);
53
+ }
54
+
55
+ if (normalizedSchema.items != null) {
56
+ normalizedSchema.items = Array.isArray(normalizedSchema.items)
57
+ ? normalizedSchema.items.map(item => normalizeSchema(item))
58
+ : normalizeSchema(normalizedSchema.items);
59
+ }
60
+
61
+ if (normalizedSchema.allOf != null) {
62
+ normalizedSchema.allOf = normalizedSchema.allOf.map(item =>
63
+ normalizeSchema(item),
64
+ );
65
+ }
66
+
67
+ if (normalizedSchema.anyOf != null) {
68
+ normalizedSchema.anyOf = normalizedSchema.anyOf.map(item =>
69
+ normalizeSchema(item),
70
+ );
71
+ }
72
+
73
+ if (normalizedSchema.oneOf != null) {
74
+ normalizedSchema.oneOf = normalizedSchema.oneOf.map(item =>
75
+ normalizeSchema(item),
76
+ );
77
+ }
78
+
79
+ if (
80
+ typeof normalizedSchema.additionalProperties === 'object' &&
81
+ normalizedSchema.additionalProperties != null
82
+ ) {
83
+ normalizedSchema.additionalProperties = normalizeSchema(
84
+ normalizedSchema.additionalProperties,
85
+ );
86
+ }
87
+
88
+ const reference = getSingletonReference(normalizedSchema);
89
+
90
+ if (reference == null || isRoot) {
91
+ return normalizedSchema;
92
+ }
93
+
94
+ const { allOf: _allOf, ...schemaWithoutAllOf } = normalizedSchema;
95
+ return { ...schemaWithoutAllOf, $ref: reference };
96
+ }
97
+
98
+ function normalizeSchemaRecord(
99
+ record: Record<string, JSONSchema7Definition>,
100
+ ): Record<string, JSONSchema7Definition> {
101
+ return Object.fromEntries(
102
+ Object.entries(record).map(([key, value]) => [
103
+ key,
104
+ normalizeSchema(value),
105
+ ]),
106
+ );
107
+ }
108
+
109
+ function getSingletonReference(schema: JSONSchema7): string | undefined {
110
+ if (schema.allOf?.length !== 1) {
111
+ return undefined;
112
+ }
113
+
114
+ const [allOfSchema] = schema.allOf;
115
+ return typeof allOfSchema === 'object' &&
116
+ allOfSchema != null &&
117
+ Object.keys(allOfSchema).length === 1 &&
118
+ typeof allOfSchema.$ref === 'string'
119
+ ? allOfSchema.$ref
120
+ : undefined;
121
+ }
122
+
123
+ function getLocalReferenceSchema(
124
+ reference: string,
125
+ schema: JSONSchema7,
126
+ ): JSONSchema7Definition | undefined {
127
+ const match = /^#\/(definitions|\$defs)\/(.+)$/.exec(reference);
128
+
129
+ if (match == null) {
130
+ return undefined;
131
+ }
132
+
133
+ const [, keyword, encodedName] = match;
134
+ const name = encodedName.replace(/~1/g, '/').replace(/~0/g, '~');
135
+ const definition =
136
+ keyword === 'definitions'
137
+ ? schema.definitions?.[name]
138
+ : schema.$defs?.[name];
139
+
140
+ return definition;
141
+ }
142
+ }
@@ -12,6 +12,7 @@ import {
12
12
  isNonNullable,
13
13
  parseJSON,
14
14
  parseProviderOptions,
15
+ safeValidateTypes,
15
16
  validateTypes,
16
17
  type ToolNameMapping,
17
18
  } from '@ai-sdk/provider-utils';
@@ -30,11 +31,13 @@ import {
30
31
  toolSearchInputSchema,
31
32
  toolSearchOutputSchema,
32
33
  } from '../tool/tool-search';
34
+ import { webSearchOutputSchema } from '../tool/web-search';
33
35
  import type {
34
36
  OpenAIResponsesCustomToolCallOutput,
35
37
  OpenAIResponsesFunctionCallOutput,
36
38
  OpenAIResponsesInput,
37
39
  OpenAIResponsesReasoning,
40
+ OpenAIResponsesWebSearchCall,
38
41
  } from './openai-responses-api';
39
42
  import {
40
43
  getParallelToolCallMetadata,
@@ -45,6 +48,65 @@ function serializeToolCallArguments(input: unknown): string {
45
48
  return JSON.stringify(input === undefined ? {} : input);
46
49
  }
47
50
 
51
+ async function convertWebSearchToolResultOutput({
52
+ output,
53
+ id,
54
+ }: {
55
+ output: LanguageModelV3ToolResultOutput;
56
+ id: string;
57
+ }): Promise<OpenAIResponsesWebSearchCall | undefined> {
58
+ if (output.type !== 'json') {
59
+ return undefined;
60
+ }
61
+
62
+ const validation = await safeValidateTypes({
63
+ value: output.value,
64
+ schema: webSearchOutputSchema,
65
+ });
66
+
67
+ if (!validation.success || validation.value.action == null) {
68
+ return undefined;
69
+ }
70
+
71
+ const { action, sources } = validation.value;
72
+
73
+ switch (action.type) {
74
+ case 'search':
75
+ return {
76
+ type: 'web_search_call',
77
+ id,
78
+ status: 'completed',
79
+ action: {
80
+ type: 'search',
81
+ ...(action.query != null && { query: action.query }),
82
+ ...(action.queries != null && { queries: action.queries }),
83
+ ...(sources != null && { sources }),
84
+ },
85
+ };
86
+ case 'openPage':
87
+ return {
88
+ type: 'web_search_call',
89
+ id,
90
+ status: 'completed',
91
+ action: {
92
+ type: 'open_page',
93
+ url: action.url,
94
+ },
95
+ };
96
+ case 'findInPage':
97
+ return {
98
+ type: 'web_search_call',
99
+ id,
100
+ status: 'completed',
101
+ action: {
102
+ type: 'find_in_page',
103
+ url: action.url,
104
+ pattern: action.pattern,
105
+ },
106
+ };
107
+ }
108
+ }
109
+
48
110
  async function convertFunctionToolResultOutput({
49
111
  output,
50
112
  promptCacheBreakpoint,
@@ -865,6 +927,33 @@ export async function convertToOpenAIResponsesInput({
865
927
  part.toolName,
866
928
  );
867
929
 
930
+ if (
931
+ resolvedResultToolName === 'web_search' ||
932
+ resolvedResultToolName === 'web_search_preview'
933
+ ) {
934
+ const itemId =
935
+ (
936
+ part.providerOptions?.[providerOptionsName] as
937
+ | { itemId?: string }
938
+ | undefined
939
+ )?.itemId ?? part.toolCallId;
940
+
941
+ if (store) {
942
+ input.push({ type: 'item_reference', id: itemId });
943
+ break;
944
+ }
945
+
946
+ const webSearchCall = await convertWebSearchToolResultOutput({
947
+ output: part.output,
948
+ id: itemId,
949
+ });
950
+
951
+ if (webSearchCall != null) {
952
+ input.push(webSearchCall);
953
+ break;
954
+ }
955
+ }
956
+
868
957
  if (part.toolName === toolSearchToolName) {
869
958
  const itemId = (part.providerOptions?.[providerOptionsName]
870
959
  ?.itemId ??
@@ -86,6 +86,7 @@ export type OpenAIResponsesInputItem =
86
86
  | OpenAIResponsesApplyPatchCallOutput
87
87
  | OpenAIResponsesToolSearchCall
88
88
  | OpenAIResponsesToolSearchOutput
89
+ | OpenAIResponsesWebSearchCall
89
90
  | OpenAIResponsesReasoning
90
91
  | OpenAIResponsesItemReference
91
92
  | OpenAIResponsesConfigurationUpdate;
@@ -228,6 +229,13 @@ export type OpenAIResponsesFunctionCallOutput = {
228
229
  >;
229
230
  };
230
231
 
232
+ export type OpenAIResponsesWebSearchCall = {
233
+ type: 'web_search_call';
234
+ id: string;
235
+ status: 'completed';
236
+ action: OpenAIResponsesWebSearchAction;
237
+ };
238
+
231
239
  export type OpenAIResponsesCustomToolCall = {
232
240
  type: 'custom_tool_call';
233
241
  id?: string;
@@ -612,7 +620,7 @@ const openaiResponsesNestedErrorChunkSchema = z.object({
612
620
  sequence_number: z.number(),
613
621
  error: z.object({
614
622
  type: z.string(),
615
- code: z.string(),
623
+ code: z.string().nullish(),
616
624
  message: z.string(),
617
625
  param: z.string().nullish(),
618
626
  }),
@@ -30,6 +30,7 @@ import {
30
30
  import type { OpenAIConfig } from '../openai-config';
31
31
  import { openaiFailedResponseHandler } from '../openai-error';
32
32
  import { getOpenAILanguageModelCapabilities } from '../openai-language-model-capabilities';
33
+ import { normalizeOpenAIJsonSchema } from '../normalize-openai-json-schema';
33
34
  import {
34
35
  createOpenAIProviderStreamError,
35
36
  throwIfOpenAIStreamErrorBeforeOutput,
@@ -422,7 +423,7 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV3 {
422
423
  strict: strictJsonSchema,
423
424
  name: responseFormat.name ?? 'response',
424
425
  description: responseFormat.description,
425
- schema: responseFormat.schema,
426
+ schema: normalizeOpenAIJsonSchema(responseFormat.schema),
426
427
  }
427
428
  : { type: 'json_object' },
428
429
  }),