函数式 API 允许您以最小的代码更改将 LangGraph 的关键功能 — 持久化、内存、人在回路和流式传输 — 添加到您的应用程序中。它旨在将这些功能集成到可能使用标准语言原语进行分支和控制流的现有代码中,例如 if 语句、for 循环和函数调用。与许多需要将代码重组为显式管道或 DAG 的数据编排框架不同,函数式 API 允许您在不强制执行严格执行模型的情况下合并这些能力。函数式 API 使用两个关键构建块:
from langgraph.checkpoint.memory import InMemorySaverfrom langgraph.func import entrypoint, taskfrom langgraph.types import interrupt@taskdef write_essay(topic: str) -> str: """Write an essay about the given topic.""" time.sleep(1) # A placeholder for a long-running task. return f"An essay about topic: {topic}"@entrypoint(checkpointer=InMemorySaver())def workflow(topic: str) -> dict: """A simple workflow that writes an essay and asks for a review.""" essay = write_essay("cat").result() is_approved = interrupt({ # Any json-serializable payload provided to interrupt as argument. # It will be surfaced on the client side as an Interrupt when streaming data # from the workflow. "essay": essay, # The essay we want reviewed. # We can add any additional information that we need. # For example, introduce a key called "action" with some instructions. "action": "Please approve/reject the essay", }) return { "essay": essay, # The essay that was generated "is_approved": is_approved, # Response from HIL }
Detailed Explanation
This workflow will write an essay about the topic “cat” and then pause to get a review from a human. The workflow can be interrupted for an indefinite amount of time until a review is provided.When the workflow is resumed, it executes from the very start, but because the result of the writeEssay task was already saved, the task result will be loaded from the checkpoint instead of being recomputed.
import timeimport uuidfrom langgraph.func import entrypoint, taskfrom langgraph.types import interruptfrom langgraph.checkpoint.memory import InMemorySaver@taskdef write_essay(topic: str) -> str: """Write an essay about the given topic.""" time.sleep(1) # This is a placeholder for a long-running task. return f"An essay about topic: {topic}"@entrypoint(checkpointer=InMemorySaver())def workflow(topic: str) -> dict: """A simple workflow that writes an essay and asks for a review.""" essay = write_essay("cat").result() is_approved = interrupt( { # Any json-serializable payload provided to interrupt as argument. # It will be surfaced on the client side as an Interrupt when streaming data # from the workflow. "essay": essay, # The essay we want reviewed. # We can add any additional information that we need. # For example, introduce a key called "action" with some instructions. "action": "Please approve/reject the essay", } ) return { "essay": essay, # The essay that was generated "is_approved": is_approved, # Response from HIL }thread_id = str(uuid.uuid4())config = {"configurable": {"thread_id": thread_id}}for item in workflow.stream("cat", config): print(item)# > {'write_essay': 'An essay about topic: cat'}# > {# > '__interrupt__': (# > Interrupt(# > value={# > 'essay': 'An essay about topic: cat',# > 'action': 'Please approve/reject the essay'# > },# > id='b9b2b9d788f482663ced6dc755c9e981'# > ),# > )# > }
An essay has been written and is ready for review. Once the review is provided, we can resume the workflow:
from langgraph.types import Command# Get review from a user (e.g., via a UI)# In this case, we're using a bool, but this can be any json-serializable value.human_review = Truefor item in workflow.stream(Command(resume=human_review), config): print(item)
{'workflow': {'essay': 'An essay about topic: cat', 'is_approved': False}}
The workflow has been completed and the review has been added to the essay.
from langgraph.func import entrypoint@entrypoint(checkpointer=checkpointer)def my_workflow(some_input: dict) -> int: # some logic that may involve long-running tasks like API calls, # and may be interrupted for human-in-the-loop. ... return result
from langgraph.func import entrypoint@entrypoint(checkpointer=checkpointer)async def my_workflow(some_input: dict) -> int: # some logic that may involve long-running tasks like API calls, # and may be interrupted for human-in-the-loop ... return result
For accessing run time configuration. See RunnableConfig for information.
使用适当的名称和类型注释声明参数。
Requesting Injectable Parameters
from langchain_core.runnables import RunnableConfigfrom langgraph.func import entrypointfrom langgraph.store.base import BaseStorefrom langgraph.store.memory import InMemoryStorein_memory_store = InMemoryStore(...) # An instance of InMemoryStore for long-term memory@entrypoint( checkpointer=checkpointer, # Specify the checkpointer store=in_memory_store # Specify the store)def my_workflow( some_input: dict, # The input (e.g., passed via `invoke`) *, previous: Any = None, # For short-term memory store: BaseStore, # For long-term memory writer: StreamWriter, # For streaming custom data config: RunnableConfig # For accessing the configuration passed to the entrypoint) -> ...:
from langgraph.types import Commandconfig = { "configurable": { "thread_id": "some_thread_id" }}for chunk in my_workflow.stream(Command(resume=some_resume_value), config): print(chunk)
from langgraph.types import Commandconfig = { "configurable": { "thread_id": "some_thread_id" }}async for chunk in my_workflow.astream(Command(resume=some_resume_value), config): print(chunk)
Resuming after an errorTo resume after an error, run the entrypoint with a None and the same thread id (config).This assumes that the underlying error has been resolved and execution can proceed successfully.
When an entrypoint is defined with a checkpointer, it stores information between successive invocations on the same thread id in checkpoints.This allows accessing the state from the previous invocation using the previous parameter.By default, the previous parameter is the return value of the previous invocation.
@entrypoint(checkpointer=checkpointer)def my_workflow(number: int, *, previous: Any = None) -> int: previous = previous or 0 return number + previousconfig = { "configurable": { "thread_id": "some_thread_id" }}my_workflow.invoke(1, config) # 1 (previous was None)my_workflow.invoke(2, config) # 3 (previous was 1 from the previous invocation)
entrypoint.final is a special primitive that can be returned from an entrypoint and allows decoupling the value that is saved in the checkpoint from the return value of the entrypoint.The first value is the return value of the entrypoint, and the second value is the value that will be saved in the checkpoint. The type annotation is entrypoint.final[return_type, save_type].
@entrypoint(checkpointer=checkpointer)def my_workflow(number: int, *, previous: Any = None) -> entrypoint.final[int, int]: previous = previous or 0 # This will return the previous value to the caller, saving # 2 * number to the checkpoint, which will be used in the next invocation # for the `previous` parameter. return entrypoint.final(value=previous, save=2 * number)config = { "configurable": { "thread_id": "1" }}my_workflow.invoke(3, config) # 0 (previous was None)my_workflow.invoke(1, config) # 6 (previous was 3 * 2 from the previous invocation)
A task represents a discrete unit of work, such as an API call or data processing step. It has two key characteristics:
Asynchronous Execution: Tasks are designed to be executed asynchronously, allowing multiple operations to run concurrently without blocking.
Checkpointing: Task results are saved to a checkpoint, enabling resumption of the workflow from the last saved state. (See persistence for more details).
Tasks can only be called from within an entrypoint, another task, or a state graph node.Tasks cannot be called directly from the main application code.When you call a task, it returns immediately with a future object. A future is a placeholder for a result that will be available later.To obtain the result of a task, you can either wait for it synchronously (using result()) or await it asynchronously (using await).
Synchronous Invocation
Asynchronous Invocation
@entrypoint(checkpointer=checkpointer)def my_workflow(some_input: int) -> int: future = slow_computation(some_input) return future.result() # Wait for the result synchronously
Checkpointing: When you need to save the result of a long-running operation to a checkpoint, so you don’t need to recompute it when resuming the workflow.
Human-in-the-loop: If you’re building a workflow that requires human intervention, you MUST use tasks to encapsulate any randomness (e.g., API calls) to ensure that the workflow can be resumed correctly. See the determinism section for more details.
Parallel Execution: For I/O-bound tasks, tasks enable parallel execution, allowing multiple operations to run concurrently without blocking (e.g., calling multiple APIs).
Observability: Wrapping operations in tasks provides a way to track the progress of the workflow and monitor the execution of individual operations using LangSmith.
Retryable Work: When work needs to be retried to handle failures or inconsistencies, tasks provide a way to encapsulate and manage the retry logic.
There are two key aspects to serialization in LangGraph:
entrypoint inputs and outputs must be JSON-serializable.
task outputs must be JSON-serializable.
These requirements are necessary for enabling checkpointing and workflow resumption. Use python primitives like dictionaries, lists, strings, numbers, and booleans to ensure that your inputs and outputs are serializable.Serialization ensures that workflow state, such as task results and intermediate values, can be reliably saved and restored. This is critical for enabling human-in-the-loop interactions, fault tolerance, and parallel execution.Providing non-serializable inputs or outputs will result in a runtime error when a workflow is configured with a checkpointer.
To utilize features like human-in-the-loop, any randomness should be encapsulated inside of tasks. This guarantees that when execution is halted (e.g., for human in the loop) and then resumed, it will follow the same sequence of steps, even if task results are non-deterministic.LangGraph achieves this behavior by persisting task and subgraph results as they execute. A well-designed workflow ensures that resuming execution follows the same sequence of steps, allowing previously computed results to be retrieved correctly without having to re-execute them. This is particularly useful for long-running tasks or tasks with non-deterministic results, as it avoids repeating previously done work and allows resuming from essentially the same.While different runs of a workflow can produce different results, resuming a specific run should always follow the same sequence of recorded steps. This allows LangGraph to efficiently look up task and subgraph results that were executed prior to the graph being interrupted and avoid recomputing them.
Idempotency ensures that running the same operation multiple times produces the same result. This helps prevent duplicate API calls and redundant processing if a step is rerun due to a failure. Always place API calls inside tasks functions for checkpointing, and design them to be idempotent in case of re-execution. Re-execution can occur if a task starts, but does not complete successfully. Then, if the workflow is resumed, the task will run again. Use idempotency keys or verify existing results to avoid duplication.
Encapsulate side effects (e.g., writing to a file, sending an email) in tasks to ensure they are not executed multiple times when resuming a workflow.
Incorrect
Correct
In this example, a side effect (writing to a file) is directly included in the workflow, so it will be executed a second time when resuming the workflow.
@entrypoint(checkpointer=checkpointer)def my_workflow(inputs: dict) -> int: # This code will be executed a second time when resuming the workflow. # Which is likely not what you want. with open("output.txt", "w") as f: f.write("Side effect executed") value = interrupt("question") return value
In this example, the side effect is encapsulated in a task, ensuring consistent execution upon resumption.
from langgraph.func import task@taskdef write_to_file(): with open("output.txt", "w") as f: f.write("Side effect executed")@entrypoint(checkpointer=checkpointer)def my_workflow(inputs: dict) -> int: # The side effect is now encapsulated in a task. write_to_file().result() value = interrupt("question") return value
Operations that might give different results each time (like getting current time or random numbers) should be encapsulated in tasks to ensure that on resume, the same result is returned.
In a task: Get random number (5) → interrupt → resume → (returns 5 again) → …
Not in a task: Get random number (5) → interrupt → resume → get new random number (7) → …
This is especially important when using human-in-the-loop workflows with multiple interrupts calls. LangGraph keeps a list of resume values for each task/entrypoint. When an interrupt is encountered, it’s matched with the corresponding resume value. This matching is strictly index-based, so the order of the resume values should match the order of the interrupts.If order of execution is not maintained when resuming, one interrupt call may be matched with the wrong resume value, leading to incorrect results.Please read the section on determinism for more details.
Incorrect
Correct
In this example, the workflow uses the current time to determine which task to execute. This is non-deterministic because the result of the workflow depends on the time at which it is executed.
from langgraph.func import entrypoint@entrypoint(checkpointer=checkpointer)def my_workflow(inputs: dict) -> int: t0 = inputs["t0"] t1 = time.time() delta_t = t1 - t0 if delta_t > 1: result = slow_task(1).result() value = interrupt("question") else: result = slow_task(2).result() value = interrupt("question") return { "result": result, "value": value }
In this example, the workflow uses the input t0 to determine which task to execute. This is deterministic because the result of the workflow depends only on the input.
import timefrom langgraph.func import task@taskdef get_time() -> float: return time.time()@entrypoint(checkpointer=checkpointer)def my_workflow(inputs: dict) -> int: t0 = inputs["t0"] t1 = get_time().result() delta_t = t1 - t0 if delta_t > 1: result = slow_task(1).result() value = interrupt("question") else: result = slow_task(2).result() value = interrupt("question") return { "result": result, "value": value }