Fix: Sanitize whitespace in $ref paths for OpenAI strict mode#1705
Open
PaulyBearCoding wants to merge 1 commit intoopenai:masterfrom
Open
Fix: Sanitize whitespace in $ref paths for OpenAI strict mode#1705PaulyBearCoding wants to merge 1 commit intoopenai:masterfrom
PaulyBearCoding wants to merge 1 commit intoopenai:masterfrom
Conversation
Fixes openai#1679 When using zodResponseFormat with property names containing spaces, the generated JSON Schema includes $ref values with literal spaces, causing OpenAI API validation failures. Root cause: In parseDef.ts line 134, the extract-to-root case joins path segments with underscores but doesn't sanitize the segments themselves. Path segments like "Thing With Spaces" preserve their internal spaces in the final $ref value. Solution: Map over each path segment and replace all whitespace characters with underscores before joining. Changes: - src/_vendor/zod-to-json-schema/parseDef.ts: Added .map() to sanitize each segment with .replace(/\s+/g, '_') - tests/helpers/zod.test.ts: Added test verifying $ref values and definition keys contain no spaces Testing: - All 18 unit tests pass (16 existing + 2 new) - Tested both Zod v3 and v4 compatibility - Comprehensive edge cases: multiple spaces, tabs, newlines, Unicode spaces, deeply nested structures, emoji, CJK characters - Performance: 0.03ms per schema generation (1000 iterations) - No breaking changes: valid identifiers (underscores, hyphens, alphanumeric) preserved unchanged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1679
Problem Statement
When using
zodResponseFormatwith Zod schemas that contain property names with whitespace characters, the generated JSON Schema produces$refvalues and definition keys containing literal spaces. This causes validation failures when submitting the schema to the OpenAI API.Root Cause
In
src/_vendor/zod-to-json-schema/parseDef.tsline 134, theextract-to-rootcase joins path segments with underscores but doesn't sanitize the segments themselves. Path segments like"Thing With Spaces"preserve their internal spaces in the final$refvalue.The
join('_')method only adds separators between array elements - it does not modify the content within individual elements.Solution
Map over each path segment and replace all whitespace characters with underscores before joining:
```typescript
const name = item.path
.slice(refs.basePath.length + 1)
.map((segment) => segment.replace(/\s+/g, ''))
.join('');
```
The regex
/\s+/gmatches all types of whitespace: spaces, tabs, newlines, and Unicode whitespace characters.Testing
Unit tests:
Comprehensive edge cases validated:
No breaking changes:
Changes
Modified files:
src/_vendor/zod-to-json-schema/parseDef.ts(4 lines)tests/helpers/zod.test.ts(added comprehensive test)