LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangGraph
  • Web
  • Channels
  • Pregel
  • Prebuilt
  • Remote
LangGraph SDK
  • Client
  • Auth
  • React
  • Logging
  • React Ui
  • Server
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
  • Store
LangGraph Checkpoint Redis
  • Shallow
  • Store
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
  • Cli
LangGraph API
LangGraph CLI
LangGraph CUA
  • Utils
LangGraph Supervisor
LangGraph Swarm
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangGraph
WebChannelsPregelPrebuiltRemote
LangGraph SDK
ClientAuthReactLoggingReact UiServer
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
Store
LangGraph Checkpoint Redis
ShallowStore
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
Cli
LangGraph API
LangGraph CLI
LangGraph CUA
Utils
LangGraph Supervisor
LangGraph Swarm
Language
Theme
JavaScript@langchain/langgraphindexGraphNode
Type●Since v0.3

GraphNode

Strongly-typed utility for authoring graph nodes outside of the StateGraph builder, supporting inference for both state (from Schema) and config context (from Context type).

This type enables you to define graph node functions with full type safety—both for the evolving state and for additional context that may be passed in at runtime. Typing the context parameter allows for better code organization and precise editor support.

Works with StateSchema, AnnotationRoot, and Zod object schemas for state, and with a user-defined object shape for context.

Supports two patterns:

  1. Single schema usage - Single schema for both input and output: GraphNode<Schema, Context, Nodes>

  2. Type bag pattern - Separate schemas for input, output, context: GraphNode<{ InputSchema; OutputSchema; ContextSchema; Nodes }>

Copy
GraphNode: IsGraphNodeTypeBag<
  Schema
> extends true  (
  state: ExtractBagInput<Schema, unknown>,
  config: LangGraphRunnableConfig<ExtractBagContext<Schema, Record<string, unknown>>>) => GraphNodeReturnValue<ExtractBagOutput<Schema,
  Partial<ExtractBagInput<Schema, unknown>>>, ExtractBagNodes<Schema,
  string>> : (state: ExtractStateType<Schema>, config: LangGraphRunnableConfig<Context>
) => GraphNodeReturnValue<ExtractUpdateType<Schema>, Nodes>

Used in Docs

  • Build a custom SQL agent
  • Frontend
  • Graph API overview
  • Handoffs
  • INVALID_GRAPH_NODE_RETURN_VALUE

Example 1

Copy
import { StateSchema, GraphNode } from "@langchain/langgraph";
import { z } from "zod/v4";

const AgentState = new StateSchema({
  messages: MessagesValue,
  step: z.number().default(0),
});

// Context shape for custom node logic (optional)
type MyContext = { userId: string };

// Node receiving state and config
const processNode: GraphNode<typeof AgentState, MyContext> = (state, config) => {
  const userId = config.configurable?.userId; // type-safe context access
  return { step: state.step + 1 };
};

// Node with type-safe graph routing
const routerNode: GraphNode<typeof AgentState, MyContext, "agent" | "tool"> = (state, config) => {
  if (state.needsTool) {
    return new Command({ goto: "tool", update: { step: state.step + 1 } });
  }
  return new Command({ goto: "agent" });
};

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

Example 2

Copy
const InputSchema = new StateSchema({
  messages: z.array(z.string()),
  query: z.string(),
});

const OutputSchema = new StateSchema({
  answer: z.string(),
});

const ContextSchema = z.object({ userId: z.string() });

const node: GraphNode<{
  InputSchema: typeof InputSchema;
  OutputSchema: typeof OutputSchema;
  ContextSchema: typeof ContextSchema;
  Nodes: "agent" | "tool";
}> = (state, config) => {
  // state is { messages: string[]; query: string }
  // config.configurable is { userId: string } | undefined
  return { answer: `Response to: ${state.query}` };
};
View source on GitHub