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

Zod => JSONSchema compilation produces infinitely recursive schemas when using transforms #1009

Closed
1 task done
airhorns opened this issue Aug 23, 2024 · 4 comments · Fixed by #1065
Closed
1 task done
Labels
bug Something isn't working

Comments

@airhorns
Copy link

airhorns commented Aug 23, 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

When using a zodFunction with a zod schema that has multiple references to an object with a transformed property, the produced JSON schema is infinitely recursive and invalid. It causes the OpenAI API to 500 when requested.

I believe both of these conditions are necessary to trigger the bug:

  • the schema contains an object with one property that has a .transform attached to it
  • the schema contains references to that object more than once, so the JSONSchema transformer has to pop it out into the definitions

The produced JSONSchema ends up looking like this:

{
  "definitions": {
     "test_properties_first_properties_baz": {
       "$ref": "#/definitions/test_properties_first_properties_baz",
     }
   }
}

which is self-referential and broken, sad.

To Reproduce

Here's a test that reproduces the error:

// use ajv to validate that the produced json schema is valid
import Ajv from "ajv";
const ajv = new Ajv();

  test("unchanged schemas with referenced inner pieces produce valid JSON schemas", () => {
    const Inner = z.object({
      baz: z.boolean().transform((v) => v ?? true),
    });

    const Outer = z.object({
      first: Inner,
      second: Inner,
    });

    const jsonSchema = zodFunction({ name: "test", parameters: Outer }).function.parameters;

    expect(jsonSchema).toBeDefined();
    expect(jsonSchema).toMatchSnapshot();

    ajv.compile(jsonSchema as any);
  });

Code snippets

Here's what the problematic compiled JSONSchema looks like:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "additionalProperties": false,
  "definitions": {
    "test": {
      "additionalProperties": false,
      "properties": {
        "first": {
          "additionalProperties": false,
          "properties": {
            "baz": {
              "type": "boolean",
            },
          },
          "required": [
            "baz",
          ],
          "type": "object",
        },
        "second": {
          "$ref": "#/definitions/test_properties_first",
        },
      },
      "required": [
        "first",
        "second",
      ],
      "type": "object",
    },
    "test_properties_first": {
      "additionalProperties": false,
      "properties": {
        "baz": {
          "$ref": "#/definitions/test_properties_first_properties_baz",
        },
      },
      "required": [
        "baz",
      ],
      "type": "object",
    },
    "test_properties_first_properties_baz": {
      "$ref": "#/definitions/test_properties_first_properties_baz",
    },
  },
  "properties": {
    "first": {
      "additionalProperties": false,
      "properties": {
        "baz": {
          "type": "boolean",
        },
      },
      "required": [
        "baz",
      ],
      "type": "object",
    },
    "second": {
      "$ref": "#/definitions/test_properties_first",
    },
  },
  "required": [
    "first",
    "second",
  ],
  "type": "object",
}

OS

macOS

Node version

node v22.2.0

Library version

openai v4.56.0

@airhorns airhorns added the bug Something isn't working label Aug 23, 2024
@airhorns
Copy link
Author

ping @RobertCraigie sorry to bother just want to make sure you saw this as it seems up your alley!

@RobertCraigie
Copy link
Collaborator

Hey @airhorns, thanks for the bump. I can reproduce this issue and will investigate!

@RobertCraigie
Copy link
Collaborator

This will be fixed in the next release! :) #1066

@airhorns
Copy link
Author

Bless your beautiful heart thank you!

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

Successfully merging a pull request may close this issue.

2 participants