StateSchema provides a unified API for defining LangGraph state schemas.
class StateSchemaimport { z } from "zod";
import { StateSchema, ReducedValue, MessagesValue } from "@langchain/langgraph";
const AgentState = new StateSchema({
// Prebuilt messages value
messages: MessagesValue,
// Basic LastValue channel from any standard schema
currentStep: z.string(),
// LastValue with native default
count: z.number().default(0),
// ReducedValue for fields needing reducers
history: new ReducedValue(
z.array(z.string()).default(() => []),
{
inputSchema: z.string(),
reducer: (current, next) => [...current, next],
}
),
});
// Extract types
type State = typeof AgentState.State;
type Update = typeof AgentState.Update;
// Use in StateGraph
const graph = new StateGraph(AgentState);Type declaration for the full state type.
Use: typeof myState.State
Type declaration for the update type.
Use: typeof myState.Update
Get all keys (channels + managed values).
Get the list of channel keys (excluding managed values).
Get the channel definitions for use with StateGraph. This converts the StateSchema fields into BaseChannel instances.
Get the JSON schema for the update/input type. All fields are optional in updates.
Get the JSON schema for the full state type. Used by Studio and API for schema introspection.
Validate input data against the schema. This validates each field using its corresponding schema.
Type guard to check if a value is a ReducedValue instance.