Standalone tasks
A standalone task is the simplest way to define a unit of work. Apply the@hatchet.task() decorator to a function and Hatchet wraps it in a single-task workflow.
Task decorator parameters
The name used to register this task. Defaults to the decorated function’s
__name__.An optional human-readable description.
A Pydantic model class used to validate and parse the input passed to the task. When not set, defaults to
EmptyModel (accepts any extra fields).A list of event keys that automatically trigger this task when pushed via
hatchet.event.push().A list of cron expressions that schedule recurring runs of this task.
Pin task execution to a specific worker. Values:
StickyStrategy.SOFT, StickyStrategy.HARD.Scheduling priority for this task. Higher values are scheduled before lower ones.
Limit how many runs can execute concurrently. Pass an
int for a constant cap with GROUP_ROUND_ROBIN strategy, or a ConcurrencyExpression for CEL-based grouping.Maximum time to wait for a worker slot before cancelling the run.
Maximum time allowed for the task to complete once it starts.
Number of times to retry the task on failure.
Rate limit configurations applied to this task.
Multiplier controlling exponential backoff between retries.
Upper bound (in seconds) for exponential backoff retry delays.
Metadata attached to every run of this task by default.
Input validation with Pydantic
Pass a Pydantic model asinput_validator to get typed, validated input:
Durable tasks
Durable tasks use@hatchet.durable_task() and receive a DurableContext instead of a plain Context. They must be async and support additional durability primitives such as sleeping, waiting for external events, and memoizing results.
@hatchet.durable_task() accepts the same parameters as @hatchet.task() plus:
Controls when an idle durable task is evicted from a worker slot while waiting for a condition.
Workflows
A workflow groups multiple tasks and defines dependencies between them (a DAG). Create a workflow withhatchet.workflow(), then add tasks with @workflow.task().
hatchet.workflow() parameters
The name used to register this workflow.
Optional human-readable description.
Pydantic model for validating workflow input. Defaults to
EmptyModel.Event keys that trigger this workflow automatically.
Cron expressions for recurring execution.
Affinity strategy for pinning runs to a worker.
Scheduling priority for workflow runs.
Concurrency limits applied at the workflow level.
Default
schedule_timeout, execution_timeout, retries, backoff_factor, backoff_max_seconds, and priority applied to all tasks in this workflow unless overridden per task.An optional version string for the workflow.
Workflow task decorator parameters
@workflow.task() accepts the following parameters from @hatchet.task(): name, schedule_timeout, execution_timeout, retries, rate_limits, backoff_factor, backoff_max_seconds, concurrency, desired_worker_labels. It does not accept the workflow-level parameters (description, input_validator, on_events, on_crons, version, sticky, default_priority, default_filters, default_additional_metadata) since those are set on the enclosing hatchet.workflow(). It also accepts:
List of task objects that must complete before this task runs. Parent tasks must be defined before their dependents.
Additional conditions that must be satisfied before this task runs.
Conditions that, if met, cause this task to be skipped.
Conditions that, if met, cause this task to be cancelled.
On-failure and on-success tasks
Use@workflow.on_failure_task() to run cleanup logic when any task fails, and @workflow.on_success_task() to run logic after all tasks succeed:
on_failure_task and one on_success_task may be registered per workflow.
Task context
Every task receives actx: Context (or ctx: DurableContext) as its second argument.
Reading parent task output
ctx.task_output(task) returns the typed output of a parent task. It raises ValueError if the parent was skipped or its output is not found.
Logging
Refreshing the execution timeout
Cancelling a run
Checking retry state
DurableContext extras
Triggering tasks and workflows
Standalone tasks andWorkflow objects share the same run methods.
Synchronous trigger and wait
Async trigger and wait
Fire-and-forget
Bulk trigger
Scheduling
One-time schedule
Cron schedule
@daily, @hourly, @weekly, @monthly, and @yearly are also accepted.