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.
class StateGraphGraph<N, S, U, StateGraphNodeSpec<S, U>, ToStateDefinition<C>>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",
// }The channels in the graph, mapping channel names to their BaseChannel or ManagedValueSpec instances
Set graph-wide default node policies that apply to every node in this graph.
Per-node values passed to addNode always take precedence over these
defaults. Defaults are resolved at compile time, so call order does
not matter — you may call this before or after addNode, including as the
last step before compile(). Calling it multiple times merges the provided
fields, with later calls overriding earlier ones on a per-field basis.
Policies set here are not inherited by subgraphs.
retryPolicy and timeout defaults apply to all nodes, including
auto-generated error-handler nodes. cachePolicy and errorHandler
defaults apply to regular nodes only — caching an error-handler result
is unsafe, and a handler must never catch its own (or another handler's)
failure.
Validates the graph structure to ensure it is well-formed. Checks for:
Validates the graph structure to ensure it is well-formed.