langchain.js
    Preparing search index...

    Type Alias ConditionalEdgeRouter<Schema, Context, Nodes>

    ConditionalEdgeRouter: IsConditionalEdgeRouterTypeBag<Schema> extends true
        ? (
            state: ExtractBagInput<Schema, unknown>,
            config: LangGraphRunnableConfig<
                ExtractBagContext<Schema, Record<string, unknown>>,
            >,
        ) =>
            | ConditionalEdgeRouterReturnValue<
                ExtractBagNodes<Schema, string>,
                ExtractBagInput<Schema, unknown>,
            >
            | Promise<
                ConditionalEdgeRouterReturnValue<
                    ExtractBagNodes<Schema, string>,
                    ExtractBagInput<Schema, unknown>,
                >,
            >
        : (
            state: ExtractStateType<Schema>,
            config: LangGraphRunnableConfig<Context>,
        ) =>
            | ConditionalEdgeRouterReturnValue<Nodes, ExtractStateType<Schema>>
            | Promise<
                ConditionalEdgeRouterReturnValue<Nodes, ExtractStateType<Schema>>,
            >

    Type for conditional edge routing functions.

    Use this to type functions passed to addConditionalEdges for full type safety on state, runtime context, and return values.

    Supports two patterns:

    1. Single schema pattern - Single schema: ConditionalEdgeRouter<Schema, Context, Nodes>

    2. Type bag pattern - Separate schemas for state, context: ConditionalEdgeRouter<{ Schema; ContextSchema; Nodes }>

    Type Parameters

    • Schema

      The state schema type OR a type bag

    • Context extends Record<string, any> = Record<string, any>

      The runtime context type available to node logic

    • Nodes extends string = string

      Union of valid node names that can be routed to

    type MyContext = { userId: string };
    const router: ConditionalEdgeRouter<typeof AgentState, MyContext, "agent" | "tool"> =
    (state, config) => {
    const userId = config.context?.userId;
    if (state.done) return END;
    return state.needsTool ? "tool" : "agent";
    };

    graph.addConditionalEdges("router", router, ["agent", "tool"]);
    const router: ConditionalEdgeRouter<{
    Schema: typeof StateSchema;
    ContextSchema: typeof ContextSchema;
    Nodes: "agent" | "tool";
    }> = (state, config) => {
    if (state.done) return END;
    return "agent";
    };