-
Couldn't load subscription status.
- 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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| } | ||
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.