<@ULVA73B9P> what’s the difference between prefect.runtime.get_tags() and prefect.context.TagsContex...
a
@Marvin what’s the difference between prefect.runtime.get_tags() and prefect.context.TagsContext()
m
Here's a breakdown of the differences between
prefect.runtime.get_tags()
and
prefect.context.TagsContext()
based on the available documentation: 1. Prefect Runtime (
prefect.runtime.get_tags()
)
: - This is a part of the Prefect runtime tools that provides access to metadata and information about the current flow or task run. - Although I couldn't find a direct reference to
prefect.runtime.get_tags()
, the general use of runtime context like
prefect.runtime
is to retrieve real-time information related to flows and tasks that are executing. 2. Prefect Context (
prefect.context.TagsContext()
)
: -
prefect.context
is used to access contextual information during flow and task runs. It provides the execution context for tasks and flows. - The runtime API context allows access to attributes related to the current run, and using functions like
prefect.context.get_run_context()
provides direct access to the run context. -
TagsContext()
isn't directly mentioned either, but it could be a pattern to create or manage tags within the context. Prefect often uses contexts to encapsulate settings and states that are active during a run. The functionalities of these methods might have evolved between different Prefect versions (2.x and 3.x), but the documentation I accessed didn't give specifics on these exact methods. It's possible these functions or classes are used internally or have recently been introduced. For additional details, you might want to check the Prefect runtime context documentation for Prefect 2.x and Prefect 3.x.
n
tldr the runtime module is explicitly for user consumption, the
TagsContext
is an implementation detail that you wont find in the docs
🙌 1
a
is it possible for the two to return different values? i.e. will this ever fail the assertion:
Copy code
from prefect.runtime import flow_run
from prefect.context import TagsContext

assert flow_run.get_tags() == TagsContext.get().current_tags
n
yes - I would recommend against using
TagsContext
directly, because when you do, you're not necessarily talking about the tags context that the flow run is using
what are you trying to do?
a
we’ve been using tags to fork logic in different flows/tasks and noticed the TagsContext.get().current_tags doesn’t always carry things downstream; though it works when I run the flow locally
the workaround we were using was to wrap the task with tags context manager, but I wanted to know why it doesn’t get carried automatically
n
hmm im still not sure why you need them from both places, sorry could be missing something
Copy code
In [1]: from prefect.context import get_run_context

In [2]: from prefect import task

In [3]: @task(tags={"a", "b", "c"})
   ...: def t():
   ...:     print(get_run_context().task_run.tags)
   ...:

In [4]: t()
['a', 'b', 'c']
this is how I would access the tags for a current flow/task
a
ah I see; we definitely don’t need both, just wanted to know which is the correct one
n
👍 yeah
runtime
and
get_run_context
are both "public" interfaces and more or less providing access to the same things
runtime
is just convenient for stuff like
task_run_name
or
result_storage_key
so you can template extra stuff into those things