Can not export graphs with Zod state schemas without type annotation #231

Closed
opened 2026-02-15 17:17:39 -05:00 by yindo · 2 comments
Owner

Originally created by @bracesproul on GitHub (Apr 10, 2025).

This error is shown when you try to export a graph with a Zod schemas as the state schema

Code:

import "@langchain/langgraph/zod";
import { z } from "zod";
import { START, StateGraph } from "@langchain/langgraph";
import { reflect } from "./nodes/reflect.js";

export const ReflectionZodState = z.object({
  // add state fields here
});

const workflow = new StateGraph(ReflectionZodState)
  .addNode("reflect", reflect)
  .addEdge(START, "reflect")

export const graph = workflow.compile();
graph.name = "Reflection Graph";

The inferred type of 'graph' cannot be named without a reference to '../../node_modules/@langchain/langgraph/dist/graph/zod/state.js'. This is likely not portable. A type annotation is necessary.ts(2742)
const graph: CompiledStateGraph<StateType<ZodToStateDefinition<z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>>, UpdateType<ZodToStateDefinition<z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>>, "__start__" | "reflect", ZodToStateDefinition<...>, ZodToStateDefinition<...>, StateDefinition>
Originally created by @bracesproul on GitHub (Apr 10, 2025). This error is shown when you try to export a graph with a Zod schemas as the state schema Code: ```typescript import "@langchain/langgraph/zod"; import { z } from "zod"; import { START, StateGraph } from "@langchain/langgraph"; import { reflect } from "./nodes/reflect.js"; export const ReflectionZodState = z.object({ // add state fields here }); const workflow = new StateGraph(ReflectionZodState) .addNode("reflect", reflect) .addEdge(START, "reflect") export const graph = workflow.compile(); graph.name = "Reflection Graph"; ``` ```txt The inferred type of 'graph' cannot be named without a reference to '../../node_modules/@langchain/langgraph/dist/graph/zod/state.js'. This is likely not portable. A type annotation is necessary.ts(2742) const graph: CompiledStateGraph<StateType<ZodToStateDefinition<z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>>, UpdateType<ZodToStateDefinition<z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>>>, "__start__" | "reflect", ZodToStateDefinition<...>, ZodToStateDefinition<...>, StateDefinition> ```
yindo closed this issue 2026-02-15 17:17:39 -05:00
Author
Owner

@dqbd commented on GitHub (May 19, 2025):

Cannot reproduce the issue, I believe this issue stems from possible duplicate versions of zod and/or tsconfig.json configuration.

For anyone encountering issue, would it be possible to attach both the package manager lockfile (+ package.json if working on Bun) and the used TypeScript version (might differ if using VSCode's built-in version)

@dqbd commented on GitHub (May 19, 2025): Cannot reproduce the issue, I believe this issue stems from possible duplicate versions of `zod` and/or `tsconfig.json` configuration. For anyone encountering issue, would it be possible to attach both the package manager lockfile (+ `package.json` if working on Bun) and the used TypeScript version (might differ if using VSCode's built-in version)
Author
Owner

@xxuun commented on GitHub (Nov 25, 2025):

Faced same error.

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.ts",
  "type": "module",
  "scripts": {
    "dev": "tsx main.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "packageManager": "pnpm@10.20.0",
  "devDependencies": {
    "tsx": "^4.20.6",
    "typescript": "^5.9.3"
  },
  "dependencies": {
    "@langchain/core": "^1.1.0",
    "@langchain/langgraph": "^1.0.2",
    "zod": "^4.1.13"
  }
}

tsconfig.json

{
  // Visit https://aka.ms/tsconfig to read more about this file
  "compilerOptions": {
    // File Layout
    // "rootDir": "./src",
    // "outDir": "./dist",

    // Environment Settings
    // See also https://aka.ms/tsconfig/module
    "module": "nodenext",
    "target": "esnext",
    "types": [],
    // For nodejs:
    // "lib": ["esnext"],
    // "types": ["node"],
    // and npm install -D @types/node

    // Other Outputs
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,

    // Stricter Typechecking Options
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,

    // Style Options
    // "noImplicitReturns": true,
    // "noImplicitOverride": true,
    // "noUnusedLocals": true,
    // "noUnusedParameters": true,
    // "noFallthroughCasesInSwitch": true,
    // "noPropertyAccessFromIndexSignature": true,

    // Recommended Options
    "strict": true,
    "jsx": "react-jsx",
    "verbatimModuleSyntax": true,
    "isolatedModules": true,
    "noUncheckedSideEffectImports": true,
    "moduleDetection": "force",
    "skipLibCheck": true,
  }
}

lib.ts

import { StateGraph, START, END } from "@langchain/langgraph";
import * as z from "zod";

const State = z.object({});

type StateType = z.infer<typeof State>;

async function testNode(state: StateType) {
  console.log("node test", state);
}

export const graph = new StateGraph(State)
  .addNode("node", testNode)
  .addEdge(START, "node")
  .addEdge("node", END)
  .compile();

main.ts

import { graph } from "./lib.js";

graph.invoke({});

It works when I run the command: pnpm dev, but the editor is not happy:

Image
@xxuun commented on GitHub (Nov 25, 2025): Faced same error. package.json ```json { "name": "test", "version": "1.0.0", "description": "", "main": "main.ts", "type": "module", "scripts": { "dev": "tsx main.ts", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "packageManager": "pnpm@10.20.0", "devDependencies": { "tsx": "^4.20.6", "typescript": "^5.9.3" }, "dependencies": { "@langchain/core": "^1.1.0", "@langchain/langgraph": "^1.0.2", "zod": "^4.1.13" } } ``` tsconfig.json ```json { // Visit https://aka.ms/tsconfig to read more about this file "compilerOptions": { // File Layout // "rootDir": "./src", // "outDir": "./dist", // Environment Settings // See also https://aka.ms/tsconfig/module "module": "nodenext", "target": "esnext", "types": [], // For nodejs: // "lib": ["esnext"], // "types": ["node"], // and npm install -D @types/node // Other Outputs "sourceMap": true, "declaration": true, "declarationMap": true, // Stricter Typechecking Options "noUncheckedIndexedAccess": true, "exactOptionalPropertyTypes": true, // Style Options // "noImplicitReturns": true, // "noImplicitOverride": true, // "noUnusedLocals": true, // "noUnusedParameters": true, // "noFallthroughCasesInSwitch": true, // "noPropertyAccessFromIndexSignature": true, // Recommended Options "strict": true, "jsx": "react-jsx", "verbatimModuleSyntax": true, "isolatedModules": true, "noUncheckedSideEffectImports": true, "moduleDetection": "force", "skipLibCheck": true, } } ``` lib.ts ```typescript import { StateGraph, START, END } from "@langchain/langgraph"; import * as z from "zod"; const State = z.object({}); type StateType = z.infer<typeof State>; async function testNode(state: StateType) { console.log("node test", state); } export const graph = new StateGraph(State) .addNode("node", testNode) .addEdge(START, "node") .addEdge("node", END) .compile(); ``` main.ts ```typescript import { graph } from "./lib.js"; graph.invoke({}); ``` It works when I run the command: `pnpm dev`, but the editor is not happy: <img width="734" height="469" alt="Image" src="https://github.com/user-attachments/assets/ce28a2a3-4aeb-49b1-a185-ec9fc9a85e30" />
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#231