task<
ArgsT extends unknown[],
OutputT
>(
optionsOrName: string | TaskOptions,
func: TaskFunc| Name | Type | Description |
|---|---|---|
optionsOrName* | string | TaskOptions | |
func* | TaskFunc<ArgsT, OutputT> |
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:
Either an TaskOptions object, or a string for the name of the task
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;
});