Define a LangGraph task using the task function.
Tasks can only be called from within an entrypoint or from within a StateGraph. A task can be called like a regular function with the following differences:
task<
ArgsT extends unknown[],
OutputT
>(
optionsOrName: string | TaskOptions,
func: TaskFunc<ArgsT, OutputT>): (args: ArgsT
) => Promise<OutputT>| Name | Type | Description |
|---|---|---|
optionsOrName* | string | TaskOptions | Either an TaskOptions object, or a string for the name of the task |
func* | TaskFunc<ArgsT, OutputT> | The function that executes this task |
const addOne = task("add", async (a: number) => a + 1);
const workflow = entrypoint("example", async (numbers: number[]) => {
const promises = numbers.map(n => addOne(n));
const results = await Promise.all(promises);
return results;
});
// Call the entrypoint
await workflow.invoke([1, 2, 3]); // Returns [2, 3, 4]const addOne = task({
name: "add",
retry: { maxAttempts: 3 }
},
async (a: number) => a + 1
);
const workflow = entrypoint("example", async (numbers: number[]) => {
const promises = numbers.map(n => addOne(n));
const results = await Promise.all(promises);
return results;
});