langchain.js
    Preparing search index...

    Class StateSchema<TFields>

    StateSchema provides a unified API for defining LangGraph state schemas.

    import { 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 Parameters

    Index

    Constructors

    Properties

    fields: TFields
    Node: RunnableLike<
        InferStateSchemaValue<TFields>,
        InferStateSchemaUpdate<TFields>,
    >

    Type declaration for node functions. Use: typeof myState.Node to type node functions outside the graph builder.

    const AgentState = new StateSchema({
    count: z.number().default(0),
    });

    const myNode: typeof AgentState.Node = (state) => {
    return { count: state.count + 1 };
    };

    Type declaration for the full state type. Use: typeof myState.State

    Type declaration for the update type. Use: typeof myState.Update

    Methods

    • Get all keys (channels + managed values).

      Returns string[]

    • Get the list of channel keys (excluding managed values).

      Returns string[]

    • Get the channel definitions for use with StateGraph. This converts the StateSchema fields into BaseChannel instances.

      Returns Record<string, BaseChannel>

    • Get the JSON schema for the update/input type. All fields are optional in updates.

      Returns JsonSchema7Type

    • Get the JSON schema for the full state type. Used by Studio and API for schema introspection.

      Returns JsonSchema7Type

    • Validate input data against the schema. This validates each field using its corresponding schema.

      Type Parameters

      • T

      Parameters

      • data: T

        The input data to validate

      Returns Promise<T>

      The validated data with coerced types

    • Type guard to check if a value is a StateSchema instance.

      Type Parameters

      Parameters

      Returns value is StateSchema<TFields>

      True if the value is a StateSchema instance with the correct runtime tag.

    • Type guard to check if a value is a StateSchema instance.

      Parameters

      • value: unknown

        The value to check.

      Returns value is StateSchema<any>

      True if the value is a StateSchema instance with the correct runtime tag.