Commit 31de22b
fix(ai): undo strict-mode null-widening before structured-output validation (#732)
* fix(ai): undo strict-mode null-widening before structured-output validation
Optional fields are widened to required+nullable for strict structured output, so providers return `null` for an absent optional. Validating that `null` against the original schema failed (`.optional()` is `T | undefined`, not `T | null`), surfacing as a StandardSchemaValidationError — most visibly through @tanstack/ai-openrouter, whose adapter preserves provider nulls.
Add `undoNullWidening(value, schema)` to @tanstack/ai-utils: a schema-aware counterpart to `transformNullsToUndefined` that drops only synthesized nulls (those the original JSON Schema disallows) while preserving the ones a `.nullable()`/`.nullish()` field genuinely allows. The chat activity runs it on the structured-output result before Standard Schema validation.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(ai-utils): handle tuple items and ambiguous anyOf branches in undoNullWidening
Addresses CodeRabbit review on #732:
- resolveSchema now descends only when exactly one non-null anyOf/oneOf branch matches the value's shape; ambiguous unions keep the original schema rather than risk stripping a null a sibling branch allows.
- Array walking applies tuple-style `items: [a, b, …]` schemas per index instead of always using the first.
Adds coverage for both and fixes the test's import order.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* refactor(ai): record null-widening map at conversion time instead of re-deriving it
Replace the schema-guessing `undoNullWidening` — which reverse-engineered which
nulls strict-mode widening synthesized by pattern-matching response values
against the un-widened schema's anyOf branches, and bailed on ambiguity — with a
precise map recorded by the widening pass itself.
`makeStructuredOutputCompatible` now returns the strict schema plus a
`NullWideningMap` marking exactly the positions where it added `null`. The new
`convertSchemaForStructuredOutput` exposes both, and the chat activity threads
that map into `undoNullWidening`. This drops `resolveSchema`/`allowsNull` branch
guessing, preserves `.nullish()` nulls by construction, and closes the
ambiguous-union gap where synthesized nulls were previously left in place.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(ai): un-widen structured-output nulls in the engine for both stream modes and every adapter
Strict-mode structured output widens optional fields to `required` + nullable,
so providers return `null` for an absent optional. That `null` fails validation
against the original `.optional()` schema (`T | undefined`, not `T | null`).
Previously only the Promise<T> path un-widened, and only for adapters that
preserved provider nulls (OpenRouter). The OpenAI-family adapters instead
blind-stripped every null via `transformStructuredOutput`, which masked the bug
but also destroyed genuine `.nullable()` nulls — and the streaming path didn't
un-widen at all.
Move un-widening into the engine, the one layer that holds the schema's
null-widening map:
- Add `finalStructuredOutput.normalize`, applied the instant the structured
output is captured, so it flows to BOTH the streaming
`structured-output.complete` event and the Promise<T> result (plus the
native-combined harvest path). Both activity callers now pass it via
`convertSchemaForStructuredOutput`; streaming switches off the map-less
`convertSchemaToJsonSchema`. Validation runs on already-normalized data.
- openai-base `transformStructuredOutput` default is now a passthrough — the
blind null-strip is gone (the engine un-widens precisely instead). Fixes the
responses-text streaming path that bypassed the hook. OpenAI/Grok/Groq inherit
this; OpenRouter's now-redundant override is simplified and its dead
`transformNullsToUndefined` imports dropped.
Genuine `.nullable()` nulls now survive on every adapter and both directions;
synthesized optional nulls are dropped everywhere.
Tests: streaming normalization + a converter→undo round-trip (closing the
untested map-production gap); adapter passthrough tests updated; e2e gains an
optional field returning `null` asserted un-widened across all 5 streaming
providers (real regression guard for OpenRouter).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* test(ai): cover native-combined + streaming-rewrite null normalization; fix comment
Follow-up to the engine-level un-widening commit, addressing review gaps:
- Fix an inaccurate inline comment: the `structured-output.complete` event's
value is `{ object, raw, reasoning? }` — it carries no `messageId` (that's on
`structured-output.start`). The outbound-chunk rewrite preserves `raw`, not
`messageId`.
- Add native-combined mode coverage (the `harvestCombinedStructuredOutput`
capture site was untested): both the harvested Promise<T> result and the
synthesized streaming complete event must un-widen.
- Add a streaming-rewrite test asserting the engine replaces only `object`
(un-widened) while spreading the event's sibling `raw`/`reasoning` through
untouched — guards the `{ ...value, object }` contract.
- Add a round-trip case proving a genuine `.nullable()` null inside an array
item survives (the spot the array/tuple handling could wrongly strip).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* test(openai-base): assert structuredOutputStream passes provider nulls through
Addresses CodeRabbit review (PR #732): the non-streaming passthrough assertion
had no streaming sibling. Adds a `structuredOutputStream()` case emitting a
provider `null` and asserting the terminal `structured-output.complete` object
preserves it — guarding against the stream path regressing to a blind null-strip
while the non-stream path relies on engine-level un-widening.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* ci: apply automated fixes
* test(react-native-smoke): register @tanstack/ai-utils in resolution configs
The chat activity and schema-converter now import @tanstack/ai-utils, so the React Native smoke graph reaches it. The smoke fixture resolves workspace packages to source via explicit per-tool mappings, so add @tanstack/ai-utils (mirroring @tanstack/ai-event-client) to the tsconfig paths, metro packageEntryPoints, the esbuild alias map, and the import-surface walker. Fixes the TS2307 'Cannot find module @tanstack/ai-utils' in the smoke typecheck.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Tom Beckenham <34339192+tombeckenham@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>1 parent 6caac6b commit 31de22b
26 files changed
Lines changed: 882 additions & 177 deletions
File tree
- .changeset
- docs
- structured-outputs
- packages
- ai-openrouter
- src/adapters
- tests
- ai-utils
- src
- tests
- ai
- src/activities/chat
- tools
- tests
- openai-base
- src/adapters
- tests
- testing
- e2e
- fixtures/structured-output-stream
- src/lib
- tests
- react-native-smoke
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
| 175 | + | |
| 176 | + | |
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
90 | 92 | | |
91 | 93 | | |
92 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
697 | 697 | | |
698 | 698 | | |
699 | 699 | | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
705 | 704 | | |
706 | 705 | | |
707 | | - | |
708 | 706 | | |
709 | 707 | | |
710 | 708 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
624 | 624 | | |
625 | 625 | | |
626 | 626 | | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | | - | |
631 | | - | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
632 | 631 | | |
633 | 632 | | |
634 | | - | |
635 | 633 | | |
636 | 634 | | |
637 | 635 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1249 | 1249 | | |
1250 | 1250 | | |
1251 | 1251 | | |
1252 | | - | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
1253 | 1259 | | |
1254 | 1260 | | |
1255 | 1261 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
4 | 5 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
| |||
44 | 49 | | |
45 | 50 | | |
46 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
| |||
0 commit comments