@tanstack/openai-base 0.9.1 → 0.9.2
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.
|
@@ -22,13 +22,18 @@ export declare function stripUnsupportedFormats(node: any): any;
|
|
|
22
22
|
*/
|
|
23
23
|
export declare function makeStructuredOutputCompatible(schema: Record<string, any>, originalRequired?: Array<string>): Record<string, any>;
|
|
24
24
|
/**
|
|
25
|
-
* Returns `false` when `schema`
|
|
26
|
-
*
|
|
27
|
-
* made strict-compatible and must be sent with `strict: false`.
|
|
25
|
+
* Returns `false` when `schema` cannot be made strict-compatible and must be
|
|
26
|
+
* sent with `strict: false`. Two ways that happens:
|
|
28
27
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
28
|
+
* 1. It uses a JSON-Schema keyword outside OpenAI's strict subset anywhere in
|
|
29
|
+
* the tree (`oneOf`/`allOf`/`not`/`$ref`/`$defs`).
|
|
30
|
+
* 2. It contains a *typeless* schema node — a property/items/anyOf entry with
|
|
31
|
+
* no `type` (nor `enum`/`const`/combinator), e.g. the `{}` that `z.any()`
|
|
32
|
+
* produces. Strict mode rejects typeless schemas.
|
|
33
|
+
*
|
|
34
|
+
* Conservative by design: for (1) keywords are matched as object keys, so a
|
|
35
|
+
* property literally named e.g. `oneOf` also trips it. That only costs that one
|
|
36
|
+
* tool its strict mode, which is strictly safer than a false "compatible"
|
|
37
|
+
* verdict that 400s the whole request.
|
|
33
38
|
*/
|
|
34
39
|
export declare function isStrictModeCompatible(schema: unknown): boolean;
|
|
@@ -32,8 +32,17 @@ const STRICT_UNSUPPORTED_KEYWORDS = [
|
|
|
32
32
|
"$defs",
|
|
33
33
|
"definitions"
|
|
34
34
|
];
|
|
35
|
+
const TYPE_INDICATOR_KEYWORDS = [
|
|
36
|
+
"type",
|
|
37
|
+
"enum",
|
|
38
|
+
"const",
|
|
39
|
+
"anyOf",
|
|
40
|
+
"oneOf",
|
|
41
|
+
"allOf",
|
|
42
|
+
"$ref"
|
|
43
|
+
];
|
|
35
44
|
function isStrictModeCompatible(schema) {
|
|
36
|
-
return !containsStrictUnsupportedKeyword(schema);
|
|
45
|
+
return !containsStrictUnsupportedKeyword(schema) && !containsTypelessSchema(schema);
|
|
37
46
|
}
|
|
38
47
|
function containsStrictUnsupportedKeyword(node) {
|
|
39
48
|
if (Array.isArray(node)) {
|
|
@@ -46,6 +55,33 @@ function containsStrictUnsupportedKeyword(node) {
|
|
|
46
55
|
}
|
|
47
56
|
return false;
|
|
48
57
|
}
|
|
58
|
+
function isTypelessSchema(node) {
|
|
59
|
+
if (node === null || typeof node !== "object" || Array.isArray(node)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return !TYPE_INDICATOR_KEYWORDS.some((key) => key in node);
|
|
63
|
+
}
|
|
64
|
+
function containsTypelessSchema(node) {
|
|
65
|
+
if (node === null || typeof node !== "object" || Array.isArray(node)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
const schema = node;
|
|
69
|
+
const children = [];
|
|
70
|
+
if (schema.properties && typeof schema.properties === "object") {
|
|
71
|
+
children.push(...Object.values(schema.properties));
|
|
72
|
+
}
|
|
73
|
+
if (schema.items !== void 0) {
|
|
74
|
+
children.push(
|
|
75
|
+
...Array.isArray(schema.items) ? schema.items : [schema.items]
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(schema.anyOf)) {
|
|
79
|
+
children.push(...schema.anyOf);
|
|
80
|
+
}
|
|
81
|
+
return children.some(
|
|
82
|
+
(child) => isTypelessSchema(child) || containsTypelessSchema(child)
|
|
83
|
+
);
|
|
84
|
+
}
|
|
49
85
|
function coerceStrictSchema(schema, originalRequired) {
|
|
50
86
|
const result = { ...schema };
|
|
51
87
|
const required = originalRequired ?? (Array.isArray(result["required"]) ? result["required"] : []);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-converter.js","sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["/**\n * String `format` values accepted by OpenAI's strict Structured Outputs subset.\n * Any other format (e.g. \"uri\", \"uri-reference\", \"regex\") causes the API to\n * reject the whole request with `400 ... '<format>' is not a valid format`.\n * MCP servers and hand-written tools routinely declare such formats, so we strip\n * the unsupported ones before sending. See:\n * https://platform.openai.com/docs/guides/structured-outputs#supported-properties\n */\nconst SUPPORTED_STRING_FORMATS = new Set([\n 'date-time',\n 'time',\n 'date',\n 'duration',\n 'email',\n 'hostname',\n 'ipv4',\n 'ipv6',\n 'uuid',\n])\n\n/**\n * Recursively drop JSON-Schema `format` keywords whose value isn't in OpenAI's\n * strict-mode allowlist. Pure — returns a fresh tree and never mutates `node`,\n * so the caller's original tool definition is left intact.\n *\n * A property *named* `format` always has a schema (object/boolean) value, never\n * a bare string, so it is preserved and recursed into; only the `format`\n * *keyword* (whose value is a string) is subject to removal.\n */\nexport function stripUnsupportedFormats(node: any): any {\n if (Array.isArray(node)) return node.map(stripUnsupportedFormats)\n if (node === null || typeof node !== 'object') return node\n\n const out: Record<string, any> = {}\n for (const [key, value] of Object.entries(node)) {\n if (\n key === 'format' &&\n typeof value === 'string' &&\n !SUPPORTED_STRING_FORMATS.has(value)\n ) {\n continue\n }\n out[key] = stripUnsupportedFormats(value)\n }\n return out\n}\n\n/**\n * Transform a JSON schema to be compatible with OpenAI's structured output requirements.\n * OpenAI requires:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for objects\n * - String `format` keywords must be from a fixed allowlist (others are stripped)\n *\n * @param schema - JSON schema to transform\n * @param originalRequired - Original required array (to know which fields were optional)\n * @returns Transformed schema compatible with OpenAI structured output\n */\nexport function makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n return stripUnsupportedFormats(coerceStrictSchema(schema, originalRequired))\n}\n\n/**\n * JSON-Schema keywords outside OpenAI's strict Structured Outputs subset. A\n * schema using any of these can't be coerced into a strict-valid shape, and\n * sending it with `strict: true` makes the API reject the ENTIRE request\n * (e.g. `400 Invalid schema ... 'additionalProperties' is required to be ...`).\n * Tools with such schemas are emitted with `strict: false` instead (see the\n * tool converters) so they remain callable. MCP servers (e.g. Notion) routinely\n * emit these.\n *\n * - `oneOf` / `allOf` / `not` — combinator keywords strict mode rejects\n * - `$ref` / `$defs` / `definitions` — references and definition pools whose\n * object subschemas escape the `additionalProperties: false` normalization\n * strict mode requires\n */\nconst STRICT_UNSUPPORTED_KEYWORDS: ReadonlyArray<string> = [\n 'oneOf',\n 'allOf',\n 'not',\n '$ref',\n '$defs',\n 'definitions',\n]\n\n/**\n * Returns `false` when `schema` (anywhere in the tree) uses a JSON-Schema\n * keyword outside OpenAI's strict Structured Outputs subset — i.e. it cannot be\n * made strict-compatible and must be sent with `strict: false`.\n *\n * Conservative by design: keywords are matched as object keys, so a property\n * literally named e.g. `oneOf` also trips it. That only costs that one tool its\n * strict mode, which is strictly safer than a false \"compatible\" verdict that\n * 400s the whole request.\n */\nexport function isStrictModeCompatible(schema: unknown): boolean {\n return !containsStrictUnsupportedKeyword(schema)\n}\n\nfunction containsStrictUnsupportedKeyword(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsStrictUnsupportedKeyword)\n }\n if (node === null || typeof node !== 'object') return false\n for (const [key, value] of Object.entries(node)) {\n if (STRICT_UNSUPPORTED_KEYWORDS.includes(key)) return true\n if (containsStrictUnsupportedKeyword(value)) return true\n }\n return false\n}\n\n/**\n * Strict-mode structural rewrite (required widening, nullability,\n * additionalProperties). Kept private so the public entry point can apply the\n * format-stripping pass exactly once over the fully-rewritten tree.\n */\nfunction coerceStrictSchema(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n const result = { ...schema }\n const required =\n originalRequired ??\n (Array.isArray(result['required']) ? result['required'] : [])\n\n if (result.type === 'object' && result.properties) {\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n for (const propName of allPropertyNames) {\n let prop = properties[propName]\n const wasOptional = !required.includes(propName)\n\n // Step 1: Recurse into nested structures\n if (prop.type === 'object' && prop.properties) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.type === 'array' && prop.items) {\n prop = {\n ...prop,\n items: coerceStrictSchema(prop.items, prop.items.required || []),\n }\n } else if (prop.anyOf) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n // Step 2: Apply null-widening for optional properties (after recursion)\n if (wasOptional) {\n if (prop.anyOf) {\n // For anyOf, add a null variant if not already present\n if (!prop.anyOf.some((v: any) => v.type === 'null')) {\n prop = { ...prop, anyOf: [...prop.anyOf, { type: 'null' }] }\n }\n } else if (prop.type && !Array.isArray(prop.type)) {\n prop = { ...prop, type: [prop.type, 'null'] }\n } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {\n prop = { ...prop, type: [...prop.type, 'null'] }\n }\n }\n\n properties[propName] = prop\n }\n\n result.properties = properties\n result.required = allPropertyNames\n result.additionalProperties = false\n }\n\n if (result.type === 'array' && result.items) {\n result.items = coerceStrictSchema(result.items, result.items.required || [])\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n result.anyOf = result.anyOf.map((variant) =>\n coerceStrictSchema(variant, variant.required || []),\n )\n }\n\n if (result.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n return result\n}\n"],"names":[],"mappings":"AAQA,MAAM,+CAA+B,IAAI;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAWM,SAAS,wBAAwB,MAAgB;AACtD,MAAI,MAAM,QAAQ,IAAI,EAAG,QAA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,MAAI,OAAO,SAAS,MAAM,QAAQ,OAAO,KAAK,GAAG;AAC/C,WAAO,QAAQ,OAAO,MAAM;AAAA,MAAI,CAAC,YAC/B,mBAAmB,SAAS,QAAQ,YAAY,CAAA,CAAE;AAAA,IAAA;AAAA,EAEtD;AAEA,MAAI,OAAO,OAAO;AAChB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"schema-converter.js","sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["/**\n * String `format` values accepted by OpenAI's strict Structured Outputs subset.\n * Any other format (e.g. \"uri\", \"uri-reference\", \"regex\") causes the API to\n * reject the whole request with `400 ... '<format>' is not a valid format`.\n * MCP servers and hand-written tools routinely declare such formats, so we strip\n * the unsupported ones before sending. See:\n * https://platform.openai.com/docs/guides/structured-outputs#supported-properties\n */\nconst SUPPORTED_STRING_FORMATS = new Set([\n 'date-time',\n 'time',\n 'date',\n 'duration',\n 'email',\n 'hostname',\n 'ipv4',\n 'ipv6',\n 'uuid',\n])\n\n/**\n * Recursively drop JSON-Schema `format` keywords whose value isn't in OpenAI's\n * strict-mode allowlist. Pure — returns a fresh tree and never mutates `node`,\n * so the caller's original tool definition is left intact.\n *\n * A property *named* `format` always has a schema (object/boolean) value, never\n * a bare string, so it is preserved and recursed into; only the `format`\n * *keyword* (whose value is a string) is subject to removal.\n */\nexport function stripUnsupportedFormats(node: any): any {\n if (Array.isArray(node)) return node.map(stripUnsupportedFormats)\n if (node === null || typeof node !== 'object') return node\n\n const out: Record<string, any> = {}\n for (const [key, value] of Object.entries(node)) {\n if (\n key === 'format' &&\n typeof value === 'string' &&\n !SUPPORTED_STRING_FORMATS.has(value)\n ) {\n continue\n }\n out[key] = stripUnsupportedFormats(value)\n }\n return out\n}\n\n/**\n * Transform a JSON schema to be compatible with OpenAI's structured output requirements.\n * OpenAI requires:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for objects\n * - String `format` keywords must be from a fixed allowlist (others are stripped)\n *\n * @param schema - JSON schema to transform\n * @param originalRequired - Original required array (to know which fields were optional)\n * @returns Transformed schema compatible with OpenAI structured output\n */\nexport function makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n return stripUnsupportedFormats(coerceStrictSchema(schema, originalRequired))\n}\n\n/**\n * JSON-Schema keywords outside OpenAI's strict Structured Outputs subset. A\n * schema using any of these can't be coerced into a strict-valid shape, and\n * sending it with `strict: true` makes the API reject the ENTIRE request\n * (e.g. `400 Invalid schema ... 'additionalProperties' is required to be ...`).\n * Tools with such schemas are emitted with `strict: false` instead (see the\n * tool converters) so they remain callable. MCP servers (e.g. Notion) routinely\n * emit these.\n *\n * - `oneOf` / `allOf` / `not` — combinator keywords strict mode rejects\n * - `$ref` / `$defs` / `definitions` — references and definition pools whose\n * object subschemas escape the `additionalProperties: false` normalization\n * strict mode requires\n */\nconst STRICT_UNSUPPORTED_KEYWORDS: ReadonlyArray<string> = [\n 'oneOf',\n 'allOf',\n 'not',\n '$ref',\n '$defs',\n 'definitions',\n]\n\n/**\n * Keys that give a schema node a resolvable type under OpenAI's strict subset.\n * A schema-position node carrying none of these is *typeless* (e.g. the empty\n * `{}` that `z.any()` / `z.unknown()` emit). Strict mode requires every schema\n * to declare a type, so a typeless node 400s the whole request — such tools\n * must be sent with `strict: false` instead. (`oneOf`/`allOf`/`$ref` count as\n * type indicators here even though they're independently strict-unsupported;\n * the keyword check below already rejects them.)\n */\nconst TYPE_INDICATOR_KEYWORDS: ReadonlyArray<string> = [\n 'type',\n 'enum',\n 'const',\n 'anyOf',\n 'oneOf',\n 'allOf',\n '$ref',\n]\n\n/**\n * Returns `false` when `schema` cannot be made strict-compatible and must be\n * sent with `strict: false`. Two ways that happens:\n *\n * 1. It uses a JSON-Schema keyword outside OpenAI's strict subset anywhere in\n * the tree (`oneOf`/`allOf`/`not`/`$ref`/`$defs`).\n * 2. It contains a *typeless* schema node — a property/items/anyOf entry with\n * no `type` (nor `enum`/`const`/combinator), e.g. the `{}` that `z.any()`\n * produces. Strict mode rejects typeless schemas.\n *\n * Conservative by design: for (1) keywords are matched as object keys, so a\n * property literally named e.g. `oneOf` also trips it. That only costs that one\n * tool its strict mode, which is strictly safer than a false \"compatible\"\n * verdict that 400s the whole request.\n */\nexport function isStrictModeCompatible(schema: unknown): boolean {\n return (\n !containsStrictUnsupportedKeyword(schema) && !containsTypelessSchema(schema)\n )\n}\n\nfunction containsStrictUnsupportedKeyword(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsStrictUnsupportedKeyword)\n }\n if (node === null || typeof node !== 'object') return false\n for (const [key, value] of Object.entries(node)) {\n if (STRICT_UNSUPPORTED_KEYWORDS.includes(key)) return true\n if (containsStrictUnsupportedKeyword(value)) return true\n }\n return false\n}\n\n/** A schema-position node that declares no type and so 400s strict mode. */\nfunction isTypelessSchema(node: unknown): boolean {\n if (node === null || typeof node !== 'object' || Array.isArray(node)) {\n // boolean schemas (`true`/`false`) and non-objects aren't typeless props.\n return false\n }\n return !TYPE_INDICATOR_KEYWORDS.some((key) => key in node)\n}\n\n/**\n * Walks the genuine schema positions (property values, `items`, `anyOf`\n * variants) and reports whether any is typeless. Unlike the keyword walk this\n * must respect structure: an empty `{}` is only a problem at a schema position,\n * not e.g. an empty `properties` map.\n */\nfunction containsTypelessSchema(node: unknown): boolean {\n if (node === null || typeof node !== 'object' || Array.isArray(node)) {\n return false\n }\n const schema = node as Record<string, any>\n\n const children: Array<unknown> = []\n if (schema.properties && typeof schema.properties === 'object') {\n children.push(...Object.values(schema.properties))\n }\n if (schema.items !== undefined) {\n children.push(\n ...(Array.isArray(schema.items) ? schema.items : [schema.items]),\n )\n }\n if (Array.isArray(schema.anyOf)) {\n children.push(...schema.anyOf)\n }\n\n return children.some(\n (child) => isTypelessSchema(child) || containsTypelessSchema(child),\n )\n}\n\n/**\n * Strict-mode structural rewrite (required widening, nullability,\n * additionalProperties). Kept private so the public entry point can apply the\n * format-stripping pass exactly once over the fully-rewritten tree.\n */\nfunction coerceStrictSchema(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): Record<string, any> {\n const result = { ...schema }\n const required =\n originalRequired ??\n (Array.isArray(result['required']) ? result['required'] : [])\n\n if (result.type === 'object' && result.properties) {\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n for (const propName of allPropertyNames) {\n let prop = properties[propName]\n const wasOptional = !required.includes(propName)\n\n // Step 1: Recurse into nested structures\n if (prop.type === 'object' && prop.properties) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.type === 'array' && prop.items) {\n prop = {\n ...prop,\n items: coerceStrictSchema(prop.items, prop.items.required || []),\n }\n } else if (prop.anyOf) {\n prop = coerceStrictSchema(prop, prop.required || [])\n } else if (prop.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n // Step 2: Apply null-widening for optional properties (after recursion)\n if (wasOptional) {\n if (prop.anyOf) {\n // For anyOf, add a null variant if not already present\n if (!prop.anyOf.some((v: any) => v.type === 'null')) {\n prop = { ...prop, anyOf: [...prop.anyOf, { type: 'null' }] }\n }\n } else if (prop.type && !Array.isArray(prop.type)) {\n prop = { ...prop, type: [prop.type, 'null'] }\n } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {\n prop = { ...prop, type: [...prop.type, 'null'] }\n }\n }\n\n properties[propName] = prop\n }\n\n result.properties = properties\n result.required = allPropertyNames\n result.additionalProperties = false\n }\n\n if (result.type === 'array' && result.items) {\n result.items = coerceStrictSchema(result.items, result.items.required || [])\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n result.anyOf = result.anyOf.map((variant) =>\n coerceStrictSchema(variant, variant.required || []),\n )\n }\n\n if (result.oneOf) {\n throw new Error(\n 'oneOf is not supported in OpenAI structured output schemas. Check the supported outputs here: https://platform.openai.com/docs/guides/structured-outputs#supported-types',\n )\n }\n\n return result\n}\n"],"names":[],"mappings":"AAQA,MAAM,+CAA+B,IAAI;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAWM,SAAS,wBAAwB,MAAgB;AACtD,MAAI,MAAM,QAAQ,IAAI,EAAG,QAAO,KAAK,IAAI,uBAAuB;AAChE,MAAI,SAAS,QAAQ,OAAO,SAAS,SAAU,QAAO;AAEtD,QAAM,MAA2B,CAAA;AACjC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,IAAI,GAAG;AAC/C,QACE,QAAQ,YACR,OAAO,UAAU,YACjB,CAAC,yBAAyB,IAAI,KAAK,GACnC;AACA;AAAA,IACF;AACA,QAAI,GAAG,IAAI,wBAAwB,KAAK;AAAA,EAC1C;AACA,SAAO;AACT;AAcO,SAAS,+BACd,QACA,kBACqB;AACrB,SAAO,wBAAwB,mBAAmB,QAAQ,gBAAgB,CAAC;AAC7E;AAgBA,MAAM,8BAAqD;AAAA,EACzD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAWA,MAAM,0BAAiD;AAAA,EACrD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAiBO,SAAS,uBAAuB,QAA0B;AAC/D,SACE,CAAC,iCAAiC,MAAM,KAAK,CAAC,uBAAuB,MAAM;AAE/E;AAEA,SAAS,iCAAiC,MAAwB;AAChE,MAAI,MAAM,QAAQ,IAAI,GAAG;AACvB,WAAO,KAAK,KAAK,gCAAgC;AAAA,EACnD;AACA,MAAI,SAAS,QAAQ,OAAO,SAAS,SAAU,QAAO;AACtD,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,IAAI,GAAG;AAC/C,QAAI,4BAA4B,SAAS,GAAG,EAAG,QAAO;AAC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,KAAK,GAAG;AAC/C,WAAO,QAAQ,OAAO,MAAM;AAAA,MAAI,CAAC,YAC/B,mBAAmB,SAAS,QAAQ,YAAY,CAAA,CAAE;AAAA,IAAA;AAAA,EAEtD;AAEA,MAAI,OAAO,OAAO;AAChB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO;AACT;"}
|
package/package.json
CHANGED
|
@@ -88,17 +88,43 @@ const STRICT_UNSUPPORTED_KEYWORDS: ReadonlyArray<string> = [
|
|
|
88
88
|
]
|
|
89
89
|
|
|
90
90
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
91
|
+
* Keys that give a schema node a resolvable type under OpenAI's strict subset.
|
|
92
|
+
* A schema-position node carrying none of these is *typeless* (e.g. the empty
|
|
93
|
+
* `{}` that `z.any()` / `z.unknown()` emit). Strict mode requires every schema
|
|
94
|
+
* to declare a type, so a typeless node 400s the whole request — such tools
|
|
95
|
+
* must be sent with `strict: false` instead. (`oneOf`/`allOf`/`$ref` count as
|
|
96
|
+
* type indicators here even though they're independently strict-unsupported;
|
|
97
|
+
* the keyword check below already rejects them.)
|
|
98
|
+
*/
|
|
99
|
+
const TYPE_INDICATOR_KEYWORDS: ReadonlyArray<string> = [
|
|
100
|
+
'type',
|
|
101
|
+
'enum',
|
|
102
|
+
'const',
|
|
103
|
+
'anyOf',
|
|
104
|
+
'oneOf',
|
|
105
|
+
'allOf',
|
|
106
|
+
'$ref',
|
|
107
|
+
]
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns `false` when `schema` cannot be made strict-compatible and must be
|
|
111
|
+
* sent with `strict: false`. Two ways that happens:
|
|
112
|
+
*
|
|
113
|
+
* 1. It uses a JSON-Schema keyword outside OpenAI's strict subset anywhere in
|
|
114
|
+
* the tree (`oneOf`/`allOf`/`not`/`$ref`/`$defs`).
|
|
115
|
+
* 2. It contains a *typeless* schema node — a property/items/anyOf entry with
|
|
116
|
+
* no `type` (nor `enum`/`const`/combinator), e.g. the `{}` that `z.any()`
|
|
117
|
+
* produces. Strict mode rejects typeless schemas.
|
|
94
118
|
*
|
|
95
|
-
* Conservative by design: keywords are matched as object keys, so a
|
|
96
|
-
* literally named e.g. `oneOf` also trips it. That only costs that one
|
|
97
|
-
* strict mode, which is strictly safer than a false "compatible"
|
|
98
|
-
* 400s the whole request.
|
|
119
|
+
* Conservative by design: for (1) keywords are matched as object keys, so a
|
|
120
|
+
* property literally named e.g. `oneOf` also trips it. That only costs that one
|
|
121
|
+
* tool its strict mode, which is strictly safer than a false "compatible"
|
|
122
|
+
* verdict that 400s the whole request.
|
|
99
123
|
*/
|
|
100
124
|
export function isStrictModeCompatible(schema: unknown): boolean {
|
|
101
|
-
return
|
|
125
|
+
return (
|
|
126
|
+
!containsStrictUnsupportedKeyword(schema) && !containsTypelessSchema(schema)
|
|
127
|
+
)
|
|
102
128
|
}
|
|
103
129
|
|
|
104
130
|
function containsStrictUnsupportedKeyword(node: unknown): boolean {
|
|
@@ -113,6 +139,45 @@ function containsStrictUnsupportedKeyword(node: unknown): boolean {
|
|
|
113
139
|
return false
|
|
114
140
|
}
|
|
115
141
|
|
|
142
|
+
/** A schema-position node that declares no type and so 400s strict mode. */
|
|
143
|
+
function isTypelessSchema(node: unknown): boolean {
|
|
144
|
+
if (node === null || typeof node !== 'object' || Array.isArray(node)) {
|
|
145
|
+
// boolean schemas (`true`/`false`) and non-objects aren't typeless props.
|
|
146
|
+
return false
|
|
147
|
+
}
|
|
148
|
+
return !TYPE_INDICATOR_KEYWORDS.some((key) => key in node)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Walks the genuine schema positions (property values, `items`, `anyOf`
|
|
153
|
+
* variants) and reports whether any is typeless. Unlike the keyword walk this
|
|
154
|
+
* must respect structure: an empty `{}` is only a problem at a schema position,
|
|
155
|
+
* not e.g. an empty `properties` map.
|
|
156
|
+
*/
|
|
157
|
+
function containsTypelessSchema(node: unknown): boolean {
|
|
158
|
+
if (node === null || typeof node !== 'object' || Array.isArray(node)) {
|
|
159
|
+
return false
|
|
160
|
+
}
|
|
161
|
+
const schema = node as Record<string, any>
|
|
162
|
+
|
|
163
|
+
const children: Array<unknown> = []
|
|
164
|
+
if (schema.properties && typeof schema.properties === 'object') {
|
|
165
|
+
children.push(...Object.values(schema.properties))
|
|
166
|
+
}
|
|
167
|
+
if (schema.items !== undefined) {
|
|
168
|
+
children.push(
|
|
169
|
+
...(Array.isArray(schema.items) ? schema.items : [schema.items]),
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
if (Array.isArray(schema.anyOf)) {
|
|
173
|
+
children.push(...schema.anyOf)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return children.some(
|
|
177
|
+
(child) => isTypelessSchema(child) || containsTypelessSchema(child),
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
|
|
116
181
|
/**
|
|
117
182
|
* Strict-mode structural rewrite (required widening, nullability,
|
|
118
183
|
* additionalProperties). Kept private so the public entry point can apply the
|