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

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

Hierarchy (view full)

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

Constructors

Properties

_configSchema: undefined | C

Used only for typing.

_inputDefinition: I
_outputDefinition: O
_schemaDefinition: StateDefinition
_schemaDefinitions: Map<any, any>

Map schemas to managed values

branches: Record<string, Record<string, Branch<S, N>>>
channels: Record<string, BaseChannel<unknown, unknown, unknown> | ManagedValueSpec>
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

    Returns void

  • Parameters

    • source: BranchOptions<S, N>

    Returns this

  • Parameters

    • source: N
    • path: ((input, config?) => string | Send | (string | Send)[] | Promise<string | Send | (string | Send)[]>)
        • (input, config?): string | Send | (string | Send)[] | Promise<string | Send | (string | Send)[]>
        • Parameters

          • input: S
          • Optional config: RunnableConfig

          Returns string | Send | (string | Send)[] | Promise<string | Send | (string | Send)[]>

    • Optional pathMap: Record<string, "__end__" | N> | ("__end__" | N)[]

    Returns this

  • Parameters

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

    Returns this

  • Type Parameters

    • K extends string
    • NodeInput = S

    Parameters

    • key: K
    • action: RunnableLike<NodeInput, U extends object
          ? U<U> & Record<string, any>
          : U>
    • Optional options: StateGraphAddNodeOptions

    Returns StateGraph<SD, S, U, N | K, I, O, C>

  • 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