Class StateGraph<SD, S, U, N, I, O, C, NodeReturnType>

A graph whose nodes communicate by reading and writing to a shared state. Each node takes a defined State as input and returns a Partial<State>.

Each state key can optionally be annotated with a reducer function that will be used to aggregate the values of that key received from multiple nodes. The signature of a reducer function is (left: Value, right: UpdateValue) => Value.

See Annotation for more on defining state.

After adding nodes and edges to your graph, you must call .compile() on it before you can use it.

Example

import {
type BaseMessage,
AIMessage,
HumanMessage,
} from "@langchain/core/messages";
import { StateGraph, Annotation } from "@langchain/langgraph";

// Define a state with a single key named "messages" that will
// combine a returned BaseMessage or arrays of BaseMessages
const StateAnnotation = Annotation.Root({
sentiment: Annotation<string>,
messages: Annotation<BaseMessage[]>({
reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
if (Array.isArray(right)) {
return left.concat(right);
}
return left.concat([right]);
},
default: () => [],
}),
});

const graphBuilder = new StateGraph(StateAnnotation);

// A node in the graph that returns an object with a "messages" key
// will update the state by combining the existing value with the returned one.
const myNode = (state: typeof StateAnnotation.State) => {
return {
messages: [new AIMessage("Some new response")],
sentiment: "positive",
};
};

const graph = graphBuilder
.addNode("myNode", myNode)
.addEdge("__start__", "myNode")
.addEdge("myNode", "__end__")
.compile();

await graph.invoke({ messages: [new HumanMessage("how are you?")] });

// {
// messages: [HumanMessage("how are you?"), AIMessage("Some new response")],
// sentiment: "positive",
// }

Type Parameters

  • SD extends SDZod | unknown
  • S = SD extends SDZod
        ? StateType<ToStateDefinition<SD>>
        : SD
  • U = SD extends SDZod
        ? UpdateType<ToStateDefinition<SD>>
        : Partial<S>
  • N extends string = typeof START
  • I extends SDZod = SD extends SDZod
        ? ToStateDefinition<SD>
        : StateDefinition
  • O extends SDZod = SD extends SDZod
        ? ToStateDefinition<SD>
        : StateDefinition
  • C extends SDZod = StateDefinition
  • NodeReturnType = unknown

Hierarchy (view full)

  • Graph<N, S, U, StateGraphNodeSpec<S, U>, ToStateDefinition<C>>
    • StateGraph

Constructors

Properties

branches: Record<string, Record<string, Branch<S, N, any>>>
channels: Record<string, BaseChannel<unknown, unknown, unknown>>
compiled: boolean
edges: Set<["__start__" | N, "__end__" | N]>
entryPoint?: string
nodes: Record<N, StateGraphNodeSpec<S, U>>
waitingEdges: Set<[N[], N]>

Accessors

  • get allEdges(): Set<[string, string]>
  • Returns Set<[string, string]>

Methods

  • Parameters

    • stateDefinition: SDZod

    Returns void

  • Parameters

    • startKey: "__start__" | N | N[]
    • endKey: "__end__" | N

    Returns this

  • Parameters

    • Optional __namedParameters: {
          cache?: BaseCache<unknown>;
          checkpointer?: boolean | BaseCheckpointSaver<number>;
          description?: string;
          interruptAfter?: "*" | N[];
          interruptBefore?: "*" | N[];
          name?: string;
          store?: BaseStore;
      }
      • Optional cache?: BaseCache<unknown>
      • Optional checkpointer?: boolean | BaseCheckpointSaver<number>
      • Optional description?: string
      • Optional interruptAfter?: "*" | N[]
      • Optional interruptBefore?: "*" | N[]
      • Optional name?: string
      • Optional store?: BaseStore

    Returns CompiledStateGraph<{
        [K in string | number | symbol]: S[K]
    }, {
        [K in string | number | symbol]: U[K]
    }, N, I, O, C, NodeReturnType>

  • Parameters

    • key: N

    Returns this

    Deprecated

    use addEdge(START, key) instead

  • Parameters

    • key: N

    Returns this

    Deprecated

    use addEdge(key, END) instead

  • Parameters

    • Optional interrupt: string[]

    Returns void

  • Parameters

    • message: string

    Returns void