fix: undo strict null widening for tool inputs#939
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughStrict-mode schema conversion now records null widening and detects unsupported ChangesOpenAI tool input null restoration
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant Chat
participant OpenAIAdapter
participant ToolInputNormalizer
participant ToolHandler
Client->>Chat: Start chat with ask_user tool
Chat->>OpenAIAdapter: Send strict tool schema
OpenAIAdapter-->>Chat: Stream tool-call arguments with null values
Chat->>ToolInputNormalizer: Restore null-widened input
ToolInputNormalizer-->>Chat: Return normalized tool input
Chat->>ToolHandler: Execute ask_user
ToolHandler-->>Chat: Return tool result
Chat->>OpenAIAdapter: Request follow-up completion
OpenAIAdapter-->>Client: Stream final assistant text
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
2a6c4c8 to
0eeb026
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/openai-base/src/utils/schema-converter.ts`:
- Around line 293-296: Preserve and merge the nullWideningMap returned by
coerceStrictSchema for each supported anyOf variant instead of retaining only
nested.schema. Update both anyOf handling sites near the prop conversion logic
so variant-specific metadata remains associated without marking unrelated
genuinely nullable fields, and add a regression test for an optional nested
property inside anyOf that verifies provider-added nulls are removed during
original-schema validation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 67373ee5-ff74-40a3-8e8b-9b825e20f5be
📒 Files selected for processing (12)
.changeset/calm-tools-return.mdpackages/openai-base/src/adapters/chat-completions-text.tspackages/openai-base/src/adapters/responses-text.tspackages/openai-base/src/utils/schema-converter.tspackages/openai-base/src/utils/tool-input-normalizer.tspackages/openai-base/tests/chat-completions-text.test.tspackages/openai-base/tests/responses-text.test.tspackages/openai-base/tests/schema-converter.test.tspackages/openai-base/tests/tool-input-normalizer.test.tstesting/e2e/src/routeTree.gen.tstesting/e2e/src/routes/api.openai-strict-tool-null-wire.tstesting/e2e/tests/openai-strict-tool-null-wire.spec.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/openai-base/tests/tool-converter-strict-fallback.test.ts (1)
58-88: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoffRelocate tests alongside the source they cover.
The current file tests multiple modules and is located in a dedicated
tests/directory. Consider splitting this file into separate test files colocated with their respective source modules (e.g.,packages/openai-base/src/adapters/responses-tool-converter.test.ts). As per coding guidelines, place unit tests in*.test.tsfiles alongside the source they cover.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/openai-base/tests/tool-converter-strict-fallback.test.ts` around lines 58 - 88, Split the tests in tool-converter-strict-fallback.test.ts by the source module or converter they cover, and colocate each resulting *.test.ts file beside its corresponding implementation under src, such as responses-tool-converter.test.ts. Preserve all existing test coverage and behavior while removing the multi-module test file from the dedicated tests directory.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/openai-base/tests/tool-converter-strict-fallback.test.ts`:
- Around line 58-88: Split the tests in tool-converter-strict-fallback.test.ts
by the source module or converter they cover, and colocate each resulting
*.test.ts file beside its corresponding implementation under src, such as
responses-tool-converter.test.ts. Preserve all existing test coverage and
behavior while removing the multi-module test file from the dedicated tests
directory.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a792ea3c-5b8c-4b77-9473-10228ca296ad
📒 Files selected for processing (4)
packages/openai-base/src/utils/schema-converter.tspackages/openai-base/tests/schema-converter.test.tspackages/openai-base/tests/tool-converter-strict-fallback.test.tspackages/openai-base/tests/tool-input-normalizer.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
- packages/openai-base/tests/tool-input-normalizer.test.ts
- packages/openai-base/tests/schema-converter.test.ts
- packages/openai-base/src/utils/schema-converter.ts
38ffc6f to
59525c4
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/openai-base/tests/tool-input-normalizer.test.ts (1)
1-100: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMove this new unit test alongside
tool-input-normalizer.ts.The current path does not satisfy the repository instruction requiring
*.test.tsfiles alongside their covered source.As per coding guidelines, “Place unit tests in
*.test.tsfiles alongside the source they cover.”<coding_guidelines>🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/openai-base/tests/tool-input-normalizer.test.ts` around lines 1 - 100, Move the createToolInputNormalizer unit tests from the tests directory into a tool-input-normalizer.test.ts file alongside tool-input-normalizer.ts, preserving all existing test cases and imports with paths adjusted for the new location.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/openai-base/tests/tool-input-normalizer.test.ts`:
- Around line 1-100: Move the createToolInputNormalizer unit tests from the
tests directory into a tool-input-normalizer.test.ts file alongside
tool-input-normalizer.ts, preserving all existing test cases and imports with
paths adjusted for the new location.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 5488cee5-334f-40d1-8389-cb3979cf2856
📒 Files selected for processing (13)
.changeset/calm-tools-return.mdpackages/openai-base/src/adapters/chat-completions-text.tspackages/openai-base/src/adapters/responses-text.tspackages/openai-base/src/utils/schema-converter.tspackages/openai-base/src/utils/tool-input-normalizer.tspackages/openai-base/tests/chat-completions-text.test.tspackages/openai-base/tests/responses-text.test.tspackages/openai-base/tests/schema-converter.test.tspackages/openai-base/tests/tool-converter-strict-fallback.test.tspackages/openai-base/tests/tool-input-normalizer.test.tstesting/e2e/src/routeTree.gen.tstesting/e2e/src/routes/api.openai-strict-tool-null-wire.tstesting/e2e/tests/openai-strict-tool-null-wire.spec.ts
🚧 Files skipped from review as they are similar to previous changes (8)
- testing/e2e/tests/openai-strict-tool-null-wire.spec.ts
- packages/openai-base/src/utils/tool-input-normalizer.ts
- .changeset/calm-tools-return.md
- packages/openai-base/src/adapters/responses-text.ts
- packages/openai-base/tests/responses-text.test.ts
- testing/e2e/src/routeTree.gen.ts
- testing/e2e/src/routes/api.openai-strict-tool-null-wire.ts
- packages/openai-base/src/utils/schema-converter.ts
🎯 Changes
OpenAI-compatible strict function schemas promote optional properties to required nullable properties. The model can therefore return
nullfor an omitted optional field, but TanStack AI subsequently validates that value against the original Standard Schema, where.optional()accepts absence/undefined, notnull. This prevents otherwise valid server tools from executing.This PR:
NullWideningMapduring the same OpenAI strict-schema conversion that adds nullabilityTest plan
pnpm run test:prpnpm --filter @tanstack/openai-base test:lib(134 tests)pnpm --filter @tanstack/ai-e2e test:e2e -- openai-strict-tool-null-wire.spec.tspnpm --filter @tanstack/ai-e2e test:e2e(347 passed, 2 unrelated flaky tests passed on retry, 1 gated live smoke skipped)✅ Checklist
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit
nullvalues for streamed OpenAI-compatible tool-call inputs by undoing strict null widening before validating/emitting tool-call payloads.anyOfedge cases.