<@ULVA73B9P> Can I use dynamic tags in the task fu...
# ask-marvin
p
@Marvin Can I use dynamic tags in the task function decorator? Example:
@task(name="load", task_run_name="load-{target_date}", tags=["{target}-load"])
1
m
thought for 220.3 seconds
Short answer: not in the decorator. In Prefect 3.x,
task_run_name
supports dynamic formatting with parameters, but
tags
on the
@task
decorator are static. A literal
tags=["{target}-load"]
will stay exactly that string. Supported ways to add dynamic tags: - Use the
prefect.tags(...)
context manager around the call site - Use
task.with_options(tags=[...])
at call time Examples:
Copy code
from prefect import task, flow, tags

@task(name="load", task_run_name="load-{target_date}", tags={"static"})
def load(target: str, target_date: str):
    ...

@flow
def etl(target: str, target_date: str):
    # 1) Context manager — tags are added to all task runs inside this block
    with tags(f"{target}-load"):
        load(target=target, target_date=target_date)

    # 2) with_options at call-time — creates a modified copy of the task
    load.with_options(tags=[f"{target}-load"])(target=target, target_date=target_date)
Notes: - Tags from the decorator and from
prefect.tags(...)
are combined. -
task_run_name="load-{target_date}"
will format using the task’s parameters. Refs: - Task decorator API (incl.
tags
and
task_run_name
): Task API - Tags context manager source/docs: prefect.context.tags - Task class
with_options
(supports
tags
at call time): prefect.tasks.Task - How-to: tag-based concurrency limits: Tag-based concurrency