@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.
@@ -128,6 +128,98 @@ function getGptVersion(modelId) {
128
128
  };
129
129
  }
130
130
 
131
+ // src/normalize-openai-json-schema.ts
132
+ function normalizeOpenAIJsonSchema(schema) {
133
+ const normalizedSchema = normalizeSchema(schema, true);
134
+ const reference = getSingletonReference(normalizedSchema);
135
+ if (reference == null) {
136
+ return normalizedSchema;
137
+ }
138
+ const { allOf: _allOf, ...schemaWithoutAllOf } = normalizedSchema;
139
+ const referencedSchema = getLocalReferenceSchema(reference, normalizedSchema);
140
+ return typeof referencedSchema !== "object" || referencedSchema == null ? normalizedSchema : { ...referencedSchema, ...schemaWithoutAllOf };
141
+ function normalizeSchema(schema2, isRoot = false) {
142
+ if (typeof schema2 !== "object" || schema2 == null) {
143
+ return schema2;
144
+ }
145
+ const normalizedSchema2 = { ...schema2 };
146
+ if (normalizedSchema2.properties != null) {
147
+ normalizedSchema2.properties = normalizeSchemaRecord(
148
+ normalizedSchema2.properties
149
+ );
150
+ }
151
+ if (normalizedSchema2.patternProperties != null) {
152
+ normalizedSchema2.patternProperties = normalizeSchemaRecord(
153
+ normalizedSchema2.patternProperties
154
+ );
155
+ }
156
+ if (normalizedSchema2.definitions != null) {
157
+ normalizedSchema2.definitions = normalizeSchemaRecord(
158
+ normalizedSchema2.definitions
159
+ );
160
+ }
161
+ if (normalizedSchema2.$defs != null) {
162
+ normalizedSchema2.$defs = normalizeSchemaRecord(normalizedSchema2.$defs);
163
+ }
164
+ if (normalizedSchema2.items != null) {
165
+ normalizedSchema2.items = Array.isArray(normalizedSchema2.items) ? normalizedSchema2.items.map((item) => normalizeSchema(item)) : normalizeSchema(normalizedSchema2.items);
166
+ }
167
+ if (normalizedSchema2.allOf != null) {
168
+ normalizedSchema2.allOf = normalizedSchema2.allOf.map(
169
+ (item) => normalizeSchema(item)
170
+ );
171
+ }
172
+ if (normalizedSchema2.anyOf != null) {
173
+ normalizedSchema2.anyOf = normalizedSchema2.anyOf.map(
174
+ (item) => normalizeSchema(item)
175
+ );
176
+ }
177
+ if (normalizedSchema2.oneOf != null) {
178
+ normalizedSchema2.oneOf = normalizedSchema2.oneOf.map(
179
+ (item) => normalizeSchema(item)
180
+ );
181
+ }
182
+ if (typeof normalizedSchema2.additionalProperties === "object" && normalizedSchema2.additionalProperties != null) {
183
+ normalizedSchema2.additionalProperties = normalizeSchema(
184
+ normalizedSchema2.additionalProperties
185
+ );
186
+ }
187
+ const reference2 = getSingletonReference(normalizedSchema2);
188
+ if (reference2 == null || isRoot) {
189
+ return normalizedSchema2;
190
+ }
191
+ const { allOf: _allOf2, ...schemaWithoutAllOf2 } = normalizedSchema2;
192
+ return { ...schemaWithoutAllOf2, $ref: reference2 };
193
+ }
194
+ function normalizeSchemaRecord(record) {
195
+ return Object.fromEntries(
196
+ Object.entries(record).map(([key, value]) => [
197
+ key,
198
+ normalizeSchema(value)
199
+ ])
200
+ );
201
+ }
202
+ function getSingletonReference(schema2) {
203
+ var _a2;
204
+ if (((_a2 = schema2.allOf) == null ? void 0 : _a2.length) !== 1) {
205
+ return void 0;
206
+ }
207
+ const [allOfSchema] = schema2.allOf;
208
+ return typeof allOfSchema === "object" && allOfSchema != null && Object.keys(allOfSchema).length === 1 && typeof allOfSchema.$ref === "string" ? allOfSchema.$ref : void 0;
209
+ }
210
+ function getLocalReferenceSchema(reference2, schema2) {
211
+ var _a2, _b;
212
+ const match = /^#\/(definitions|\$defs)\/(.+)$/.exec(reference2);
213
+ if (match == null) {
214
+ return void 0;
215
+ }
216
+ const [, keyword, encodedName] = match;
217
+ const name = encodedName.replace(/~1/g, "/").replace(/~0/g, "~");
218
+ const definition = keyword === "definitions" ? (_a2 = schema2.definitions) == null ? void 0 : _a2[name] : (_b = schema2.$defs) == null ? void 0 : _b[name];
219
+ return definition;
220
+ }
221
+ }
222
+
131
223
  // src/openai-stream-error.ts
132
224
  var import_provider = require("@ai-sdk/provider");
133
225
  function createOpenAIProviderStreamError(frame) {
@@ -1022,7 +1114,7 @@ var OpenAIChatLanguageModel = class {
1022
1114
  response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
1023
1115
  type: "json_schema",
1024
1116
  json_schema: {
1025
- schema: responseFormat.schema,
1117
+ schema: normalizeOpenAIJsonSchema(responseFormat.schema),
1026
1118
  strict: strictJsonSchema,
1027
1119
  name: (_e = responseFormat.name) != null ? _e : "response",
1028
1120
  description: responseFormat.description
@@ -6267,7 +6359,7 @@ var OpenAIResponsesLanguageModel = class {
6267
6359
  strict: strictJsonSchema,
6268
6360
  name: (_h = responseFormat.name) != null ? _h : "response",
6269
6361
  description: responseFormat.description,
6270
- schema: responseFormat.schema
6362
+ schema: normalizeOpenAIJsonSchema(responseFormat.schema)
6271
6363
  } : { type: "json_object" }
6272
6364
  },
6273
6365
  ...(openaiOptions == null ? void 0 : openaiOptions.textVerbosity) && {