Const
import { MessagesZodState, StateGraph } from "@langchain/langgraph";
const graph = new StateGraph(MessagesZodState)
.addNode(...)
...
Which is equivalent to initializing the schema object manually like this:
import { z } from "zod";
import type { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
import { StateGraph, messagesStateReducer } from "@langchain/langgraph";
import "@langchain/langgraph/zod";
const AgentState = z.object({
messages: z
.custom<BaseMessage[]>()
.default(() => [])
.langgraph.reducer(
messagesStateReducer,
z.custom<BaseMessageLike | BaseMessageLike[]>()
),
});
const graph = new StateGraph(AgentState)
.addNode(...)
...
Prebuilt state object that uses Zod to combine returned messages. This utility is synonymous with the
MessagesAnnotation
annotation, but uses Zod as the way to express messages state.You can use import and use this prebuilt schema like this: