-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Open
ro0sterjam
wants to merge
1
commit into
langchain-ai:main
Choose a base branch
from
ro0sterjam:fix-transform-input-schema-mutate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ export type ZodObjectV3 = z3.ZodObject<any, any, any, any>; | |
|
||
export type ZodObjectV4 = z4.$ZodObject; | ||
|
||
export type ZodTypeV4 = z4.$ZodType; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type InteropZodType<Output = any, Input = Output> = | ||
| z3.ZodType<Output, z3.ZodTypeDef, Input> | ||
|
@@ -748,7 +750,9 @@ export function interopZodTransformInputSchema( | |
if (recursive) { | ||
// Handle nested object schemas | ||
if (isZodObjectV4(outputSchema)) { | ||
const outputShape: Mutable<z4.$ZodShape> = outputSchema._zod.def.shape; | ||
const outputShape: Mutable<z4.$ZodShape> = { | ||
...outputSchema._zod.def.shape, | ||
}; | ||
Comment on lines
+753
to
+755
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Primary fix here. |
||
for (const [key, keySchema] of Object.entries( | ||
outputSchema._zod.def.shape | ||
)) { | ||
|
@@ -781,3 +785,27 @@ export function interopZodTransformInputSchema( | |
|
||
throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType"); | ||
} | ||
|
||
/** | ||
* Sanitizes a Zod schema by transforming it to its input schema and then making it strict. | ||
* Supports both Zod v3 and v4 schemas. If `recursive` is true, applies strictness recursively to all nested object schemas and arrays of object schemas. | ||
* | ||
* @param schema - The Zod schema instance (v3 or v4) | ||
* @param {boolean} [recursive=false] - Whether to recursively process nested objects/arrays. | ||
* @returns The sanitized Zod schema. | ||
*/ | ||
export function interopZodSanitizeSchema( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted so that it can be called independently here. |
||
schema: InteropZodType, | ||
recursive: boolean = false | ||
): InteropZodType { | ||
const inputSchema = interopZodTransformInputSchema(schema, recursive); | ||
if (isZodObjectV4(inputSchema)) { | ||
const strictSchema = interopZodObjectStrict( | ||
inputSchema, | ||
recursive | ||
) as ZodObjectV4; | ||
return strictSchema; | ||
} else { | ||
return inputSchema; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Am I correct to assume that
toJSONSchema(schema)
was a typo and that it should have beentoJSONSchema(inputSchema)
?If not, please let me know, as this PR effectively makes that change.