Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai, openai-base): apply strict schema conversion inside $defs and definitions #2079

Mergedpcbeingused33(GitHub profile, opens in a new tab)merged 5 commits intomain fromrefs/pull/2079/headmerged by AlemTuzlak View on GitHub

Showing 6 changed files with 97 additions and 13 deletions (loading 3).

x
0 of 6 viewed, 6 left
5 additions and 0 deletions.changeset/ai-strict-defs-null-undo.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/ai': patch
+---
+​
+Structured output now applies the strict conversion to schemas inside `$defs` and `definitions`, and undoes it after the response. An optional field inside a definition comes back as an omitted key, not as `null`, so a `.optional()` field passes validation. This also works for recursive schemas, for example a zod 4 `next?: Node`: an optional `$ref` gets a `null` branch so the chain can end, and zod's `allOf: [{ $ref }]` form becomes a bare `$ref`, because strict mode does not support `allOf`. Fields that are really nullable keep their `null`.
7 additions and 0 deletions.changeset/openai-base-strict-defs.mdAdded
View file
@@ -0,0 +1,7 @@
+---
+'@tanstack/openai-base': patch
+---
+​
+Apply the strict structured-output conversion to schemas inside `$defs` and `definitions`. Recursive schemas (for example from zod 4) previously reached the API with optional properties missing from `required`, which OpenAI and Azure OpenAI reject.
+​
+An optional property that is only a `$ref`, or zod 4's `allOf: [{ $ref }]` form of it, now becomes `anyOf: [{ $ref }, { type: 'null' }]`, so recursive schemas such as `next?: Node` can end. Strict mode does not support `allOf`.
85 additions and 13 deletionspackages/ai/src/activities/chat/tools/schema-converter.tsModified
View file
@@ -99,13 +99,34 @@
return Object.keys(map).length > 0 ? map : undefined
​
-function coerceArrayItems(items: JSONSchema | Array<JSONSchema>): {
+/** `$defs`/`definitions` entries, keyed by `$ref`: shared map + nullability. */
+type DefinitionMaps = Record<
+ string,
+ { map: NullWideningMap; nullable: boolean }
+>
+​
+/**