@ai-sdk/openai 3.0.123 → 3.0.124

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.124",
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
+ }
@@ -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
  }),