zod4/MessagesZodState: "Invalid element at key "messages": expected a Zod schema" when attempting to extend using shape #333

Closed
opened 2026-02-15 18:15:58 -05:00 by yindo · 3 comments
Owner

Originally created by @fauxbytes on GitHub (Aug 5, 2025).

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangGraph.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 LangGraph.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package).

Example Code

import { z } from 'zod';
import { MessagesZodState } from '@langchain/langgraph';

//works
const UsingExtend = MessagesZodState.extend({});
console.log(UsingExtend.parse({messages: ['foo']}));

//fails
try {
  const UsingShape = z.object({...MessagesZodState.shape});
  console.log(UsingShape.parse({messages: ['foo']}));
} catch (e) {console.log(e);}

Error Message and Stack Trace (if applicable)

{ messages: [ 'foo' ] }
Error: Invalid element at key "messages": expected a Zod schema
    at <snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\schemas.cjs:727:23
    at get value [as value] (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\util.cjs:77:31)
    at inst._zod.parse (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\schemas.cjs:818:39)
    at Object.parse (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\parse.cjs:32:32)
    at inst.parse (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\classic\schemas.cjs:137:42)
    at <anonymous> (<snip>\src\fufu.ts:11:26)
    at Object.<anonymous> (<snip>\src\fufu.ts:12:29)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)

Description

Seems extending the builtin schema using shape and spread operator isn't working. See details.

System Info

$ cat package.json |jq '{dependencies, devDependencies}'
{
  "dependencies": {
    "@langchain/anthropic": "^0.3.25",
    "@langchain/core": "^0.3.66",
    "@langchain/langgraph": "^0.3.12",
    "@langchain/openai": "^0.6.3",
    "@langchain/tavily": "^0.1.4",
    "dotenv": "^17.2.0",
    "langchain": "^0.3.30",
    "sqlite": "^5.1.1",
    "typeorm": "^0.3.25",
    "zod": "^4.0.5"
  },
  "devDependencies": {
    "@types/node": "^24.0.14",
    "ts-node": "^10.9.2",
    "typescript": "^5.8.3"
  }
}

$ pnpm -v
10.11.0

$ node -v
v20.18.3

$ wmic os get caption
Caption
Microsoft Windows 11 Pro
Originally created by @fauxbytes on GitHub (Aug 5, 2025). ### Checked other resources - [x] I added a very descriptive title to this issue. - [x] I searched the LangGraph.js documentation with the integrated search. - [x] I used the GitHub search to find a similar question and didn't find it. - [x] I am sure that this is a bug in LangGraph.js rather than my code. - [x] The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package). ### Example Code ```typescript import { z } from 'zod'; import { MessagesZodState } from '@langchain/langgraph'; //works const UsingExtend = MessagesZodState.extend({}); console.log(UsingExtend.parse({messages: ['foo']})); //fails try { const UsingShape = z.object({...MessagesZodState.shape}); console.log(UsingShape.parse({messages: ['foo']})); } catch (e) {console.log(e);} ``` ### Error Message and Stack Trace (if applicable) ``` { messages: [ 'foo' ] } Error: Invalid element at key "messages": expected a Zod schema at <snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\schemas.cjs:727:23 at get value [as value] (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\util.cjs:77:31) at inst._zod.parse (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\schemas.cjs:818:39) at Object.parse (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\core\parse.cjs:32:32) at inst.parse (<snip>\node_modules\.pnpm\zod@4.0.5\node_modules\zod\v4\classic\schemas.cjs:137:42) at <anonymous> (<snip>\src\fufu.ts:11:26) at Object.<anonymous> (<snip>\src\fufu.ts:12:29) at Module._compile (node:internal/modules/cjs/loader:1469:14) ``` ### Description Seems extending the builtin schema using shape and spread operator isn't working. See details. ### System Info ```bash $ cat package.json |jq '{dependencies, devDependencies}' { "dependencies": { "@langchain/anthropic": "^0.3.25", "@langchain/core": "^0.3.66", "@langchain/langgraph": "^0.3.12", "@langchain/openai": "^0.6.3", "@langchain/tavily": "^0.1.4", "dotenv": "^17.2.0", "langchain": "^0.3.30", "sqlite": "^5.1.1", "typeorm": "^0.3.25", "zod": "^4.0.5" }, "devDependencies": { "@types/node": "^24.0.14", "ts-node": "^10.9.2", "typescript": "^5.8.3" } } $ pnpm -v 10.11.0 $ node -v v20.18.3 $ wmic os get caption Caption Microsoft Windows 11 Pro ```
yindo added the bug label 2026-02-15 18:15:58 -05:00
yindo closed this issue 2026-02-15 18:15:58 -05:00
Author
Owner

@fauxbytes commented on GitHub (Aug 5, 2025):

No issue when downgrading zod to 3.25.76.

@fauxbytes commented on GitHub (Aug 5, 2025): No issue when downgrading `zod` to 3.25.76.
Author
Owner

@Stereobit commented on GitHub (Aug 26, 2025):

I'm having similar issues when using zod 4.x. I'm sharing types across my apps and everything else is using the latest zod version. Would be great if LangGraph could upgrade 🙏🏻

@Stereobit commented on GitHub (Aug 26, 2025): I'm having similar issues when using zod 4.x. I'm sharing types across my apps and everything else is using the latest zod version. Would be great if LangGraph could upgrade 🙏🏻
Author
Owner

@dqbd commented on GitHub (Aug 29, 2025):

Hello! The MessagesZodState is implicitly using Zod 3 (and will be removed in V1).

For Zod 4, please use the following for now

import { BaseMessage } from "@langchain/core/messages";
import { MessagesZodMeta } from "@langchain/langgraph";

import { registry } from "@langchain/langgraph/zod";
import { z } from "zod/v4";

const MessagesZodState = z.object({
  messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta),
});
@dqbd commented on GitHub (Aug 29, 2025): Hello! The `MessagesZodState` is implicitly using Zod 3 (and will be removed in V1). For Zod 4, please use the following for now ```typescript import { BaseMessage } from "@langchain/core/messages"; import { MessagesZodMeta } from "@langchain/langgraph"; import { registry } from "@langchain/langgraph/zod"; import { z } from "zod/v4"; const MessagesZodState = z.object({ messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta), }); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#333