-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(langchain): Fix toJsonSchema mutating underlying zod schema #9125
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: main
Are you sure you want to change the base?
fix(langchain): Fix toJsonSchema mutating underlying zod schema #9125
Conversation
|
|
@ro0sterjam is attempting to deploy a commit to the LangChain Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
a6b992c to
047002c
Compare
|
Really appreciate you taking the time to open this PR, @ro0sterjam 🙏 |
2313a71 to
669dc5e
Compare
| // Handle nested object schemas | ||
| if (isZodObjectV4(outputSchema)) { | ||
| const outputShape: Mutable<z4.$ZodShape> = outputSchema._zod.def.shape; | ||
| const outputShape: Mutable<z4.$ZodShape> = {}; |
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.
primary fix here
669dc5e to
15cd573
Compare
| name, | ||
| strict: true, | ||
| schema: toJSONSchemaV4(zodSchema, { | ||
| schema: toJsonSchema(zodSchema, { |
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.
Call the version of toJsonSchema that also strips off transforms, since we can no longer assume that that has already been done for us by this point.
In
interopZodTransformInputSchema, need to make a shallow copy of the schema before replacing the properties rather than replacing in place to prevent mutation of underlying schema.Doing so however breaks the OpenAI integration because it takes advantage of the underlying schema being mutated as part of the first call to
toJsonSchema. Thus when it reachestoJSONSchemaV4from zod, it already assumes that the transforms have been stripped.Since this PR fixes the underlying mutation, this assumption no longer holds, so I have to manually make sure that the downstream schema passed to zod's
toJSONSchemaV4runs through the transform stripping logic.Note: I was unable to determine how best to write a unit/integration test for OpenAI to ensure that it works with
transformon schemas. But I was able to validate live.Fixes #9100