Batch Tasks
Batch tasks are in beta and may change in future releases.
For tasks that do not need to be executed immediately, you can use batch tasks to automatically accumulate tasks and execute them in a single batch. The input to a batch task is a map that associates the ID of the originating task with it's input. The output must be the same shape, using the same keys, to send outputs back to the original call site (unless output broadcasting is used, see Broadcasting outputs). For the caller of a batch task, the batched execution is hidden. It is called, and receives output, the same way as a normal task. Though each batch will be executed in one handler function, the call site only receives output corresponding to its input, not the output for the entire batch.
Batch flushing behavior
Batches will accumulate tasks continuously, preventing them from executing until one of the following conditions is true:
- The max size for the batch is reached.
- The maximum interval has elapsed since the first task was added to the batch. If you don't set a maximum interval, it defaults to 1 second.
- The size of the payloads for the buffered tasks has exceeded the 4Mb gRPC message limit.
In the following example, the batch task will be executed once 3 tasks have accumulated, or every 200ms, whichever comes first.
@hatchet.batch_task( batch_max_size=3, batch_max_interval=timedelta(milliseconds=200), input_validator=SimpleInput,)async def batch_simple( tasks: dict[BatchMemberId, SimpleInput], context: Context) -> dict[BatchMemberId, SimpleOutput]: return { id: SimpleOutput(transformed_message=inp.message.upper()) for id, inp in tasks.items() }Batch keys
To allow for accumulation of multiple batches simultaneously using the same workflow, batch keys can be used.
For example, if you had multiple tenants, and wanted to ensure that each batch execution did not mix inputs from multiple
tenants, batch keys can be used to partition the inputs. The batch key is a CEL expression evaluated against the run's input and additional metadata. In the following example, the batches will flush independently based on the group value.
@hatchet.batch_task( batch_max_size=2, batch_max_interval=timedelta(milliseconds=200), batch_group_key="input.group", input_validator=KeyedInput,)async def batch_keyed( tasks: dict[BatchMemberId, KeyedInput], context: Context) -> dict[BatchMemberId, KeyedOutput]: unique_keys = len({inp.group for _, inp in tasks.items()}) return { id: KeyedOutput( batch_key=inp.group, batch_size=len(tasks), unique_keys=unique_keys, uppercase=inp.message.upper(), ) for id, inp in tasks.items() }Concurrent batches
groupMaxRuns limits how many batches with the same batch key can run at once. If you don't set it, the limit is 100. While a batch key is at its limit, batches that are ready to flush stay buffered until one of that key's running batches finishes. Without a batch key, every run of the task shares one key, so the limit applies to the whole task.
Broadcasting outputs
In the default case, batch outputs are mapped 1-1 back to the callers. So if you called a batch task with input a and b at separate
call sites, you would receive back outputs a' and b' respectively, despite the fact they would both be executed at the same time, in the same handler.
However, if you wish to return the same input back to all callsites, you can use output broadcasting.
In the following example, every task will receive the same output regardless of call site. Note, however, that this will only return
the same output to every task buffered into the same batch. See the batch flushing rules above for more details.
@hatchet.batch_task( batch_max_size=10, batch_max_interval=timedelta(seconds=2), input_validator=SimpleInput, broadcast_output=True,)async def batch_broadcast( tasks: dict[BatchMemberId, SimpleInput], context: Context) -> BroadcastOutput: return BroadcastOutput(sum=sum(len(i.message) for _, i in tasks.items()))Failures, retries, and ordering
- If the handler throws, every member of the batch fails with that error. The handler can't fail individual members.
- Handler failures aren't retried. Setting
retrieson a batch task has no effect, so a member fails on its first attempt. - A run is placed in at most one batch per attempt. If the worker running a batch stops sending heartbeats for 30 seconds, the batch's members are reassigned and can run in a new batch.
- The order of members in the handler's input isn't guaranteed.
Last updated on November 26, 2026