[PR #1852] [MERGED] feat(langgraph): add type utilities #1779

Closed
opened 2026-02-15 20:16:48 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/langgraphjs/pull/1852
Author: @hntrl
Created: 1/8/2026
Status: Merged
Merged: 1/15/2026
Merged by: @hntrl

Base: mainHead: hunter/graph-node-types


📝 Commits (10+)

📊 Changes

11 files changed (+1048 additions, -63 deletions)

View changed files

.changeset/graph-node-type-utilities.md (+41 -0)
📝 .changeset/tame-houses-happen.md (+43 -2)
📝 libs/langgraph-core/src/constants.ts (+2 -2)
📝 libs/langgraph-core/src/graph/index.ts (+6 -0)
📝 libs/langgraph-core/src/graph/state.ts (+15 -48)
libs/langgraph-core/src/graph/types.ts (+191 -0)
📝 libs/langgraph-core/src/prebuilt/react_agent_executor.ts (+8 -7)
📝 libs/langgraph-core/src/pregel/loop.ts (+5 -1)
📝 libs/langgraph-core/src/state/schema.ts (+96 -3)
libs/langgraph-core/src/tests/types.test-d.ts (+637 -0)
📝 libs/langgraph-core/src/web.ts (+4 -0)

📄 Description

Adds utility types for defining typed graph nodes outside of the StateGraph builder, improving code organization and editor support.

Changes

New Type Utilities

libs/langgraph-core/src/graph/types.ts (new file)

  • ExtractStateType<Schema> - Extracts the state type from StateSchema, AnnotationRoot, or Zod object schemas
  • ExtractUpdateType<Schema> - Extracts the update type (what nodes can return)
  • GraphNode<Schema, Nodes?, Config?> - Type for node functions, with optional typed Command.goto routing
  • ConditionalEdgeRouter<Schema, Nodes> - Type for conditional edge routing functions

Updates

  • StateSchema - Added Node type declaration for parity with AnnotationRoot.Node
  • StateGraph - Now imports shared ExtractStateType/ExtractUpdateType from types.ts
  • Exports - New types exported from graph/index.ts and web.ts

Tests

  • Comprehensive type-level tests in types.test-d.ts covering all new utilities with StateSchema, Annotation, and Zod schemas

Usage

import { StateGraph, GraphNode, Annotation } from "@langchain/langgraph";

const AgentState = Annotation.Root({
  messages: Annotation<BaseMessage[]>({ reducer: messagesReducer }),
  step: Annotation<number>({ default: () => 0 }),
});

// Define typed nodes outside the graph builder
const processNode: GraphNode<typeof AgentState> = (state, config) => {
  return { step: state.step + 1 };
};

// With typed Command routing
const routerNode: GraphNode<typeof AgentState, "agent" | "tool"> = (state) => {
  if (state.needsTool) {
    return new Command({ goto: "tool" }); // ✓ Type-safe
  }
  return new Command({ goto: "agent" });
};

// Build graph
const graph = new StateGraph(AgentState)
  .addNode("process", processNode)
  .addNode("router", routerNode)
  .compile();

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/langgraphjs/pull/1852 **Author:** [@hntrl](https://github.com/hntrl) **Created:** 1/8/2026 **Status:** ✅ Merged **Merged:** 1/15/2026 **Merged by:** [@hntrl](https://github.com/hntrl) **Base:** `main` ← **Head:** `hunter/graph-node-types` --- ### 📝 Commits (10+) - [`472b195`](https://github.com/langchain-ai/langgraphjs/commit/472b1954e58052dba5000eb8f03a27889c2ce7bd) cr - [`9d9a862`](https://github.com/langchain-ai/langgraphjs/commit/9d9a8625c2d20cb77b9c201c6fa2d7fe16e4f933) pin zod - [`d1ecc37`](https://github.com/langchain-ai/langgraphjs/commit/d1ecc3709bbe8321c893e4119a9098337f77d8b0) feat(langgraph-core): add type utilities - [`b78ea61`](https://github.com/langchain-ai/langgraphjs/commit/b78ea61bd06a8e22131f71842000e4df6ea1cd09) cr - [`5ebaf62`](https://github.com/langchain-ai/langgraphjs/commit/5ebaf625d7129cb8b0a5b9a8e3b48f39495adbac) cr - [`5b2d488`](https://github.com/langchain-ai/langgraphjs/commit/5b2d488efa76d49ef10221e6145cf7873fa48c3a) cr - [`2a04527`](https://github.com/langchain-ai/langgraphjs/commit/2a045273be82cf06d6fda37743b7f87b6660e39a) cr - [`4929ec7`](https://github.com/langchain-ai/langgraphjs/commit/4929ec7692b3e115a7f7e8fe2f8128bdf8b60b49) cr - [`f0192d3`](https://github.com/langchain-ai/langgraphjs/commit/f0192d3383ab491329a2aa568fd9cfb339235e2a) cr - [`5dc51d7`](https://github.com/langchain-ai/langgraphjs/commit/5dc51d746d5afef0119530b6a9c0f9eaa665423c) cr ### 📊 Changes **11 files changed** (+1048 additions, -63 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/graph-node-type-utilities.md` (+41 -0) 📝 `.changeset/tame-houses-happen.md` (+43 -2) 📝 `libs/langgraph-core/src/constants.ts` (+2 -2) 📝 `libs/langgraph-core/src/graph/index.ts` (+6 -0) 📝 `libs/langgraph-core/src/graph/state.ts` (+15 -48) ➕ `libs/langgraph-core/src/graph/types.ts` (+191 -0) 📝 `libs/langgraph-core/src/prebuilt/react_agent_executor.ts` (+8 -7) 📝 `libs/langgraph-core/src/pregel/loop.ts` (+5 -1) 📝 `libs/langgraph-core/src/state/schema.ts` (+96 -3) ➕ `libs/langgraph-core/src/tests/types.test-d.ts` (+637 -0) 📝 `libs/langgraph-core/src/web.ts` (+4 -0) </details> ### 📄 Description Adds utility types for defining typed graph nodes outside of the `StateGraph` builder, improving code organization and editor support. ## Changes ### New Type Utilities **`libs/langgraph-core/src/graph/types.ts`** (new file) - **`ExtractStateType<Schema>`** - Extracts the state type from `StateSchema`, `AnnotationRoot`, or Zod object schemas - **`ExtractUpdateType<Schema>`** - Extracts the update type (what nodes can return) - **`GraphNode<Schema, Nodes?, Config?>`** - Type for node functions, with optional typed `Command.goto` routing - **`ConditionalEdgeRouter<Schema, Nodes>`** - Type for conditional edge routing functions ### Updates - **`StateSchema`** - Added `Node` type declaration for parity with `AnnotationRoot.Node` - **`StateGraph`** - Now imports shared `ExtractStateType`/`ExtractUpdateType` from `types.ts` - **Exports** - New types exported from `graph/index.ts` and `web.ts` ### Tests - Comprehensive type-level tests in `types.test-d.ts` covering all new utilities with `StateSchema`, `Annotation`, and Zod schemas --- ## Usage ```typescript import { StateGraph, GraphNode, Annotation } from "@langchain/langgraph"; const AgentState = Annotation.Root({ messages: Annotation<BaseMessage[]>({ reducer: messagesReducer }), step: Annotation<number>({ default: () => 0 }), }); // Define typed nodes outside the graph builder const processNode: GraphNode<typeof AgentState> = (state, config) => { return { step: state.step + 1 }; }; // With typed Command routing const routerNode: GraphNode<typeof AgentState, "agent" | "tool"> = (state) => { if (state.needsTool) { return new Command({ goto: "tool" }); // ✓ Type-safe } return new Command({ goto: "agent" }); }; // Build graph const graph = new StateGraph(AgentState) .addNode("process", processNode) .addNode("router", routerNode) .compile(); ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-15 20:16:48 -05:00
yindo closed this issue 2026-02-15 20:16:48 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#1779