LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangGraph
  • Web
  • Channels
  • Pregel
  • Prebuilt
  • Remote
LangGraph SDK
  • Client
  • Auth
  • React
  • Logging
  • React Ui
  • Server
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
  • Store
LangGraph Checkpoint Redis
  • Shallow
  • Store
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
  • Cli
LangGraph API
LangGraph CLI
LangGraph CUA
  • Utils
LangGraph Supervisor
LangGraph Swarm
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangGraph
WebChannelsPregelPrebuiltRemote
LangGraph SDK
ClientAuthReactLoggingReact UiServer
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
Store
LangGraph Checkpoint Redis
ShallowStore
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
Cli
LangGraph API
LangGraph CLI
LangGraph CUA
Utils
LangGraph Supervisor
LangGraph Swarm
Language
Theme
JavaScript@langchain/langgraphwebtask
Functionā—Since v0.3

task

Copy
task<
  ArgsT extends unknown[],
  OutputT
>(
  optionsOrName: string | TaskOptions,
  func: TaskFunc

Used in Docs

  • Choosing between Graph and Functional APIs
  • Functional API overview
  • Quickstart
  • Use the functional API
  • Workflows and agents
View source on GitHub
<
ArgsT
,
OutputT
>
)
:
(
args
:
ArgsT
)
=
>
Promise
<
OutputT
>

Parameters

NameTypeDescription
optionsOrName*string | TaskOptions
func*TaskFunc<ArgsT, OutputT>

Example 1

Example 2

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:

  • When a checkpointer is enabled, the function inputs and outputs must be serializable.
  • The wrapped function can only be called from within an entrypoint or StateGraph.
  • Calling the function produces a promise. This makes it easy to parallelize tasks.

Either an TaskOptions object, or a string for the name of the task

The function that executes this task

Copy
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]
Copy
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;
});