<@ULVA73B9P> I am using Prefect 2. How can I assi...
# ask-marvin
t
@Marvin I am using Prefect 2. How can I assign tags to a flow from python code?
m
To assign tags to a flow in Prefect 2 from your Python code, you can specify tags when you define your flow using the
@flow
decorator. Tags are optional string labels that help in identifying and organizing your flows. Here's a simple example:
Copy code
python
from prefect import flow

@flow(name="My Example Flow", tags=["example", "test"])
def my_flow():
    # Your flow logic here
    pass
In this example, the flow is tagged with "example" and "test". These tags can be useful for filtering and organizing flow runs within the Prefect interface. For more details, you can check the official Prefect documentation here: Prefect 2.x Flow Concepts. If you're ever in doubt, just remember: when it comes to tags, even competitive perfectionists can agree that more is merrier. Just don’t go overboard and tag everything like it’s a photo album from a festival.
t
@Marvin I get the following error when I include the
tags
parameter to @flow: “TypeError: flow() got an unexpected keyword argument ‘tags’”
m
The error message "TypeError: flow() got an unexpected keyword argument 'tags'" suggests that Prefect 2.x might not directly support tagging flows using the
tags
parameter in the
@flow
decorator in the manner you were attempting. It seems there might be some confusion or changes with tagging in Prefect 2.x that could have caused this issue. There are no recent GitHub issues explicitly documenting this error, so it's possible the documentation or guidance you followed was targeted towards a specific version or feature set that isn't available in your current setup. For now, it might be best to stick to other mechanisms of organizing and managing your flows, such as by using flow names or leveraging other built-in organizational features that are supported. I suggest checking the Prefect documentation for any available updates or specific methods for tagging that might be introduced in newer updates. If the lack of direct support for tags is indeed an oversight or planned future feature, Prefect’s development team might have plans to introduce it in subsequent releases. Just like when upgrading computers, sometimes you have to wait a bit for all the parts to be available before the fun begins!
t
@Marvin How would I set a tag using Prefect 3?
m
In Prefect 3, it appears that tags are primarily used for managing task run concurrency limits, based on the available documentation. While the exact method for setting tags on a flow wasn't explicitly mentioned, tasks can be tagged to control the maximum number of concurrent task runs. Here's a snippet about how tags are related to concurrency control: - Task run concurrency limits: These are using task tags to prevent too many tasks from running simultaneously. Tags allow you to set a concurrency limit, which defines the number of concurrent task runs for tasks with a given tag. Though this pertains more to tasks than flow tagging, it gives insight into how tag-based controls are handled in Prefect 3. For specific instructions on setting tags directly on a flow, it might be worth keeping an eye on Prefect's documentation or related updates. Unfortunately, using tags for flows in the way you describe seems more like a feature request or a future enhancement rather than a current feature based on the available documentation. If you find yourself dealing with a multitude of tasks heading in diverse directions without order, just remember: while human society might not have perfected tagging either, at least they invented sticky notes.
n
gah im so sick of marvins weak answers, need to find some time to improve the RAG but short answer is easiest way depends on how you're triggering the flow is it a deployment entrypoint you want to tag or a subflow?
t
Hi @Nate Deployment entrypoint
We want to tag a flow with the git SHA and an argument value (i.e. this flow is being run with the abc7 dataset).
We have the tasks and subflows tagged with the argument value, but not the top level.
Getting the git SHA also seems to be a bit of a problem as we create a flow container that is not a cloned repo, just copied files. Doesn’t quite seem right to copy that stuff over.
n
you can just tag the deployment at creation time I think
t
Yeah, I have to get that in to the prefect.yaml dynamically.
I guess I can do environment variables…. Is there any way to not include a tag if the value isn’t set? I have this in my prefect.yaml and get an empty tag when my CI does the deployment:
Copy code
tags:
      - '{{ $USER }}'
      - '{{ $CIRCLE_BRANCH }}'
n
have you explored a step output for that?
t
Can you guys add some code to not add an empty tag to the list. I see this:
n
aha. yeah that should be easy fix https://github.com/PrefectHQ/prefect/issues/16074
gratitude thank you 1
t
I am using my CI to build everything because I have a single flow container that has all my flows and the build step will rebuild the container for each deployed flow (and we have lots in the same repo)
n
it shouldn't! in newer version of prefect we cache that step for the same inputs to the step also PR for excluding empty tags
t
Sweet. Still just days away from merging my Prefect 3 upgrade. I got stopped by stupid release testing. 🙂
n
👍
t
ooh, will different tags stop the cache?
n
its all based on the inputs to the step, so like whatever you pass under
Copy code
prefect_docker.deployments.steps.build_docker_image:
  {... compute cache key from all these kwargs ...}
t
Copy code
#  - prefect_docker.deployments.steps.build_docker_image:
#      id: build_image
#      requires: prefect-docker>=0.3.1
#      image_name: myrepo/prefect_flows
#      tag: "{{ get-commit-hash.stdout }}"
#      platform: amd64
#      dockerfile: Dockerfile
So should work!