-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: add json schema transformation logic #1715
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
base: master
Are you sure you want to change the base?
Conversation
src/lib/transform-json-schema.ts
Outdated
| 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; | ||
| } | ||
| } | ||
| } |
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.
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.
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.
This is crazy, yes it seems to be true huh. There is also "not"
src/lib/transform-json-schema.ts
Outdated
| } | ||
| } | ||
| } | ||
|
|
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.
I think you're missing a case for additionalProperties as well, which is boolean | Schema
src/lib/transform-json-schema.ts
Outdated
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.
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?
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.
Makes sense. I'll move it!
| 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'); | ||
| } |
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.
q: whats the value provided in validating this?
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.
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': { |
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.
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( |
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.
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( |
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.
oh wait we already have toStrictJsonSchema() 🤦 we should use that instead of introducing the new transformJSONSchema() function
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.
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
Changes being requested
Fixes #1709
Additional context & links