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:
Single schema pattern - Single schema:
ConditionalEdgeRouter<Schema, Context, Nodes>
Type bag pattern - Separate schemas for state, context:
ConditionalEdgeRouter<{ Schema; ContextSchema; 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 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";
};