-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add warning for .optional() usage in OpenAI API schemas #1214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add warning for .optional() usage in OpenAI API schemas #1214
Conversation
This commit adds a warning when .optional() is used in schemas for OpenAI API Structured Outputs, recommending the use of .nullable() instead. - Added warning in optional.ts that triggers when openaiStrictMode is true - Added test to verify warning behavior Fixes openai#1180
|
|
||
| export const parseOptionalDef = (def: ZodOptionalDef, refs: Refs): JsonSchema7Type | undefined => { | ||
| if (refs.openaiStrictMode) { | ||
| const fieldName = refs.propertyPath?.slice(-1)[0] || 'unknown'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since refs.propertyPath is an array, can we replace .slice(-1)[0] with .at(-1) here? Using .at(-1) improves readability and performance by directly accessing the last element without creating a new array.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at
| `See: https://platform.openai.com/docs/guides/structured-outputs#all-fields-must-be-required`, | ||
| ); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary use of string concatenation (+) since we are already using template literals.
| expect(consoleSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('uses .optional() which is not supported by OpenAI API Structured Outputs'), | ||
| ); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we also test expect(consoleSpy).toHaveBeenCalledTimes(1); to ensure this is called only once.
|
Thanks for the PR and sorry for the delayed response! I did this separately in #1469 as I wanted to take a slightly different approach. |
Add warning for .optional() usage in OpenAI API schemas
This PR adds a warning when
.optional()is used in schemas for OpenAI API Structured Outputs, recommending the use of.nullable()instead since optional fields are not currently supported.Changes
optional.tsthat triggers whenopenaiStrictModeis trueTesting
✅ All tests passing (411 total)
Implementation Details
The warning is implemented in the optional field parser and only triggers when generating schemas specifically for OpenAI API usage (when
openaiStrictModeis true). This ensures developers get immediate feedback about unsupported optional fields while keeping the warning relevant only to OpenAI API usage.Created with help from Devin: https://preview.devin.ai/devin/13e97f93e3b1445c91e60da01b88febd
Fixes #1180