openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

Allow specifying `definitions` as a `zodResponseFormat` param

Open rpavlovs opened this issue 9 months ago • 0 comments

Confirm this is a feature request for the Node library and not the underlying OpenAI API.

  • [x] This is a feature request for the Node library

Describe the feature or improvement you're requesting

Allow specifying schemaDefinitions as a zodResponseFormat param

Proposed signature

const schemaDefinitions = {
  foo: z.enum('one', 'two', 'three', ....)  // 200 elements
  bar: z.enum('a', 'b', 'c', ...)  // 200 elements
)

const responseFormat = zodResponseFormat(
  z.object({
    foo: schemaDefinitions.foo
    foos: z.array[schemaDefinitions.foo]
    bar: schemaDefinitions.foo
    bars: z.array[schemaDefinitions.bar]
  }),
 { name: 'fooBar', schemaDefinitions }
)

Additional context

❌ Without definitions this schema produces 800 enums, which is above the 500 enums OpenAI limit. First occurrence of a def is left in place, starting with the second one it's copied to definitions and a reference is used.

zod-to-json-schema library has a definitions option that is used to supply definitions, and avoid duplication – resulting in 400 enums.

Alternative approach would be to detect shared definitions, move out to definitions, and replace with refs automatically. This would be a significantly more complex, but easier to use solution. Probably not worth the work for now, since everything is in place to just expose the zod-to-json-schema's definitions option

rpavlovs avatar Jan 23 '25 19:01 rpavlovs