openai 6.47.0 → 6.49.0

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.
Files changed (212) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/azure.d.mts.map +1 -1
  3. package/azure.d.ts.map +1 -1
  4. package/azure.js +5 -1
  5. package/azure.js.map +1 -1
  6. package/azure.mjs +5 -1
  7. package/azure.mjs.map +1 -1
  8. package/helpers/standard-schema.d.mts +104 -0
  9. package/helpers/standard-schema.d.mts.map +1 -0
  10. package/helpers/standard-schema.d.ts +104 -0
  11. package/helpers/standard-schema.d.ts.map +1 -0
  12. package/helpers/standard-schema.js +320 -0
  13. package/helpers/standard-schema.js.map +1 -0
  14. package/helpers/standard-schema.mjs +314 -0
  15. package/helpers/standard-schema.mjs.map +1 -0
  16. package/helpers/zod.d.mts +26 -3
  17. package/helpers/zod.d.mts.map +1 -1
  18. package/helpers/zod.d.ts +26 -3
  19. package/helpers/zod.d.ts.map +1 -1
  20. package/helpers/zod.js +97 -7
  21. package/helpers/zod.js.map +1 -1
  22. package/helpers/zod.mjs +96 -7
  23. package/helpers/zod.mjs.map +1 -1
  24. package/internal/headers.d.mts.map +1 -1
  25. package/internal/headers.d.ts.map +1 -1
  26. package/internal/headers.js +7 -3
  27. package/internal/headers.js.map +1 -1
  28. package/internal/headers.mjs +7 -3
  29. package/internal/headers.mjs.map +1 -1
  30. package/lib/AbstractChatCompletionRunner.d.mts.map +1 -1
  31. package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
  32. package/lib/AbstractChatCompletionRunner.js +43 -1
  33. package/lib/AbstractChatCompletionRunner.js.map +1 -1
  34. package/lib/AbstractChatCompletionRunner.mjs +43 -1
  35. package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
  36. package/lib/ChatCompletionStream.d.mts +2 -1
  37. package/lib/ChatCompletionStream.d.mts.map +1 -1
  38. package/lib/ChatCompletionStream.d.ts +2 -1
  39. package/lib/ChatCompletionStream.d.ts.map +1 -1
  40. package/lib/ChatCompletionStream.js +53 -14
  41. package/lib/ChatCompletionStream.js.map +1 -1
  42. package/lib/ChatCompletionStream.mjs +53 -14
  43. package/lib/ChatCompletionStream.mjs.map +1 -1
  44. package/lib/ResponsesParser.d.mts.map +1 -1
  45. package/lib/ResponsesParser.d.ts.map +1 -1
  46. package/lib/ResponsesParser.js +3 -5
  47. package/lib/ResponsesParser.js.map +1 -1
  48. package/lib/ResponsesParser.mjs +3 -5
  49. package/lib/ResponsesParser.mjs.map +1 -1
  50. package/lib/jsonschema.d.mts +3 -0
  51. package/lib/jsonschema.d.mts.map +1 -1
  52. package/lib/jsonschema.d.ts +3 -0
  53. package/lib/jsonschema.d.ts.map +1 -1
  54. package/lib/transform.d.mts +25 -1
  55. package/lib/transform.d.mts.map +1 -1
  56. package/lib/transform.d.ts +25 -1
  57. package/lib/transform.d.ts.map +1 -1
  58. package/lib/transform.js +1521 -82
  59. package/lib/transform.js.map +1 -1
  60. package/lib/transform.mjs +1515 -82
  61. package/lib/transform.mjs.map +1 -1
  62. package/package.json +1 -1
  63. package/resources/admin/organization/audit-logs.d.mts +2 -2
  64. package/resources/admin/organization/audit-logs.d.mts.map +1 -1
  65. package/resources/admin/organization/audit-logs.d.ts +2 -2
  66. package/resources/admin/organization/audit-logs.d.ts.map +1 -1
  67. package/resources/admin/organization/index.d.mts +1 -0
  68. package/resources/admin/organization/index.d.mts.map +1 -1
  69. package/resources/admin/organization/index.d.ts +1 -0
  70. package/resources/admin/organization/index.d.ts.map +1 -1
  71. package/resources/admin/organization/index.js +3 -1
  72. package/resources/admin/organization/index.js.map +1 -1
  73. package/resources/admin/organization/index.mjs +1 -0
  74. package/resources/admin/organization/index.mjs.map +1 -1
  75. package/resources/admin/organization/organization.d.mts +4 -0
  76. package/resources/admin/organization/organization.d.mts.map +1 -1
  77. package/resources/admin/organization/organization.d.ts +4 -0
  78. package/resources/admin/organization/organization.d.ts.map +1 -1
  79. package/resources/admin/organization/organization.js +4 -0
  80. package/resources/admin/organization/organization.js.map +1 -1
  81. package/resources/admin/organization/organization.mjs +4 -0
  82. package/resources/admin/organization/organization.mjs.map +1 -1
  83. package/resources/admin/organization/projects/index.d.mts +2 -1
  84. package/resources/admin/organization/projects/index.d.mts.map +1 -1
  85. package/resources/admin/organization/projects/index.d.ts +2 -1
  86. package/resources/admin/organization/projects/index.d.ts.map +1 -1
  87. package/resources/admin/organization/projects/index.js +7 -5
  88. package/resources/admin/organization/projects/index.js.map +1 -1
  89. package/resources/admin/organization/projects/index.mjs +2 -1
  90. package/resources/admin/organization/projects/index.mjs.map +1 -1
  91. package/resources/admin/organization/projects/projects.d.mts +6 -2
  92. package/resources/admin/organization/projects/projects.d.mts.map +1 -1
  93. package/resources/admin/organization/projects/projects.d.ts +6 -2
  94. package/resources/admin/organization/projects/projects.d.ts.map +1 -1
  95. package/resources/admin/organization/projects/projects.js +6 -2
  96. package/resources/admin/organization/projects/projects.js.map +1 -1
  97. package/resources/admin/organization/projects/projects.mjs +6 -2
  98. package/resources/admin/organization/projects/projects.mjs.map +1 -1
  99. package/resources/admin/organization/projects/service-accounts/api-keys.d.mts +58 -0
  100. package/resources/admin/organization/projects/service-accounts/api-keys.d.mts.map +1 -0
  101. package/resources/admin/organization/projects/service-accounts/api-keys.d.ts +58 -0
  102. package/resources/admin/organization/projects/service-accounts/api-keys.d.ts.map +1 -0
  103. package/resources/admin/organization/projects/service-accounts/api-keys.js +26 -0
  104. package/resources/admin/organization/projects/service-accounts/api-keys.js.map +1 -0
  105. package/resources/admin/organization/projects/service-accounts/api-keys.mjs +22 -0
  106. package/resources/admin/organization/projects/service-accounts/api-keys.mjs.map +1 -0
  107. package/resources/admin/organization/projects/service-accounts/index.d.mts +3 -0
  108. package/resources/admin/organization/projects/service-accounts/index.d.mts.map +1 -0
  109. package/resources/admin/organization/projects/service-accounts/index.d.ts +3 -0
  110. package/resources/admin/organization/projects/service-accounts/index.d.ts.map +1 -0
  111. package/resources/admin/organization/projects/service-accounts/index.js +9 -0
  112. package/resources/admin/organization/projects/service-accounts/index.js.map +1 -0
  113. package/resources/admin/organization/projects/service-accounts/index.mjs +4 -0
  114. package/resources/admin/organization/projects/service-accounts/index.mjs.map +1 -0
  115. package/resources/admin/organization/projects/service-accounts/service-accounts.d.mts +177 -0
  116. package/resources/admin/organization/projects/service-accounts/service-accounts.d.mts.map +1 -0
  117. package/resources/admin/organization/projects/service-accounts/service-accounts.d.ts +177 -0
  118. package/resources/admin/organization/projects/service-accounts/service-accounts.d.ts.map +1 -0
  119. package/resources/admin/organization/projects/service-accounts/service-accounts.js +109 -0
  120. package/resources/admin/organization/projects/service-accounts/service-accounts.js.map +1 -0
  121. package/resources/admin/organization/projects/service-accounts/service-accounts.mjs +104 -0
  122. package/resources/admin/organization/projects/service-accounts/service-accounts.mjs.map +1 -0
  123. package/resources/admin/organization/projects/service-accounts.d.mts +1 -167
  124. package/resources/admin/organization/projects/service-accounts.d.mts.map +1 -1
  125. package/resources/admin/organization/projects/service-accounts.d.ts +1 -167
  126. package/resources/admin/organization/projects/service-accounts.d.ts.map +1 -1
  127. package/resources/admin/organization/projects/service-accounts.js +2 -97
  128. package/resources/admin/organization/projects/service-accounts.js.map +1 -1
  129. package/resources/admin/organization/projects/service-accounts.mjs +1 -95
  130. package/resources/admin/organization/projects/service-accounts.mjs.map +1 -1
  131. package/resources/admin/organization/projects/spend-limit.d.mts +115 -0
  132. package/resources/admin/organization/projects/spend-limit.d.mts.map +1 -0
  133. package/resources/admin/organization/projects/spend-limit.d.ts +115 -0
  134. package/resources/admin/organization/projects/spend-limit.d.ts.map +1 -0
  135. package/resources/admin/organization/projects/spend-limit.js +67 -0
  136. package/resources/admin/organization/projects/spend-limit.js.map +1 -0
  137. package/resources/admin/organization/projects/spend-limit.mjs +63 -0
  138. package/resources/admin/organization/projects/spend-limit.mjs.map +1 -0
  139. package/resources/admin/organization/spend-limit.d.mts +108 -0
  140. package/resources/admin/organization/spend-limit.d.mts.map +1 -0
  141. package/resources/admin/organization/spend-limit.d.ts +108 -0
  142. package/resources/admin/organization/spend-limit.d.ts.map +1 -0
  143. package/resources/admin/organization/spend-limit.js +59 -0
  144. package/resources/admin/organization/spend-limit.js.map +1 -0
  145. package/resources/admin/organization/spend-limit.mjs +55 -0
  146. package/resources/admin/organization/spend-limit.mjs.map +1 -0
  147. package/resources/admin/organization/usage.d.mts +517 -88
  148. package/resources/admin/organization/usage.d.mts.map +1 -1
  149. package/resources/admin/organization/usage.d.ts +517 -88
  150. package/resources/admin/organization/usage.d.ts.map +1 -1
  151. package/resources/beta/responses/input-tokens.d.mts +6 -2
  152. package/resources/beta/responses/input-tokens.d.mts.map +1 -1
  153. package/resources/beta/responses/input-tokens.d.ts +6 -2
  154. package/resources/beta/responses/input-tokens.d.ts.map +1 -1
  155. package/resources/beta/responses/responses.d.mts +25 -15
  156. package/resources/beta/responses/responses.d.mts.map +1 -1
  157. package/resources/beta/responses/responses.d.ts +25 -15
  158. package/resources/beta/responses/responses.d.ts.map +1 -1
  159. package/resources/beta/responses/responses.js.map +1 -1
  160. package/resources/beta/responses/responses.mjs.map +1 -1
  161. package/resources/chat/completions/completions.d.mts +4 -3
  162. package/resources/chat/completions/completions.d.mts.map +1 -1
  163. package/resources/chat/completions/completions.d.ts +4 -3
  164. package/resources/chat/completions/completions.d.ts.map +1 -1
  165. package/resources/chat/completions/completions.js.map +1 -1
  166. package/resources/chat/completions/completions.mjs.map +1 -1
  167. package/resources/responses/input-tokens.d.mts +2 -1
  168. package/resources/responses/input-tokens.d.mts.map +1 -1
  169. package/resources/responses/input-tokens.d.ts +2 -1
  170. package/resources/responses/input-tokens.d.ts.map +1 -1
  171. package/resources/responses/responses.d.mts +9 -8
  172. package/resources/responses/responses.d.mts.map +1 -1
  173. package/resources/responses/responses.d.ts +9 -8
  174. package/resources/responses/responses.d.ts.map +1 -1
  175. package/resources/responses/responses.js.map +1 -1
  176. package/resources/responses/responses.mjs.map +1 -1
  177. package/resources/shared.d.mts +4 -1
  178. package/resources/shared.d.mts.map +1 -1
  179. package/resources/shared.d.ts +4 -1
  180. package/resources/shared.d.ts.map +1 -1
  181. package/src/azure.ts +5 -1
  182. package/src/helpers/standard-schema.ts +570 -0
  183. package/src/helpers/zod.ts +158 -29
  184. package/src/internal/headers.ts +7 -3
  185. package/src/lib/AbstractChatCompletionRunner.ts +43 -1
  186. package/src/lib/ChatCompletionStream.ts +56 -8
  187. package/src/lib/ResponsesParser.ts +7 -5
  188. package/src/lib/jsonschema.ts +3 -0
  189. package/src/lib/transform.ts +1930 -103
  190. package/src/resources/admin/organization/audit-logs.ts +169 -1
  191. package/src/resources/admin/organization/index.ts +6 -0
  192. package/src/resources/admin/organization/organization.ts +16 -0
  193. package/src/resources/admin/organization/projects/index.ts +7 -1
  194. package/src/resources/admin/organization/projects/projects.ts +29 -13
  195. package/src/resources/admin/organization/projects/service-accounts/api-keys.ts +80 -0
  196. package/src/resources/admin/organization/projects/service-accounts/index.ts +15 -0
  197. package/src/resources/admin/organization/projects/service-accounts/service-accounts.ts +284 -0
  198. package/src/resources/admin/organization/projects/service-accounts.ts +1 -264
  199. package/src/resources/admin/organization/projects/spend-limit.ts +156 -0
  200. package/src/resources/admin/organization/spend-limit.ts +144 -0
  201. package/src/resources/admin/organization/usage.ts +616 -88
  202. package/src/resources/beta/responses/input-tokens.ts +6 -2
  203. package/src/resources/beta/responses/responses.ts +25 -14
  204. package/src/resources/chat/completions/completions.ts +4 -3
  205. package/src/resources/responses/input-tokens.ts +2 -1
  206. package/src/resources/responses/responses.ts +9 -7
  207. package/src/resources/shared.ts +4 -1
  208. package/src/version.ts +1 -1
  209. package/version.d.mts +1 -1
  210. package/version.d.ts +1 -1
  211. package/version.js +1 -1
  212. package/version.mjs +1 -1
@@ -0,0 +1,570 @@
1
+ import { OpenAIError } from '../error';
2
+ import {
3
+ AutoParseableResponseFormat,
4
+ AutoParseableTextFormat,
5
+ AutoParseableTool,
6
+ makeParseableResponseFormat,
7
+ makeParseableTextFormat,
8
+ makeParseableTool,
9
+ } from '../lib/parser';
10
+ import { AutoParseableResponseTool, makeParseableResponseTool } from '../lib/ResponsesParser';
11
+ import { type JSONSchema } from '../lib/jsonschema';
12
+ import {
13
+ assertNoNestedSchemaIds,
14
+ forEachJSONSchemaChild,
15
+ hasOnlyRefAndAnnotations,
16
+ normalizeObjectAllOfForExclusivity,
17
+ resolveLocalRef,
18
+ rewriteLocalRefsIntoMovedOneOfBranches,
19
+ toStrictJsonSchema,
20
+ } from '../lib/transform';
21
+ import { ResponseFormatJSONSchema } from '../resources/index';
22
+ import { type ResponseFormatTextJSONSchemaConfig } from '../resources/responses/responses';
23
+
24
+ type StandardSchemaIssue = {
25
+ readonly message: string;
26
+ readonly path?: ReadonlyArray<PropertyKey | { readonly key: PropertyKey }> | undefined;
27
+ };
28
+
29
+ type StandardSchemaResult<Output> =
30
+ | {
31
+ readonly value: Output;
32
+ readonly issues?: undefined;
33
+ }
34
+ | {
35
+ readonly issues: ReadonlyArray<StandardSchemaIssue>;
36
+ };
37
+
38
+ type StandardJSONSchemaOptions = {
39
+ readonly target: 'draft-07';
40
+ readonly libraryOptions?: Record<string, unknown> | undefined;
41
+ };
42
+
43
+ type StandardSchemaLike<Input = unknown, Output = Input> = {
44
+ readonly '~standard': {
45
+ readonly version: 1;
46
+ readonly vendor: string;
47
+ readonly types?:
48
+ | {
49
+ readonly input: Input;
50
+ readonly output: Output;
51
+ }
52
+ | undefined;
53
+ readonly validate: (
54
+ value: unknown,
55
+ ) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
56
+ readonly jsonSchema?:
57
+ | {
58
+ readonly input: (options: StandardJSONSchemaOptions) => Record<string, unknown>;
59
+ readonly output?: (options: StandardJSONSchemaOptions) => Record<string, unknown>;
60
+ }
61
+ | undefined;
62
+ };
63
+ };
64
+
65
+ type InferStandardOutput<Schema extends StandardSchemaLike> =
66
+ [NonNullable<Schema['~standard']['types']>] extends [never] ? unknown
67
+ : NonNullable<Schema['~standard']['types']> extends { readonly output: infer Output } ? Output
68
+ : unknown;
69
+
70
+ type StandardSchemaJSONSchemaProps = {
71
+ /**
72
+ * A JSON Schema override for Standard Schema implementations that do not
73
+ * expose `~standard.jsonSchema.input()`.
74
+ */
75
+ schema?: JSONSchema | Record<string, unknown> | undefined;
76
+ };
77
+
78
+ type StandardResponseFormatProps = Omit<ResponseFormatJSONSchema.JSONSchema, 'schema' | 'strict' | 'name'> &
79
+ StandardSchemaJSONSchemaProps;
80
+
81
+ type StandardTextFormatProps = Omit<
82
+ ResponseFormatTextJSONSchemaConfig,
83
+ 'schema' | 'type' | 'strict' | 'name'
84
+ > &
85
+ StandardSchemaJSONSchemaProps;
86
+
87
+ type StandardToolFunction<Parameters extends StandardSchemaLike> = (
88
+ args: InferStandardOutput<Parameters>,
89
+ ) => unknown | Promise<unknown>;
90
+
91
+ type StandardToolOptions<Parameters extends StandardSchemaLike> = {
92
+ name: string;
93
+ parameters: Parameters;
94
+ /**
95
+ * A JSON Schema override for Standard Schema implementations that do not
96
+ * expose `~standard.jsonSchema.input()`.
97
+ */
98
+ schema?: JSONSchema | Record<string, unknown> | undefined;
99
+ function?: StandardToolFunction<Parameters> | undefined;
100
+ description?: string | undefined;
101
+ };
102
+
103
+ type StandardToolReturnOptions<
104
+ Parameters extends StandardSchemaLike,
105
+ Function extends StandardToolFunction<Parameters> | undefined,
106
+ > = {
107
+ arguments: InferStandardOutput<Parameters>;
108
+ name: string;
109
+ function: Function;
110
+ };
111
+
112
+ function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
113
+ return typeof value === 'object' && value !== null && 'then' in value && typeof value.then === 'function';
114
+ }
115
+
116
+ function formatStandardSchemaIssues(issues: ReadonlyArray<StandardSchemaIssue>): string {
117
+ return issues
118
+ .map((issue) => {
119
+ const path = issue.path
120
+ ?.map((segment) =>
121
+ typeof segment === 'object' && segment !== null && 'key' in segment ? segment.key : segment,
122
+ )
123
+ .map(String)
124
+ .join('.');
125
+ return path ? `${path}: ${issue.message}` : issue.message;
126
+ })
127
+ .join('; ');
128
+ }
129
+
130
+ const JSON_SCHEMA_TYPES = new Set(['string', 'number', 'integer', 'boolean', 'object', 'array', 'null']);
131
+
132
+ type JSONPrimitive = string | number | boolean | null;
133
+
134
+ function getSchemaTypes(schema: unknown): Set<string> | undefined {
135
+ if (!schema || typeof schema !== 'object' || Array.isArray(schema)) return undefined;
136
+
137
+ const type = (schema as Record<string, unknown>)['type'];
138
+ if (type === undefined) {
139
+ return getLiteralSchemaTypes(schema);
140
+ }
141
+ const types = Array.isArray(type) ? type : [type];
142
+ if (
143
+ types.length === 0 ||
144
+ !types.every((value) => typeof value === 'string' && JSON_SCHEMA_TYPES.has(value))
145
+ ) {
146
+ return undefined;
147
+ }
148
+
149
+ return new Set(types);
150
+ }
151
+
152
+ function isJSONPrimitive(value: unknown): value is JSONPrimitive {
153
+ return (
154
+ value === null ||
155
+ typeof value === 'string' ||
156
+ typeof value === 'boolean' ||
157
+ (typeof value === 'number' && Number.isFinite(value))
158
+ );
159
+ }
160
+
161
+ function getLiteralValues(schema: unknown): JSONPrimitive[] | undefined {
162
+ if (!schema || typeof schema !== 'object' || Array.isArray(schema)) return undefined;
163
+
164
+ const record = schema as Record<string, unknown>;
165
+ if ('const' in record && isJSONPrimitive(record['const'])) {
166
+ return [record['const']];
167
+ }
168
+
169
+ const enumValues = record['enum'];
170
+ if (Array.isArray(enumValues) && enumValues.length > 0 && enumValues.every(isJSONPrimitive)) {
171
+ return enumValues;
172
+ }
173
+
174
+ return undefined;
175
+ }
176
+
177
+ function getLiteralSchemaTypes(schema: unknown): Set<string> | undefined {
178
+ const literalValues = getLiteralValues(schema);
179
+ if (!literalValues) return undefined;
180
+
181
+ return new Set(
182
+ literalValues.map((value) => {
183
+ if (value === null) return 'null';
184
+ return typeof value;
185
+ }),
186
+ );
187
+ }
188
+
189
+ function haveDisjointLiteralValues(left: unknown, right: unknown): boolean {
190
+ const leftValues = getLiteralValues(left);
191
+ const rightValues = getLiteralValues(right);
192
+ if (!leftValues || !rightValues) return false;
193
+
194
+ return leftValues.every((leftValue) => !rightValues.some((rightValue) => leftValue === rightValue));
195
+ }
196
+
197
+ function schemaTypesOverlap(left: string, right: string): boolean {
198
+ return (
199
+ left === right || (left === 'integer' && right === 'number') || (left === 'number' && right === 'integer')
200
+ );
201
+ }
202
+
203
+ function isObjectOnlySchema(schema: unknown): boolean {
204
+ const types = getSchemaTypes(schema);
205
+ return types?.size === 1 && types.has('object');
206
+ }
207
+
208
+ function haveDisjointObjectDiscriminator(left: unknown, right: unknown, root: JSONSchema): boolean {
209
+ if (!isObjectOnlySchema(left) || !isObjectOnlySchema(right)) return false;
210
+
211
+ const leftRecord = left as Record<string, unknown>;
212
+ const rightRecord = right as Record<string, unknown>;
213
+ const leftProperties = leftRecord['properties'];
214
+ const rightProperties = rightRecord['properties'];
215
+ const leftRequired = leftRecord['required'];
216
+ const rightRequired = rightRecord['required'];
217
+ if (
218
+ !leftProperties ||
219
+ typeof leftProperties !== 'object' ||
220
+ Array.isArray(leftProperties) ||
221
+ !rightProperties ||
222
+ typeof rightProperties !== 'object' ||
223
+ Array.isArray(rightProperties) ||
224
+ !Array.isArray(leftRequired) ||
225
+ !Array.isArray(rightRequired)
226
+ ) {
227
+ return false;
228
+ }
229
+
230
+ for (const property of leftRequired) {
231
+ if (
232
+ typeof property === 'string' &&
233
+ rightRequired.includes(property) &&
234
+ haveDisjointLiteralValues(
235
+ resolveLocalRefForExclusivity((leftProperties as Record<string, unknown>)[property], root),
236
+ resolveLocalRefForExclusivity((rightProperties as Record<string, unknown>)[property], root),
237
+ )
238
+ ) {
239
+ return true;
240
+ }
241
+ }
242
+
243
+ return false;
244
+ }
245
+
246
+ function getClosedObjectPropertySet(
247
+ schema: unknown,
248
+ ): { properties: Set<string>; required: string[] } | undefined {
249
+ if (!isObjectOnlySchema(schema)) return undefined;
250
+
251
+ const record = schema as Record<string, unknown>;
252
+ const properties = record['properties'];
253
+ const required = record['required'];
254
+ if (
255
+ record['additionalProperties'] !== false ||
256
+ !properties ||
257
+ typeof properties !== 'object' ||
258
+ Array.isArray(properties) ||
259
+ !Array.isArray(required) ||
260
+ required.some((property) => typeof property !== 'string')
261
+ ) {
262
+ return undefined;
263
+ }
264
+
265
+ const propertySet = new Set(Object.keys(properties));
266
+ const requiredProperties = required as string[];
267
+ // A required undeclared property makes a closed branch unsatisfiable, but
268
+ // the strictifier rejects that shape rather than representing it. Keep this
269
+ // exclusivity proof conservative and let the normal validation path fail.
270
+ if (requiredProperties.some((property) => !propertySet.has(property))) {
271
+ return undefined;
272
+ }
273
+
274
+ return { properties: propertySet, required: requiredProperties };
275
+ }
276
+
277
+ function haveDisjointClosedObjectPropertySets(left: unknown, right: unknown): boolean {
278
+ const leftShape = getClosedObjectPropertySet(left);
279
+ const rightShape = getClosedObjectPropertySet(right);
280
+ if (!leftShape || !rightShape) return false;
281
+
282
+ // If either closed branch requires a property the other branch does not
283
+ // declare, every instance satisfying the first is rejected by the second as
284
+ // an additional property. This proves oneOf exclusivity without widening
285
+ // overlapping closed shapes.
286
+ return (
287
+ leftShape.required.some((property) => !rightShape.properties.has(property)) ||
288
+ rightShape.required.some((property) => !leftShape.properties.has(property))
289
+ );
290
+ }
291
+
292
+ function areMutuallyExclusive(left: unknown, right: unknown, root: JSONSchema): boolean {
293
+ const leftTypes = getSchemaTypes(left);
294
+ const rightTypes = getSchemaTypes(right);
295
+ if (
296
+ leftTypes &&
297
+ rightTypes &&
298
+ [...leftTypes].every((leftType) =>
299
+ [...rightTypes].every((rightType) => !schemaTypesOverlap(leftType, rightType)),
300
+ )
301
+ ) {
302
+ return true;
303
+ }
304
+
305
+ return (
306
+ haveDisjointLiteralValues(left, right) ||
307
+ haveDisjointObjectDiscriminator(left, right, root) ||
308
+ haveDisjointClosedObjectPropertySets(left, right)
309
+ );
310
+ }
311
+
312
+ function resolveLocalRefForExclusivity(
313
+ schema: unknown,
314
+ root: JSONSchema,
315
+ seenRefs: Set<string> = new Set(),
316
+ ): unknown | undefined {
317
+ if (!schema || typeof schema !== 'object' || Array.isArray(schema)) return schema;
318
+
319
+ const record = schema as Record<string, unknown>;
320
+ const ref = record['$ref'];
321
+ if (ref !== undefined) {
322
+ // Annotation keywords do not affect Draft 7 validation, so they are safe
323
+ // to retain while proving the referenced branches are mutually exclusive.
324
+ // Keep the proof conservative for every other sibling constraint.
325
+ if (typeof ref !== 'string' || !hasOnlyRefAndAnnotations(record as JSONSchema)) {
326
+ return undefined;
327
+ }
328
+ if (seenRefs.has(ref)) return undefined;
329
+
330
+ const resolved = resolveLocalRef(root, ref);
331
+ if (resolved === undefined) return undefined;
332
+
333
+ return resolveLocalRefForExclusivity(resolved, root, new Set([...seenRefs, ref]));
334
+ }
335
+
336
+ if (record['allOf'] !== undefined) {
337
+ if (!Array.isArray(record['allOf'])) return undefined;
338
+ const normalized = normalizeObjectAllOfForExclusivity(record as JSONSchema, root);
339
+ if (normalized === undefined) return undefined;
340
+
341
+ // Flattening a singleton allOf can expose a bare local ref. Feed that
342
+ // result through this same resolver so URI-fragment decoding and the
343
+ // existing local-ref cycle guard still apply before exclusivity analysis.
344
+ return resolveLocalRefForExclusivity(normalized, root, seenRefs);
345
+ }
346
+
347
+ return schema;
348
+ }
349
+
350
+ function areOneOfBranchesMutuallyExclusive(branches: unknown[], root: JSONSchema): boolean {
351
+ for (let index = 0; index < branches.length; index++) {
352
+ for (let otherIndex = index + 1; otherIndex < branches.length; otherIndex++) {
353
+ const left = resolveLocalRefForExclusivity(branches[index], root);
354
+ const right = resolveLocalRefForExclusivity(branches[otherIndex], root);
355
+ if (left === undefined || right === undefined || !areMutuallyExclusive(left, right, root)) {
356
+ return false;
357
+ }
358
+ }
359
+ }
360
+
361
+ return true;
362
+ }
363
+
364
+ function normalizeStructuredOutputSchema(schema: JSONSchema): JSONSchema {
365
+ assertNoNestedSchemaIds(schema);
366
+ const normalizedSchema = structuredClone(schema);
367
+ const oneOfSchemas: Record<string, unknown>[] = [];
368
+ const visitedSchemas = new Set<Record<string, unknown>>();
369
+
370
+ const visitSchema = (value: unknown): void => {
371
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return;
372
+ const record = value as Record<string, unknown>;
373
+ if (visitedSchemas.has(record)) return;
374
+ visitedSchemas.add(record);
375
+
376
+ if (record['oneOf'] !== undefined) {
377
+ if (!Array.isArray(record['oneOf'])) {
378
+ throw new OpenAIError(
379
+ 'Standard JSON Schema generated an invalid `oneOf`, which cannot be represented in an OpenAI strict schema',
380
+ );
381
+ }
382
+ if (record['anyOf'] !== undefined) {
383
+ throw new OpenAIError(
384
+ 'Standard JSON Schema generated both `anyOf` and `oneOf`, which cannot be represented in an OpenAI strict schema',
385
+ );
386
+ }
387
+ // `false` can never validate, so it cannot overlap another oneOf
388
+ // branch. Keep it in place until the existing anyOf normalization runs
389
+ // so local refs into surviving branch indices can be rewritten before
390
+ // the impossible alternatives are removed.
391
+ const possibleBranches = record['oneOf'].filter((branch) => branch !== false);
392
+ if (!areOneOfBranchesMutuallyExclusive(possibleBranches, normalizedSchema)) {
393
+ throw new OpenAIError(
394
+ 'Standard JSON Schema generated a `oneOf` whose branches are not provably mutually exclusive. OpenAI strict schemas do not support `oneOf`; use `anyOf` or add a discriminator with distinct literal values.',
395
+ );
396
+ }
397
+ oneOfSchemas.push(record);
398
+ }
399
+
400
+ forEachJSONSchemaChild(record, [], (child) => visitSchema(child));
401
+ };
402
+
403
+ visitSchema(normalizedSchema);
404
+ rewriteLocalRefsIntoMovedOneOfBranches(normalizedSchema);
405
+ for (const record of oneOfSchemas) {
406
+ record['anyOf'] = record['oneOf'];
407
+ delete record['oneOf'];
408
+ }
409
+ return normalizedSchema;
410
+ }
411
+
412
+ function parseStandardSchema<Schema extends StandardSchemaLike>(
413
+ standardSchema: Schema,
414
+ content: string,
415
+ ): InferStandardOutput<Schema> {
416
+ const result = standardSchema['~standard'].validate(JSON.parse(content));
417
+
418
+ if (isPromiseLike(result)) {
419
+ void Promise.resolve(result).catch(() => undefined);
420
+ throw new OpenAIError(
421
+ 'Standard Schema helpers only support synchronous validation. Use a schema with a synchronous `~standard.validate()` implementation.',
422
+ );
423
+ }
424
+
425
+ if (result.issues) {
426
+ throw new OpenAIError(`Standard Schema validation failed: ${formatStandardSchemaIssues(result.issues)}`);
427
+ }
428
+
429
+ return result.value as InferStandardOutput<Schema>;
430
+ }
431
+
432
+ function resolveStandardJSONSchema(
433
+ standardSchema: StandardSchemaLike,
434
+ schemaOverride?: JSONSchema | Record<string, unknown> | undefined,
435
+ ): Record<string, unknown> {
436
+ const schema = (schemaOverride ?? standardSchema['~standard'].jsonSchema?.input({ target: 'draft-07' })) as
437
+ | JSONSchema
438
+ | undefined;
439
+
440
+ if (!schema) {
441
+ throw new OpenAIError(
442
+ 'Standard Schema helpers require a JSON Schema. Pass `schema` or use a schema that implements `~standard.jsonSchema.input()`.',
443
+ );
444
+ }
445
+
446
+ return toStrictJsonSchema(normalizeStructuredOutputSchema(schema)) as unknown as Record<string, unknown>;
447
+ }
448
+
449
+ /**
450
+ * Creates a chat completion `JSONSchema` response format from a Standard
451
+ * Schema validator.
452
+ *
453
+ * The helper uses `~standard.jsonSchema.input()` for the model-facing schema
454
+ * and `~standard.validate()` for parsed output. Validation must be
455
+ * synchronous because the SDK's parse helpers are synchronous.
456
+ */
457
+ export function standardResponseFormat<Schema extends StandardSchemaLike>(
458
+ standardSchema: Schema,
459
+ name: string,
460
+ props?: StandardResponseFormatProps,
461
+ ): AutoParseableResponseFormat<InferStandardOutput<Schema>> {
462
+ const { schema, ...formatProps } = props ?? {};
463
+
464
+ return makeParseableResponseFormat<InferStandardOutput<Schema>>(
465
+ {
466
+ type: 'json_schema',
467
+ json_schema: {
468
+ ...formatProps,
469
+ name,
470
+ strict: true,
471
+ schema: resolveStandardJSONSchema(standardSchema, schema),
472
+ },
473
+ },
474
+ (content) => parseStandardSchema(standardSchema, content),
475
+ );
476
+ }
477
+
478
+ /**
479
+ * Creates a Responses API `json_schema` text format from a Standard Schema
480
+ * validator.
481
+ */
482
+ export function standardTextFormat<Schema extends StandardSchemaLike>(
483
+ standardSchema: Schema,
484
+ name: string,
485
+ props?: StandardTextFormatProps,
486
+ ): AutoParseableTextFormat<InferStandardOutput<Schema>> {
487
+ const { schema, ...formatProps } = props ?? {};
488
+
489
+ return makeParseableTextFormat<InferStandardOutput<Schema>>(
490
+ {
491
+ type: 'json_schema',
492
+ ...formatProps,
493
+ name,
494
+ strict: true,
495
+ schema: resolveStandardJSONSchema(standardSchema, schema),
496
+ },
497
+ (content) => parseStandardSchema(standardSchema, content),
498
+ );
499
+ }
500
+
501
+ /**
502
+ * Creates a chat completion `function` tool from a Standard Schema
503
+ * validator.
504
+ */
505
+ export function standardFunction<
506
+ Parameters extends StandardSchemaLike,
507
+ Function extends StandardToolFunction<Parameters>,
508
+ >(
509
+ options: StandardToolOptions<Parameters> & { function: Function },
510
+ ): AutoParseableTool<StandardToolReturnOptions<Parameters, Function>>;
511
+ export function standardFunction<Parameters extends StandardSchemaLike>(
512
+ options: StandardToolOptions<Parameters> & { function?: undefined },
513
+ ): AutoParseableTool<StandardToolReturnOptions<Parameters, undefined>>;
514
+ export function standardFunction<Parameters extends StandardSchemaLike>(
515
+ options: StandardToolOptions<Parameters>,
516
+ ): AutoParseableTool<StandardToolReturnOptions<Parameters, StandardToolFunction<Parameters> | undefined>>;
517
+ export function standardFunction<Parameters extends StandardSchemaLike>(
518
+ options: StandardToolOptions<Parameters>,
519
+ ) {
520
+ return makeParseableTool<any>(
521
+ {
522
+ type: 'function',
523
+ function: {
524
+ name: options.name,
525
+ parameters: resolveStandardJSONSchema(options.parameters, options.schema),
526
+ strict: true,
527
+ ...(options.description ? { description: options.description } : undefined),
528
+ },
529
+ },
530
+ {
531
+ callback: options.function,
532
+ parser: (args) => parseStandardSchema(options.parameters, args),
533
+ },
534
+ );
535
+ }
536
+
537
+ /**
538
+ * Creates a Responses API `function` tool from a Standard Schema validator.
539
+ */
540
+ export function standardResponsesFunction<
541
+ Parameters extends StandardSchemaLike,
542
+ Function extends StandardToolFunction<Parameters>,
543
+ >(
544
+ options: StandardToolOptions<Parameters> & { function: Function },
545
+ ): AutoParseableResponseTool<StandardToolReturnOptions<Parameters, Function>>;
546
+ export function standardResponsesFunction<Parameters extends StandardSchemaLike>(
547
+ options: StandardToolOptions<Parameters> & { function?: undefined },
548
+ ): AutoParseableResponseTool<StandardToolReturnOptions<Parameters, undefined>>;
549
+ export function standardResponsesFunction<Parameters extends StandardSchemaLike>(
550
+ options: StandardToolOptions<Parameters>,
551
+ ): AutoParseableResponseTool<
552
+ StandardToolReturnOptions<Parameters, StandardToolFunction<Parameters> | undefined>
553
+ >;
554
+ export function standardResponsesFunction<Parameters extends StandardSchemaLike>(
555
+ options: StandardToolOptions<Parameters>,
556
+ ) {
557
+ return makeParseableResponseTool<any>(
558
+ {
559
+ type: 'function',
560
+ name: options.name,
561
+ parameters: resolveStandardJSONSchema(options.parameters, options.schema),
562
+ strict: true,
563
+ ...(options.description ? { description: options.description } : undefined),
564
+ },
565
+ {
566
+ callback: options.function,
567
+ parser: (args) => parseStandardSchema(options.parameters, args),
568
+ },
569
+ );
570
+ }