@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.
@@ -72,6 +72,98 @@ function getGptVersion(modelId) {
72
72
  };
73
73
  }
74
74
 
75
+ // src/normalize-openai-json-schema.ts
76
+ function normalizeOpenAIJsonSchema(schema) {
77
+ const normalizedSchema = normalizeSchema(schema, true);
78
+ const reference = getSingletonReference(normalizedSchema);
79
+ if (reference == null) {
80
+ return normalizedSchema;
81
+ }
82
+ const { allOf: _allOf, ...schemaWithoutAllOf } = normalizedSchema;
83
+ const referencedSchema = getLocalReferenceSchema(reference, normalizedSchema);
84
+ return typeof referencedSchema !== "object" || referencedSchema == null ? normalizedSchema : { ...referencedSchema, ...schemaWithoutAllOf };
85
+ function normalizeSchema(schema2, isRoot = false) {
86
+ if (typeof schema2 !== "object" || schema2 == null) {
87
+ return schema2;
88
+ }
89
+ const normalizedSchema2 = { ...schema2 };
90
+ if (normalizedSchema2.properties != null) {
91
+ normalizedSchema2.properties = normalizeSchemaRecord(
92
+ normalizedSchema2.properties
93
+ );
94
+ }
95
+ if (normalizedSchema2.patternProperties != null) {
96
+ normalizedSchema2.patternProperties = normalizeSchemaRecord(
97
+ normalizedSchema2.patternProperties
98
+ );
99
+ }
100
+ if (normalizedSchema2.definitions != null) {
101
+ normalizedSchema2.definitions = normalizeSchemaRecord(
102
+ normalizedSchema2.definitions
103
+ );
104
+ }
105
+ if (normalizedSchema2.$defs != null) {
106
+ normalizedSchema2.$defs = normalizeSchemaRecord(normalizedSchema2.$defs);
107
+ }
108
+ if (normalizedSchema2.items != null) {
109
+ normalizedSchema2.items = Array.isArray(normalizedSchema2.items) ? normalizedSchema2.items.map((item) => normalizeSchema(item)) : normalizeSchema(normalizedSchema2.items);
110
+ }
111
+ if (normalizedSchema2.allOf != null) {
112
+ normalizedSchema2.allOf = normalizedSchema2.allOf.map(
113
+ (item) => normalizeSchema(item)
114
+ );
115
+ }
116
+ if (normalizedSchema2.anyOf != null) {
117
+ normalizedSchema2.anyOf = normalizedSchema2.anyOf.map(
118
+ (item) => normalizeSchema(item)
119
+ );
120
+ }
121
+ if (normalizedSchema2.oneOf != null) {
122
+ normalizedSchema2.oneOf = normalizedSchema2.oneOf.map(
123
+ (item) => normalizeSchema(item)
124
+ );
125
+ }
126
+ if (typeof normalizedSchema2.additionalProperties === "object" && normalizedSchema2.additionalProperties != null) {
127
+ normalizedSchema2.additionalProperties = normalizeSchema(
128
+ normalizedSchema2.additionalProperties
129
+ );
130
+ }
131
+ const reference2 = getSingletonReference(normalizedSchema2);
132
+ if (reference2 == null || isRoot) {
133
+ return normalizedSchema2;
134
+ }
135
+ const { allOf: _allOf2, ...schemaWithoutAllOf2 } = normalizedSchema2;
136
+ return { ...schemaWithoutAllOf2, $ref: reference2 };
137
+ }
138
+ function normalizeSchemaRecord(record) {
139
+ return Object.fromEntries(
140
+ Object.entries(record).map(([key, value]) => [
141
+ key,
142
+ normalizeSchema(value)
143
+ ])
144
+ );
145
+ }
146
+ function getSingletonReference(schema2) {
147
+ var _a2;
148
+ if (((_a2 = schema2.allOf) == null ? void 0 : _a2.length) !== 1) {
149
+ return void 0;
150
+ }
151
+ const [allOfSchema] = schema2.allOf;
152
+ return typeof allOfSchema === "object" && allOfSchema != null && Object.keys(allOfSchema).length === 1 && typeof allOfSchema.$ref === "string" ? allOfSchema.$ref : void 0;
153
+ }
154
+ function getLocalReferenceSchema(reference2, schema2) {
155
+ var _a2, _b;
156
+ const match = /^#\/(definitions|\$defs)\/(.+)$/.exec(reference2);
157
+ if (match == null) {
158
+ return void 0;
159
+ }
160
+ const [, keyword, encodedName] = match;
161
+ const name = encodedName.replace(/~1/g, "/").replace(/~0/g, "~");
162
+ const definition = keyword === "definitions" ? (_a2 = schema2.definitions) == null ? void 0 : _a2[name] : (_b = schema2.$defs) == null ? void 0 : _b[name];
163
+ return definition;
164
+ }
165
+ }
166
+
75
167
  // src/openai-stream-error.ts
76
168
  import { APICallError } from "@ai-sdk/provider";
77
169
  function createOpenAIProviderStreamError(frame) {
@@ -976,7 +1068,7 @@ var OpenAIChatLanguageModel = class {
976
1068
  response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
977
1069
  type: "json_schema",
978
1070
  json_schema: {
979
- schema: responseFormat.schema,
1071
+ schema: normalizeOpenAIJsonSchema(responseFormat.schema),
980
1072
  strict: strictJsonSchema,
981
1073
  name: (_e = responseFormat.name) != null ? _e : "response",
982
1074
  description: responseFormat.description
@@ -6350,7 +6442,7 @@ var OpenAIResponsesLanguageModel = class {
6350
6442
  strict: strictJsonSchema,
6351
6443
  name: (_h = responseFormat.name) != null ? _h : "response",
6352
6444
  description: responseFormat.description,
6353
- schema: responseFormat.schema
6445
+ schema: normalizeOpenAIJsonSchema(responseFormat.schema)
6354
6446
  } : { type: "json_object" }
6355
6447
  },
6356
6448
  ...(openaiOptions == null ? void 0 : openaiOptions.textVerbosity) && {