Skip to content

Conversation

@404Wolf
Copy link

@404Wolf 404Wolf commented Dec 4, 2025

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Fixes #1709

Additional context & links

@404Wolf 404Wolf changed the title Add json schema transformation logic fix: add json schema transformation logic Dec 4, 2025
Comment on lines 29 to 59
if (Array.isArray(anyOf)) {
jsonSchema['anyOf'] = anyOf.map((variant) => _transformJSONSchema(variant as JSONSchema));
} else if (Array.isArray(oneOf)) {
jsonSchema['anyOf'] = oneOf.map((variant) => _transformJSONSchema(variant as JSONSchema));
} else if (Array.isArray(allOf)) {
jsonSchema['allOf'] = allOf.map((entry) => _transformJSONSchema(entry as JSONSchema));
} else {
if (type === undefined) {
throw new Error('JSON schema must have a type defined if anyOf/oneOf/allOf are not used');
}

switch (type) {
case 'object': {
const properties = pop(jsonSchema, 'properties') || {};
jsonSchema['properties'] = Object.fromEntries(
Object.entries(properties).map(([key, propSchema]) => [
key,
_transformJSONSchema(propSchema as JSONSchema),
]),
);
break;
}
case 'array': {
const items = pop(jsonSchema, 'items');
if (items !== undefined) {
jsonSchema['items'] = _transformJSONSchema(items as JSONSchema);
}
break;
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember why we originally considered these mutually exclusive, but I think we shouldn't, json schema is wild and all of these properties could be set at the same time.

Copy link
Author

@404Wolf 404Wolf Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is crazy, yes it seems to be true huh. There is also "not"

}
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're missing a case for additionalProperties as well, which is boolean | Schema

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically makes it part of the public API, but I don't think we should make it public until we've decided on the "unsupported properties to description" behaviour, could you move this to src/lib/internal or src/internal?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I'll move it!

Comment on lines +34 to +37
const shouldHaveType = [anyOf, oneOf, allOf, not].some(Array.isArray);
if (!shouldHaveType && type === undefined) {
throw new Error('JSON schema must have a type defined if anyOf/oneOf/allOf are not used');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: whats the value provided in validating this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was for consistency with what we do in anthropic, but yes we don't have to do this to get rid of all the anyOfs

}

switch (type) {
case 'object': {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we shouldn't branch on the type here, properties could in theory be set when type is not for example, we can just ignore the type entirely imo

$refStrategy: 'extract-to-root',
nullableStrategy: 'property',
});
return transformJSONSchema(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: is this necessary for v3? maybe it would make sense for consistency but I think we'll already do the oneOf -> anyOf transform in the vendored to-json-schema library we use (I could be wrong)

}) as JSONSchema,
) as Record<string, unknown>;
return transformJSONSchema(
toStrictJsonSchema(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait we already have toStrictJsonSchema() 🤦 we should use that instead of introducing the new transformJSONSchema() function

Copy link
Author

@404Wolf 404Wolf Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh yeah you're right.

It looks like it also checks that the root is an object, which is slightly different behavior between v3 and v4, and if we change it users will now get vanilla Errors rather than 400s OpenAIErrors. I think that that's probably fine though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zod schema conversion broken for unions in Zod 4.1.13+

2 participants