Skip to main content
函数式 API 允许你以最少的代码更改将 LangGraph 的关键功能(持久化记忆人机协作流式输出)添加到现有应用中。
For conceptual information on the functional API, see Functional API.

创建简单工作流

When defining an entrypoint, input is restricted to the first argument of the function. To pass multiple inputs, you can use a dictionary.
This example demonstrates how to use the @task and @entrypoint decorators syntactically. Given that a checkpointer is provided, the workflow results will be persisted in the checkpointer.

并行执行

Tasks can be executed in parallel by invoking them concurrently and waiting for the results. This is useful for improving performance in IO bound tasks (e.g., calling APIs for LLMs).
This example demonstrates how to run multiple LLM calls in parallel using @task. Each call generates a paragraph on a different topic, and results are joined into a single text output.
This example uses LangGraph’s concurrency model to improve execution time, especially when tasks involve I/O like LLM completions.

调用图

The Functional API and the Graph API can be used together in the same application as they share the same underlying runtime.

调用其他入口点

You can call other entrypoints from within an entrypoint or a task.

流式输出

The Functional API uses the same streaming mechanism as the Graph API. Please read the streaming guide section for more details. Example of using the streaming API to stream both updates and custom data.
  1. Emit custom data before computation begins.
  2. Emit another custom message after computing the result.
  3. Use .stream() to process streamed output.
  4. Specify which streaming modes to use.

重试策略

缓存任务

  1. ttl is specified in seconds. The cache will be invalidated after this time.

错误后恢复

When we resume execution, we won’t need to re-run the slowTask as its result is already saved in the checkpoint.

人机协作

The functional API supports human-in-the-loop workflows using the interrupt function and the Command primitive.

Basic human-in-the-loop workflow

We will create three tasks:
  1. Append "bar".
  2. Pause for human input. When resuming, append human input.
  3. Append "qux".
We can now compose these tasks in an entrypoint:
interrupt() is called inside a task, enabling a human to review and edit the output of the previous task. The results of prior tasks— in this case step_1— are persisted, so that they are not run again following the interrupt. Let’s send in a query string:
Note that we’ve paused with an interrupt after step_1. The interrupt provides instructions to resume the run. To resume, we issue a Command containing the data expected by the human_feedback task.
After resuming, the run proceeds through the remaining step and terminates as expected.

Review tool calls

To review tool calls before execution, we add a review_tool_call function that calls interrupt. When this function is called, execution will be paused until we issue a command to resume it. Given a tool call, our function will interrupt for human review. At that point we can either:
  • Accept the tool call
  • Revise the tool call and continue
  • Generate a custom tool message (e.g., instructing the model to re-format its tool call)
We can now update our entrypoint to review the generated tool calls. If a tool call is accepted or revised, we execute in the same way as before. Otherwise, we just append the ToolMessage supplied by the human. The results of prior tasks—in this case the initial model call—are persisted, so that they are not run again following the interrupt.

短期记忆

Short-term memory allows storing information across different invocations of the same thread id. See short-term memory for more details.

Manage checkpoints

You can view and delete the information stored by the checkpointer.

View thread state

View the history of the thread

Decouple return value from saved value

Use entrypoint.final to decouple what is returned to the caller from what is persisted in the checkpoint. This is useful when:
  • You want to return a computed result (e.g., a summary or status), but save a different internal value for use on the next invocation.
  • You need to control what gets passed to the previous parameter on the next run.

Chatbot example

An example of a simple chatbot using the functional API and the InMemorySaver checkpointer. The bot is able to remember the previous conversation and continue from where it left off.

长期记忆

long-term memory allows storing information across different thread ids. This could be useful for learning information about a given user in one conversation and using it in another.

工作流

  • Workflows and agent guide for more examples of how to build workflows using the Functional API.

与其他库集成