Skip to content
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

Structured Outputs fail with gpt4o when Zod schema includes pattern #983

Open
1 task done
jbeoris opened this issue Aug 9, 2024 · 2 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@jbeoris
Copy link

jbeoris commented Aug 9, 2024

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

There is currently an issue when passing a Zod schema containg z.ZodStrings with patterns the new gpt4o structure outputs API with the NodeJS library.

For instance:

const MySchema = z.object({ myString: z.string().includes('my-pattern') })

results in the following error:

BadRequestError: 400 Invalid schema for response_format 'my-schema': In context=('properties', 'myString'), 'pattern' is not permitted

I have temporarily fixed this by wrapping all schemas passed to this endpoint with the following recursive removal code below.

function removeStringIncludes(schema: z.ZodTypeAny): z.ZodTypeAny {
  if (schema instanceof z.ZodString) {
    return z.string();
  } else if (schema instanceof z.ZodObject) {
    const newShape: { [k: string]: z.ZodTypeAny } = {};
    Object.entries(schema.shape).forEach(([key, value]) => {
      newShape[key] = removeStringIncludes(value as z.ZodTypeAny);
    });
    return z.object(newShape);
  } else if (schema instanceof z.ZodArray) {
    return z.array(removeStringIncludes(schema.element));
  } else if (schema instanceof z.ZodEnum) {
    return z.enum(schema._def.values.map((v: any) => removeStringIncludes(v)));
  } else if (schema instanceof z.ZodUnion) {
    return z.union(schema.options.map(removeStringIncludes));
  } else if (schema instanceof z.ZodIntersection) {
    return z.intersection(
      removeStringIncludes(schema._def.left),
      removeStringIncludes(schema._def.right)
    );
  }
  
  return schema;
}

It would be nice for OpenAI to support all schemas, as there are lots of validations baked into Zod schemas that are really convenient and having to work around this and strip them out while passing them to the API is tedious and weakens the power of the almighty Zod.

To Reproduce

pass this schema via zodResponseFormat(mySchema, 'schema') to the new structure outputs endpoint with the sdk.

const MySchema = z.object({ myString: z.string().includes('my-pattern') })'

Code snippets

No response

OS

macOS

Node version

Node v18.18.2

Library version

openai v4.55.0

@jbeoris jbeoris added the bug Something isn't working label Aug 9, 2024
@sreeprasannar
Copy link

what exactly is the problem with this schema? what does it mean?

BadRequestError: 400 Invalid schema for response_format 'my-schema': In context=('properties', 'myString'), 'pattern' is not permitted

does it mean the specific keyword pattern is not permitted?

@peterje
Copy link

peterje commented Aug 23, 2024

what exactly is the problem with this schema? what does it mean?

BadRequestError: 400 Invalid schema for response_format 'my-schema': In context=('properties', 'myString'), 'pattern' is not permitted

does it mean the specific keyword pattern is not permitted?

Yes. pattern is not supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants