-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,15 @@ import { JsonSchema7Type, parseDef } from '../parseDef'; | |
| import { Refs } from '../Refs'; | ||
|
|
||
| export const parseOptionalDef = (def: ZodOptionalDef, refs: Refs): JsonSchema7Type | undefined => { | ||
| if (refs.openaiStrictMode) { | ||
| const fieldName = refs.propertyPath?.slice(-1)[0] || 'unknown'; | ||
| console.warn( | ||
| `Warning: Field "${fieldName}" uses .optional() which is not supported by OpenAI API Structured Outputs. ` + | ||
| `Please use .nullable() instead. ` + | ||
| `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 commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary use of string concatenation (+) since we are already using template literals. |
||
| if (refs.currentPath.toString() === refs.propertyPath?.toString()) { | ||
| return parseDef(def.innerType._def, refs); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,24 @@ describe('zodResponseFormat', () => { | |
| `); | ||
| }); | ||
|
|
||
| it('warns when using optional fields with OpenAI API', () => { | ||
| const consoleSpy = jest.spyOn(console, 'warn'); | ||
|
|
||
| zodResponseFormat( | ||
| z.object({ | ||
| required: z.string(), | ||
| optional: z.string().optional(), | ||
| }), | ||
| 'test', | ||
| ); | ||
|
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Could we also test |
||
| consoleSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('automatically adds properties with defaults to `required`', () => { | ||
| expect( | ||
| zodResponseFormat( | ||
|
|
||
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