Skip to content

interopZodTransformInputSchema not stripping transform functions when wrapped around an optional def #9120

@ro0sterjam

Description

@ro0sterjam

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { type InteropZodType, interopZodTransformInputSchema } from "@langchain/core/utils/types";
import { z } from "zod/v4";

const schemaWithOptional = z.object({
  messages: z.array(z.string().transform((x) => x.toUpperCase())).optional(),
});

const schemaWithoutOptional = z.object({
  messages: z.array(z.string().transform((x) => x.toUpperCase())),
});

interopZodTransformInputSchema(schemaWithOptional as unknown as InteropZodType, true);
interopZodTransformInputSchema(schemaWithoutOptional as unknown as InteropZodType, true);

const message = { messages: ["hello"] };

console.log(schemaWithOptional.parse(message)); // Logs { message: [ 'HELLO' ] }
console.log(schemaWithoutOptional.parse(message)); // Logs { message: [ 'hello' ] }

Error Message and Stack Trace (if applicable)

No response

Description

interopZodTransformInputSchema is suppose to strip the transform functions from a schema, but when an object or array is wrapped with an optional, the current logic seems to skip over this element and thus does not strip any nested transforms under it.

I believe it should be a relatively trivial fix by adding the addition check for optional here.

Something like:

// Handle nested optional schemas
else if (isZodOptionalV4(outputSchema)) {
  const innerType = interopZodTransformInputSchema(
    outputSchema._zod.def.innerType,
    recursive
  ) as z4.$ZodType;
  outputSchema = clone<z4.$ZodOptional>(outputSchema, {
    ...outputSchema._zod.def,
    innerType,
  });
}

System Info

Langchain 0.3.35
Node v24.5.0
OSX 15.6.1 (24G90)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions