<@ULVA73B9P> If I add tags to my deployment (e.g. ...
# ask-marvin
t
@Marvin If I add tags to my deployment (e.g. using to_deployment), will flow runs inherit them?
m
thought for 173.3 seconds
Short answer: Yes. In Prefect 3.x, tags you set on a deployment (including via
flow.to_deployment(...)
) are inherited by all flow runs created from that deployment. If you also provide tags when creating a specific run, Prefect merges them with the deployment’s tags (set union), not replaces them. A few clarifications: - Deployment tags → inherited by flow runs created from that deployment. - Flow-run-supplied tags → merged with deployment tags. - Task runs do not inherit flow run tags. To tag task runs, use
@task(tags=[...])
or the
prefect.tags(...)
context manager. Example:
Copy code
deploy = my_flow.to_deployment("hello", tags=["prod"])
# Every flow run from this deployment will include "prod" plus any tags you supply at run time
t
@Marvin Can you give me guidelines as to when I should set tags at deployment time or flow run creation time?
m
thought for 232.0 seconds
Great question. Here’s a practical rule of thumb for Prefect 3.x: - Set tags on the deployment when they’re stable identifiers that should apply to every run of that deployment. - Add tags at flow-run creation (or via a tags context) when they’re dynamic or specific to a particular invocation. When to set tags at deployment time (static, consistent across all runs): - Environment/tier: prod, staging, dev - Service/domain or data source: etl, payments, snowflake - Team/ownership and governance: team-ml, cost-center-123, compliance - Always-on automation targeting: notifications, SLAs for a deployment class - Search/organization in the UI you want to be consistent across runs When to add tags at flow run creation (dynamic, changes per invocation): - Backfills, canaries, hotfixes, incident investigations: backfill, canary, investigate - Feature flags/branches or ad-hoc experiments: feature-x, test-run - Trigger/source labeling: manual, automation:xyz, webhook:abc - Parameter-driven classification that varies per run: customer:acme, region:eu-west-1 Special considerations: - Flow runs inherit and merge deployment tags with any runtime-supplied tags (set union), so you can safely add runtime tags without losing the deployment defaults. Code reference: server/api/deployments.py (flow-run-from-deployment merges tags). - Task concurrency limits are tag-based but apply to task run tags only. Use
@task(tags=[...])
or
with tags(...)
around task calls. Flow/deployment tags do not enforce task concurrency unless you explicitly add them to tasks via the tags context. Code reference: tasks union decorator tags with context tags in task_engine.py. - Workers do not route by tag in 3.x; use work pools/queues for routing. Tags are for organization, filtering, automations, and task concurrency. See Workers. Quick examples 1) Stable metadata at deployment time
Copy code
from prefect import flow

@flow
def etl():
    ...

etl.deploy(
    name="daily-etl",
    work_pool_name="prod-pool",
    tags=["prod", "etl", "team-ml"]
)
2) Dynamic tags at run time (via run_deployment or context)
Copy code
from prefect import flow, tags
from prefect.deployments import run_deployment

# Add tags for a specific invocation
with tags("backfill", "range:2025-03"):
    etl()  # direct run: flow run gets these tags + any deployment tags

# Or when triggering a deployment
run_deployment(
    name="etl/daily-etl",
    tags=["manual", "investigate"]
)
3) Task concurrency via task tags or context
Copy code
from prefect import flow, task, tags

@task(tags=["database"])  # will be limited by "database" tag if a limit exists
def query_db(...):
    ...

@flow
def etl():
    # propagate dynamic runtime tags to tasks as well
    with tags("database"):  # merges with task's own tags
        query_db()
Helpful docs - Deployments and tags: Deployments - Tag-based task concurrency limits: Tag-based Concurrency Limits - Automations and event triggers by tag: Automations and Event Triggers - Implementation references: flow-run tag merge server/api/deployments.py, task tags union with context task_engine.py If you share a concrete scenario (e.g., you want to alert only on prod failures but flag backfills differently), I can suggest a tagging pattern tailored to it.