LangGraph.js API Reference
    Preparing search index...

    Type Alias AIMessage<ToolCall>

    AIMessage: BaseMessage & {
        example?: boolean;
        invalid_tool_calls?: InvalidToolCall[];
        tool_calls?: ToolCall[];
        type: "ai";
        usage_metadata?: UsageMetadata;
    }

    AI message type that can be parameterized with custom tool call types.

    Type Parameters

    • ToolCall = DefaultToolCall

      The type of tool calls, defaults to DefaultToolCall. Provide a discriminated union for type-safe tool call handling.

    // Define typed tool calls as a discriminated union
    type MyToolCalls =
    | { name: "get_weather"; args: { location: string }; id?: string }
    | { name: "search"; args: { query: string; limit?: number }; id?: string };

    // Use with AIMessage
    const message: AIMessage<MyToolCalls> = ...;

    // Now tool.name === "get_weather" narrows tool.args type
    if (message.tool_calls) {
    for (const tool of message.tool_calls) {
    if (tool.name === "get_weather") {
    // tool.args is now { location: string }
    console.log(tool.args.location);
    }
    }
    }