@tanstack/openai-base 0.10.16 → 0.12.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"schema-converter.js","names":[],"sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["import type { NullWideningMap } from '@tanstack/ai-utils'\n\n/**\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 makeStructuredOutputCompatibleWithMap(schema, originalRequired).schema\n}\n\nexport interface StructuredOutputCompatibility {\n schema: Record<string, any>\n nullWideningMap: NullWideningMap | undefined\n}\n\ninterface CoercedStrictSchema extends StructuredOutputCompatibility {\n hasUntrackableAnyOfWidening: boolean\n}\n\n/**\n * Strict-schema conversion plus an exact map of the nullability introduced by\n * that conversion. Consumers can pass provider output through\n * `undoNullWidening` before validating it against the original schema.\n */\nexport function makeStructuredOutputCompatibleWithMap(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): StructuredOutputCompatibility {\n const { schema: strictSchema, nullWideningMap } = coerceStrictSchema(\n schema,\n originalRequired,\n )\n return {\n schema: stripUnsupportedFormats(strictSchema),\n nullWideningMap,\n }\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 * - `prefixItems` — 2020-12 tuple keyword. openai-node's strict transform\n * rejects it, so we send those tools with `strict: false` instead\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 'prefixItems',\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`/`prefixItems`/`$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 * 3. It contains an open object schema. OpenAI strict mode requires objects to\n * set `additionalProperties: false`, which would change the semantics of a\n * free-form map rather than merely normalizing it.\n * 4. An `anyOf` variant itself needs null widening. The inverse map is\n * intentionally schema-blind, so it cannot select a variant without risking\n * removal of a genuine nullable value accepted by another variant.\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) &&\n !containsTypelessSchema(schema) &&\n !containsOpenObject(schema) &&\n !containsUntrackableAnyOfWidening(schema)\n )\n}\n\n/**\n * Reports strict conversions whose synthesized nulls cannot be represented by\n * the schema-blind inverse map. Optional `anyOf` wrappers remain supported:\n * only widening introduced inside one of their variants triggers fallback.\n */\nfunction containsUntrackableAnyOfWidening(schema: unknown): boolean {\n if (schema === null || typeof schema !== 'object' || Array.isArray(schema)) {\n return false\n }\n return coerceStrictSchema(schema as Record<string, any>)\n .hasUntrackableAnyOfWidening\n}\n\n/**\n * Reports object schemas that cannot be closed without changing their input\n * semantics. Objects with `properties` and no explicit\n * `additionalProperties` are safe because `coerceStrictSchema` closes them.\n */\nfunction containsOpenObject(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsOpenObject)\n }\n if (node === null || typeof node !== 'object') return false\n\n const schema = node as Record<string, unknown>\n const type = schema['type']\n const isObjectSchema =\n type === 'object' || (Array.isArray(type) && type.includes('object'))\n\n if (isObjectSchema) {\n if (\n 'additionalProperties' in schema &&\n schema['additionalProperties'] !== false\n ) {\n return true\n }\n\n const properties = schema['properties']\n const hasProperties =\n properties !== null &&\n typeof properties === 'object' &&\n !Array.isArray(properties)\n if (!hasProperties && schema['additionalProperties'] !== false) {\n return true\n }\n }\n\n return Object.values(schema).some(containsOpenObject)\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 // JSON Schema permits bare boolean nodes; malformed inputs may contain\n // other primitives. OpenAI's strict subset requires a declared type, so\n // preserve the containing tool by sending it in non-strict mode.\n return true\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 pruneMap(map: NullWideningMap): NullWideningMap | undefined {\n return Object.keys(map).length > 0 ? map : undefined\n}\n\nfunction isSchemaObject(schema: unknown): schema is Record<string, any> {\n return typeof schema === 'object' && schema !== null && !Array.isArray(schema)\n}\n\n/** Whether every active JSON Schema constraint at this node admits null. */\nfunction acceptsNull(schema: unknown): boolean {\n if (schema === true) return true\n if (!isSchemaObject(schema)) return false\n\n if ('const' in schema && schema.const !== null) return false\n if (Array.isArray(schema.enum) && !schema.enum.includes(null)) return false\n\n if (typeof schema.type === 'string' && schema.type !== 'null') return false\n if (Array.isArray(schema.type) && !schema.type.includes('null')) return false\n\n if (\n Array.isArray(schema.anyOf) &&\n !schema.anyOf.some((variant: unknown) => acceptsNull(variant))\n ) {\n return false\n }\n\n return true\n}\n\nfunction coerceStrictSchema(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): CoercedStrictSchema {\n const result = { ...schema }\n const nullWideningMap: NullWideningMap = {}\n let hasUntrackableAnyOfWidening = false\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 const propertyMaps: Record<string, NullWideningMap> = {}\n\n for (const propName of allPropertyNames) {\n let prop = properties[propName]\n const wasOptional = !required.includes(propName)\n let childMap: NullWideningMap | undefined\n let widenedHere = false\n\n // Step 1: Recurse into nested structures\n if (isSchemaObject(prop) && prop.type === 'object' && prop.properties) {\n const nested = coerceStrictSchema(prop, prop.required || [])\n prop = nested.schema\n childMap = nested.nullWideningMap\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n } else if (isSchemaObject(prop) && prop.type === 'array') {\n const nested = coerceStrictSchema(prop, [])\n prop = nested.schema\n childMap = nested.nullWideningMap\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n } else if (isSchemaObject(prop) && prop.anyOf) {\n const nested = coerceStrictSchema(prop, prop.required || [])\n prop = nested.schema\n childMap = nested.nullWideningMap\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n } else if (isSchemaObject(prop) && 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 const originallyAcceptedNull = acceptsNull(prop)\n\n // `type: [..., 'null']` alone does not make null valid when an enum or\n // const still excludes it; strict decoding would be forced to emit the\n // original literal instead of the synthetic omission marker.\n if (isSchemaObject(prop) && 'const' in prop && prop.const !== null) {\n const { const: constValue, ...withoutConst } = prop\n prop = { ...withoutConst, enum: [constValue, null] }\n } else if (\n isSchemaObject(prop) &&\n Array.isArray(prop.enum) &&\n !prop.enum.includes(null)\n ) {\n prop = { ...prop, enum: [...prop.enum, null] }\n }\n\n if (isSchemaObject(prop) && prop.anyOf) {\n // A genuine null branch can use type, enum, or const. Only add a\n // provider omission marker when the original union rejected null.\n if (!acceptsNull(prop)) {\n prop = { ...prop, anyOf: [...prop.anyOf, { type: 'null' }] }\n }\n } else if (\n isSchemaObject(prop) &&\n prop.type &&\n !Array.isArray(prop.type)\n ) {\n prop = { ...prop, type: [prop.type, 'null'] }\n } else if (\n isSchemaObject(prop) &&\n Array.isArray(prop.type) &&\n !prop.type.includes('null')\n ) {\n prop = { ...prop, type: [...prop.type, 'null'] }\n }\n\n widenedHere = !originallyAcceptedNull && acceptsNull(prop)\n }\n\n properties[propName] = prop\n if (childMap || widenedHere) {\n propertyMaps[propName] = {\n ...(childMap ?? {}),\n ...(widenedHere ? { widened: true } : {}),\n }\n }\n }\n\n result.properties = properties\n result.required = allPropertyNames\n result.additionalProperties = false\n if (Object.keys(propertyMaps).length > 0) {\n nullWideningMap.properties = propertyMaps\n }\n }\n\n if (result.type === 'array' && result.items) {\n if (Array.isArray(result.items)) {\n const itemMaps: Array<NullWideningMap> = []\n result.items = result.items.map((item) => {\n if (!isSchemaObject(item)) {\n itemMaps.push({})\n return item\n }\n const nested = coerceStrictSchema(item, item.required || [])\n itemMaps.push(nested.nullWideningMap ?? {})\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n return nested.schema\n })\n if (itemMaps.some((map) => Object.keys(map).length > 0)) {\n nullWideningMap.items = itemMaps\n }\n } else {\n const nested = coerceStrictSchema(\n result.items,\n result.items.required || [],\n )\n result.items = nested.schema\n if (nested.nullWideningMap) {\n nullWideningMap.items = nested.nullWideningMap\n }\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n }\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n const variants = result.anyOf.map((variant) =>\n coerceStrictSchema(variant, variant.required || []),\n )\n result.anyOf = variants.map((variant) => variant.schema)\n hasUntrackableAnyOfWidening ||= variants.some(\n (variant) =>\n variant.nullWideningMap !== undefined ||\n variant.hasUntrackableAnyOfWidening,\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 {\n schema: result,\n nullWideningMap: pruneMap(nullWideningMap),\n hasUntrackableAnyOfWidening,\n }\n}\n"],"mappings":";;;;;;;;;AAUA,IAAM,2CAA2B,IAAI,IAAI;CACvC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;;;;;;;;;;AAWD,SAAgB,wBAAwB,MAAgB;CACtD,IAAI,MAAM,QAAQ,IAAI,GAAG,OAAO,KA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AAK,GAAG;EAC/C,MAAM,WAAW,OAAO,MAAM,KAAK,YACjC,mBAAmB,SAAS,QAAQ,YAAY,CAAC,CAAC,CACpD;EACA,OAAO,QAAQ,SAAS,KAAK,YAAY,QAAQ,MAAM;EACvD,gCAAgC,SAAS,MACtC,YACC,QAAQ,oBAAoB,KAAA,KAC5B,QAAQ,2BACZ;CACF;CAEA,IAAI,OAAO,OACT,MAAM,IAAI,MACR,0KACF;CAGF,OAAO;EACL,QAAQ;EACR,iBAAiB,SAAS,eAAe;EACzC;CACF;AACF"}
1
+ {"version":3,"file":"schema-converter.js","names":[],"sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["import type { NullWideningMap } from '@tanstack/ai-utils'\nimport type { Tool } from '@tanstack/ai'\nimport type { InternalLogger } from '@tanstack/ai/adapter-internals'\n\n/**\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 makeStructuredOutputCompatibleWithMap(schema, originalRequired).schema\n}\n\nexport interface StructuredOutputCompatibility {\n schema: Record<string, any>\n nullWideningMap: NullWideningMap | undefined\n}\n\ninterface CoercedStrictSchema extends StructuredOutputCompatibility {\n hasUntrackableAnyOfWidening: boolean\n}\n\n/**\n * Strict-schema conversion plus an exact map of the nullability introduced by\n * that conversion. Consumers can pass provider output through\n * `undoNullWidening` before validating it against the original schema.\n */\nexport function makeStructuredOutputCompatibleWithMap(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): StructuredOutputCompatibility {\n const { schema: strictSchema, nullWideningMap } = coerceStrictSchema(\n schema,\n originalRequired,\n )\n return {\n schema: stripUnsupportedFormats(strictSchema),\n nullWideningMap,\n }\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 * - `prefixItems` — 2020-12 tuple keyword. openai-node's strict transform\n * rejects it, so we send those tools with `strict: false` instead\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 'prefixItems',\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`/`prefixItems`/`$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 * 3. It contains an open object schema. OpenAI strict mode requires objects to\n * set `additionalProperties: false`, which would change the semantics of a\n * free-form map rather than merely normalizing it.\n * 4. An `anyOf` variant itself needs null widening. The inverse map is\n * intentionally schema-blind, so it cannot select a variant without risking\n * removal of a genuine nullable value accepted by another variant.\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 strictModeFallbackReason(schema) === undefined\n}\n\n/**\n * Why `schema` must be sent with `strict: false`, or `undefined` when it can be\n * strict. Runs the same checks as `isStrictModeCompatible`, in the same order.\n */\nexport function strictModeFallbackReason(schema: unknown): string | undefined {\n const keyword = findStrictUnsupportedKeyword(schema)\n if (keyword !== undefined) {\n return `schema uses ${keyword}, which strict mode does not support`\n }\n if (containsTypelessSchema(schema)) {\n return 'schema has a node with no type (for example z.any() or z.unknown())'\n }\n if (containsOpenObject(schema)) {\n return 'schema has an open object (for example z.record())'\n }\n if (containsUntrackableAnyOfWidening(schema)) {\n return 'schema has an optional field inside an anyOf variant'\n }\n return undefined\n}\n\n/** Options that every `openai-base` text adapter accepts in its config. */\nexport interface OpenAIBaseTextAdapterOptions {\n /**\n * In development, warn once per tool that is sent with `strict: false`\n * because its schema cannot be strict. Set to `false` to turn the warning\n * off. It never runs when `NODE_ENV` is `production`. Default: `true`.\n */\n strictFallbackWarning?: boolean\n}\n\n// ponytail: keyed on the Tool object, so a tool defined once warns once per\n// process. Tools rebuilt per request (e.g. from MCP) warn once per request.\nconst warnedStrictFallback = new WeakSet<Tool>()\n\n/**\n * Warn once per tool that is sent with `strict: false` because its schema\n * cannot be strict. The tool still works, but the model is not held to the\n * schema, so the developer must know (#1213).\n */\nexport function warnStrictFallback(\n tools: Array<Tool> | undefined,\n logger: InternalLogger,\n): void {\n // Development only. `process` is absent on some runtimes (e.g. Workers).\n if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production')\n return\n for (const tool of tools ?? []) {\n if (!tool.inputSchema || warnedStrictFallback.has(tool)) continue\n const reason = strictModeFallbackReason(tool.inputSchema)\n if (reason === undefined) continue\n warnedStrictFallback.add(tool)\n logger.warn(`tool \"${tool.name}\" sent with strict: false: ${reason}`, {\n tool: tool.name,\n })\n }\n}\n\n/**\n * Reports strict conversions whose synthesized nulls cannot be represented by\n * the schema-blind inverse map. Optional `anyOf` wrappers remain supported:\n * only widening introduced inside one of their variants triggers fallback.\n */\nfunction containsUntrackableAnyOfWidening(schema: unknown): boolean {\n if (schema === null || typeof schema !== 'object' || Array.isArray(schema)) {\n return false\n }\n return coerceStrictSchema(schema as Record<string, any>)\n .hasUntrackableAnyOfWidening\n}\n\n/**\n * Reports object schemas that cannot be closed without changing their input\n * semantics. Objects with `properties` and no explicit\n * `additionalProperties` are safe because `coerceStrictSchema` closes them.\n */\nfunction containsOpenObject(node: unknown): boolean {\n if (Array.isArray(node)) {\n return node.some(containsOpenObject)\n }\n if (node === null || typeof node !== 'object') return false\n\n const schema = node as Record<string, unknown>\n const type = schema['type']\n const isObjectSchema =\n type === 'object' || (Array.isArray(type) && type.includes('object'))\n\n if (isObjectSchema) {\n if (\n 'additionalProperties' in schema &&\n schema['additionalProperties'] !== false\n ) {\n return true\n }\n\n const properties = schema['properties']\n const hasProperties =\n properties !== null &&\n typeof properties === 'object' &&\n !Array.isArray(properties)\n if (!hasProperties && schema['additionalProperties'] !== false) {\n return true\n }\n }\n\n return Object.values(schema).some(containsOpenObject)\n}\n\nfunction findStrictUnsupportedKeyword(node: unknown): string | undefined {\n if (Array.isArray(node)) {\n for (const item of node) {\n const found = findStrictUnsupportedKeyword(item)\n if (found !== undefined) return found\n }\n return undefined\n }\n if (node === null || typeof node !== 'object') return undefined\n for (const [key, value] of Object.entries(node)) {\n if (STRICT_UNSUPPORTED_KEYWORDS.includes(key)) return key\n const found = findStrictUnsupportedKeyword(value)\n if (found !== undefined) return found\n }\n return undefined\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 // JSON Schema permits bare boolean nodes; malformed inputs may contain\n // other primitives. OpenAI's strict subset requires a declared type, so\n // preserve the containing tool by sending it in non-strict mode.\n return true\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 pruneMap(map: NullWideningMap): NullWideningMap | undefined {\n return Object.keys(map).length > 0 ? map : undefined\n}\n\nfunction isSchemaObject(schema: unknown): schema is Record<string, any> {\n return typeof schema === 'object' && schema !== null && !Array.isArray(schema)\n}\n\n/** Whether every active JSON Schema constraint at this node admits null. */\nfunction acceptsNull(schema: unknown): boolean {\n if (schema === true) return true\n if (!isSchemaObject(schema)) return false\n\n if ('const' in schema && schema.const !== null) return false\n if (Array.isArray(schema.enum) && !schema.enum.includes(null)) return false\n\n if (typeof schema.type === 'string' && schema.type !== 'null') return false\n if (Array.isArray(schema.type) && !schema.type.includes('null')) return false\n\n if (\n Array.isArray(schema.anyOf) &&\n !schema.anyOf.some((variant: unknown) => acceptsNull(variant))\n ) {\n return false\n }\n\n return true\n}\n\nfunction coerceStrictSchema(\n schema: Record<string, any>,\n originalRequired?: Array<string>,\n): CoercedStrictSchema {\n const result = { ...schema }\n const nullWideningMap: NullWideningMap = {}\n let hasUntrackableAnyOfWidening = false\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 const propertyMaps: Record<string, NullWideningMap> = {}\n\n for (const propName of allPropertyNames) {\n let prop = properties[propName]\n const wasOptional = !required.includes(propName)\n let childMap: NullWideningMap | undefined\n let widenedHere = false\n\n // Step 1: Recurse into nested structures\n if (isSchemaObject(prop) && prop.type === 'object' && prop.properties) {\n const nested = coerceStrictSchema(prop, prop.required || [])\n prop = nested.schema\n childMap = nested.nullWideningMap\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n } else if (isSchemaObject(prop) && prop.type === 'array') {\n const nested = coerceStrictSchema(prop, [])\n prop = nested.schema\n childMap = nested.nullWideningMap\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n } else if (isSchemaObject(prop) && prop.anyOf) {\n const nested = coerceStrictSchema(prop, prop.required || [])\n prop = nested.schema\n childMap = nested.nullWideningMap\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n } else if (isSchemaObject(prop) && 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 const originallyAcceptedNull = acceptsNull(prop)\n\n // `type: [..., 'null']` alone does not make null valid when an enum or\n // const still excludes it; strict decoding would be forced to emit the\n // original literal instead of the synthetic omission marker.\n if (isSchemaObject(prop) && 'const' in prop && prop.const !== null) {\n const { const: constValue, ...withoutConst } = prop\n prop = { ...withoutConst, enum: [constValue, null] }\n } else if (\n isSchemaObject(prop) &&\n Array.isArray(prop.enum) &&\n !prop.enum.includes(null)\n ) {\n prop = { ...prop, enum: [...prop.enum, null] }\n }\n\n if (isSchemaObject(prop) && prop.anyOf) {\n // A genuine null branch can use type, enum, or const. Only add a\n // provider omission marker when the original union rejected null.\n if (!acceptsNull(prop)) {\n prop = { ...prop, anyOf: [...prop.anyOf, { type: 'null' }] }\n }\n } else if (\n isSchemaObject(prop) &&\n prop.type &&\n !Array.isArray(prop.type)\n ) {\n prop = { ...prop, type: [prop.type, 'null'] }\n } else if (\n isSchemaObject(prop) &&\n Array.isArray(prop.type) &&\n !prop.type.includes('null')\n ) {\n prop = { ...prop, type: [...prop.type, 'null'] }\n }\n\n widenedHere = !originallyAcceptedNull && acceptsNull(prop)\n }\n\n properties[propName] = prop\n if (childMap || widenedHere) {\n propertyMaps[propName] = {\n ...(childMap ?? {}),\n ...(widenedHere ? { widened: true } : {}),\n }\n }\n }\n\n result.properties = properties\n result.required = allPropertyNames\n result.additionalProperties = false\n if (Object.keys(propertyMaps).length > 0) {\n nullWideningMap.properties = propertyMaps\n }\n }\n\n if (result.type === 'array' && result.items) {\n if (Array.isArray(result.items)) {\n const itemMaps: Array<NullWideningMap> = []\n result.items = result.items.map((item) => {\n if (!isSchemaObject(item)) {\n itemMaps.push({})\n return item\n }\n const nested = coerceStrictSchema(item, item.required || [])\n itemMaps.push(nested.nullWideningMap ?? {})\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n return nested.schema\n })\n if (itemMaps.some((map) => Object.keys(map).length > 0)) {\n nullWideningMap.items = itemMaps\n }\n } else {\n const nested = coerceStrictSchema(\n result.items,\n result.items.required || [],\n )\n result.items = nested.schema\n if (nested.nullWideningMap) {\n nullWideningMap.items = nested.nullWideningMap\n }\n hasUntrackableAnyOfWidening ||= nested.hasUntrackableAnyOfWidening\n }\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n const variants = result.anyOf.map((variant) =>\n coerceStrictSchema(variant, variant.required || []),\n )\n result.anyOf = variants.map((variant) => variant.schema)\n hasUntrackableAnyOfWidening ||= variants.some(\n (variant) =>\n variant.nullWideningMap !== undefined ||\n variant.hasUntrackableAnyOfWidening,\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 {\n schema: result,\n nullWideningMap: pruneMap(nullWideningMap),\n hasUntrackableAnyOfWidening,\n }\n}\n"],"mappings":";;;;;;;;;AAYA,IAAM,2CAA2B,IAAI,IAAI;CACvC;CACA;CACA;CACA;CACA;C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
+ "version": "0.12.1",
4
4
  "description": "Shared OpenAI SDK base adapters for TanStack AI providers using Chat Completions and Responses APIs.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -47,13 +47,13 @@
47
47
  "@tanstack/ai-utils": "^0.4.1"
48
48
  },
49
49
  "peerDependencies": {
50
- "@tanstack/ai": "^0.59.0"
50
+ "@tanstack/ai": "^0.63.0"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@vitest/coverage-v8": "4.1.10",
54
54
  "vite": "^8.2.1",
55
55
  "zod": "^4.2.0",
56
- "@tanstack/ai": "0.59.0"
56
+ "@tanstack/ai": "0.63.0"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "vite build",
@@ -61,6 +61,7 @@
61
61
  "lint:fix": "oxlint src --type-aware --fix",
62
62
  "test:build": "publint --strict",
63
63
  "test:oxlint": "oxlint src --type-aware",
64
+ "test:coverage": "vitest run --coverage --coverage.include='src/**' --coverage.reporter=text-summary --coverage.reporter=json-summary",
64
65
  "test:lib": "vitest run",
65
66
  "test:lib:dev": "pnpm test:lib --watch",
66
67
  "test:types": "tsc"
@@ -1,4 +1,9 @@
1
- import { EventType, normalizeSystemPrompts } from '@tanstack/ai'
1
+ import {
2
+ EventType,
3
+ isFileSource,
4
+ normalizeSystemPrompts,
5
+ unsupportedFileSourceError,
6
+ } from '@tanstack/ai'
2
7
  import { BaseTextAdapter } from '@tanstack/ai/adapters'
3
8
  import {
4
9
  toRunErrorPayload,
@@ -6,9 +11,15 @@ import {
6
11
  } from '@tanstack/ai/adapter-internals'
7
12
  import { generateId } from '@tanstack/ai-utils'
8
13
  import { extractRequestOptions } from '../utils/request-options'
9
- import { makeStructuredOutputCompatibleWithMap } from '../utils/schema-converter'
14
+ import {
15
+ makeStructuredOutputCompatibleWithMap,
16
+ warnStrictFallback,
17
+ } from '../utils/schema-converter'
10
18
  import { createToolInputNormalizer } from '../utils/tool-input-normalizer'
11
- import type { StructuredOutputCompatibility } from '../utils/schema-converter'
19
+ import type {
20
+ OpenAIBaseTextAdapterOptions,
21
+ StructuredOutputCompatibility,
22
+ } from '../utils/schema-converter'
12
23
  import { buildChatCompletionsUsage } from '../usage'
13
24
  import { convertToolsToChatCompletionsFormat } from './chat-completions-tool-converter'
14
25
  import type OpenAI from 'openai'
@@ -62,10 +73,19 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
62
73
  readonly name: string
63
74
  protected client: OpenAI
64
75
 
65
- constructor(model: TModel, name: string, client: OpenAI) {
76
+ /** See {@link OpenAIBaseTextAdapterOptions.strictFallbackWarning}. */
77
+ protected readonly strictFallbackWarning: boolean
78
+
79
+ constructor(
80
+ model: TModel,
81
+ name: string,
82
+ client: OpenAI,
83
+ options: OpenAIBaseTextAdapterOptions = {},
84
+ ) {
66
85
  super({}, model)
67
86
  this.name = name
68
87
  this.client = client
88
+ this.strictFallbackWarning = options.strictFallbackWarning ?? true
69
89
  }
70
90
 
71
91
  async *chatStream(
@@ -272,7 +292,19 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
272
292
  // rather than letting it cascade into a JSON-parse error on '' — the
273
293
  // root cause (the model returned no content for the structured request)
274
294
  // is then visible in logs.
275
- const rawText = response.choices[0]?.message.content
295
+ const choice = response.choices[0]
296
+
297
+ // A response cut off at the output cap is a truncated JSON document —
298
+ // or, for reasoning models, no content at all once the budget went to
299
+ // reasoning. Report it as truncation before the empty-content and
300
+ // parse errors, which would read like a schema failure (issue #1426).
301
+ if (choice?.finish_reason === 'length') {
302
+ throw new Error(
303
+ `${this.name}.structuredOutput: the response was cut off because the maximum token limit was reached (finish_reason=length); raise the output token limit (max_completion_tokens or max_tokens, depending on the provider)`,
304
+ )
305
+ }
306
+
307
+ const rawText = choice?.message.content
276
308
  if (typeof rawText !== 'string' || rawText.length === 0) {
277
309
  throw new Error(
278
310
  `${this.name}.structuredOutput: response contained no content`,
@@ -346,6 +378,7 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
346
378
  let hasClosedReasoning = false
347
379
  let stepId: string | undefined
348
380
  let lastModel: string | undefined
381
+ let finishReason: string | null = null
349
382
  let lastUsage:
350
383
  | OpenAI.Chat.Completions.ChatCompletionChunk['usage']
351
384
  | undefined
@@ -486,6 +519,7 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
486
519
 
487
520
  const choice = chunk.choices[0]
488
521
  if (!choice) continue
522
+ if (choice.finish_reason) finishReason = choice.finish_reason
489
523
 
490
524
  const deltaContent = choice.delta.content
491
525
  if (deltaContent) {
@@ -529,6 +563,22 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
529
563
  }
530
564
  }
531
565
 
566
+ // Same truncation check as `structuredOutput()`: report the token limit
567
+ // before the empty-content and parse errors (issue #1426).
568
+ if (finishReason === 'length') {
569
+ const message = `${this.name}.structuredOutputStream: the response was cut off because the maximum token limit was reached (finish_reason=length); raise the output token limit (max_completion_tokens or max_tokens, depending on the provider)`
570
+ yield {
571
+ type: EventType.RUN_ERROR,
572
+ runId: aguiState.runId,
573
+ model: lastModel || chatOptions.model,
574
+ timestamp: Date.now(),
575
+ message,
576
+ code: 'max_tokens',
577
+ error: { message, code: 'max_tokens' },
578
+ }
579
+ return
580
+ }
581
+
532
582
  if (accumulatedContent.length === 0) {
533
583
  yield {
534
584
  type: EventType.RUN_ERROR,
@@ -1185,6 +1235,9 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
1185
1235
  protected mapOptionsToRequest(
1186
1236
  options: TextOptions,
1187
1237
  ): ChatCompletionCreateParamsStreaming {
1238
+ if (this.strictFallbackWarning) {
1239
+ warnStrictFallback(options.tools, options.logger)
1240
+ }
1188
1241
  const tools = options.tools
1189
1242
  ? convertToolsToChatCompletionsFormat(
1190
1243
  options.tools,
@@ -1391,6 +1444,22 @@ export abstract class OpenAIBaseChatCompletionsTextAdapter<
1391
1444
  | { detail?: 'auto' | 'low' | 'high' }
1392
1445
  | undefined
1393
1446
 
1447
+ if (isFileSource(part.source)) {
1448
+ // The Chat Completions API references images only by URL/data URI,
1449
+ // not by an uploaded file_id. Only mention the Responses alternative
1450
+ // for OpenAI itself — compatible providers built on this base have no
1451
+ // file_id-consuming endpoint at all.
1452
+ throw unsupportedFileSourceError(
1453
+ this.name,
1454
+ // Only OpenAI's own chat-completions adapter (name 'openai-chat')
1455
+ // has a Responses sibling to point at; other subclasses (Groq,
1456
+ // Bedrock, BytePlus, compatible providers) have no file_id surface.
1457
+ this.name === 'openai-chat'
1458
+ ? 'on the Chat Completions API — use the Responses adapter (openaiText) to reference an uploaded file by file_id'
1459
+ : 'on the Chat Completions API',
1460
+ )
1461
+ }
1462
+
1394
1463
  // For base64 data, construct a data URI using the mimeType from source.
1395
1464
  // Default to a generic octet-stream MIME if the source didn't provide
1396
1465
  // one — interpolating `undefined` into the URI ("data:undefined;base64,