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
– An entrypoint encapsulates workflow logic and manages 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
function 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
function with configuration and a function.
The function must accept a single positional argument, which serves as the workflow input. If you need to pass multiple pieces of data, use an object as the input type for the first argument.
Creating an entrypoint with a function produces a workflow instance which helps to manage the execution of the workflow (e.g., handles streaming, resumption, and checkpointing).
You will often want to pass a checkpointer to the entrypoint
function to enable persistence and use features like human-in-the-loop.
entrypoint
function will return an object that can be executed using the invoke
and stream
methods.
Command
primitive.
entrypoint
with null
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 getPreviousState
function.
By default, the getPreviousState
function returns 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.
task
function, which wraps a regular function.
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.