Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai-openrouter): apply strict schema conversion inside $defs and definitions #2307

Openpcbeingused33(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2307/headopened View on GitHub

Showing 4 changed files with 101 additions and 14 deletions (loading 2).

x
0 of 4 viewed, 4 left
5 additions and 0 deletions.changeset/openrouter-strict-defs.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/ai-openrouter': patch
+---
+​
+Strict structured output and Responses function tools convert object schemas inside `$defs` and `definitions`. Optional fields there become required and nullable, and an optional `$ref` (including zod's `allOf: [{ $ref }]` form) gets a null alternative so a recursive schema can end. Tool arguments drop only those synthesized nulls.
96 additions and 14 deletionspackages/ai-openrouter/src/internal/responses-tool-converter.tsModified
View file
@@ -66,29 +66,111 @@
return Array.isArray(schema.anyOf) && schema.anyOf.some(allowsNull)
​
+/**
+ * zod 4 writes a recursive reference as `allOf: [{ $ref }]`. The converter
+ * emits the bare `$ref` (wrapped in `anyOf` when the field was optional).
+ */
+function unwrapSingleRefAllOf(schema: JSONSchema): JSONSchema {
+ const allOf = schema.allOf
+ if (
+ !Array.isArray(allOf) ||
+ allOf.length !== 1 ||
+ !allOf[0] ||
+ typeof allOf[0].$ref !== 'string'
+ ) {
+ return schema
+ }
+ const { allOf: _allOf, ...rest } = schema
+ return { ...rest, ...allOf[0] }
+}
+​