if
statements, for
loops, and function calls. Unlike many data orchestration frameworks that require restructuring code into an explicit pipeline or DAG, the Functional API allows you to incorporate these capabilities without enforcing a rigid execution model.
The Functional API uses two key building blocks:
@entrypoint
– Marks a function as the starting point of a workflow, encapsulating logic and managing execution flow, including handling long-running tasks and interrupts.@task
– Represents a discrete unit of work, such as an API call or data processing step, that can be executed asynchronously within an entrypoint. Tasks return a future-like object that can be awaited or resolved synchronously.@entrypoint
and @tasks
do not require explicit state management as their state is scoped to the function and is not shared across functions.Detailed Explanation
writeEssay
task was already saved, the task result will be loaded from the checkpoint instead of being recomputed.@entrypoint
decorator can be used to create a workflow from a function. It encapsulates workflow logic and manages execution flow, including handling long-running tasks and interrupts.
@entrypoint
decorator.
The function must accept a single positional argument, which serves as the workflow input. If you need to pass multiple pieces of data, use a dictionary as the input type for the first argument.
Decorating a function with an entrypoint
produces a Pregel
instance which helps to manage the execution of the workflow (e.g., handles streaming, resumption, and checkpointing).
You will usually want to pass a checkpointer to the @entrypoint
decorator to enable persistence and use features like human-in-the-loop.
entrypoint
, you can request access to additional parameters that will be injected automatically at run time. These parameters include:
Parameter | Description |
---|---|
previous | Access the state associated with the previous checkpoint for the given thread. See short-term-memory. |
store | An instance of [BaseStore][langgraph.store.base.BaseStore]. Useful for long-term memory. |
writer | Use to access the StreamWriter when working with Async Python < 3.11. See streaming with functional API for details. |
config | For accessing run time configuration. See RunnableConfig for information. |
Requesting Injectable Parameters
@entrypoint
yields a Pregel
object that can be executed using the invoke
, ainvoke
, stream
, and astream
methods.
entrypoint
with a None
and the same thread id (config).
This assumes that the underlying error has been resolved and execution can proceed successfully.
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.final
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]
.
@task
decorator, which wraps a regular Python function.
result()
) or await it asynchronously (using await
).
entrypoint
inputs and outputs must be JSON-serializable.task
outputs must be JSON-serializable.interrupt
call may be matched with the wrong resume
value, leading to incorrect results.
Please read the section on determinism for more details.