<@ULVA73B9P> In Prefect 3, when writing a prefect ...
# ask-marvin
n
@Marvin In Prefect 3, when writing a prefect plugin in python, is it possible to obtain the root flow run id?
m
thought for 81.1 seconds
Yes — if your plugin code is running in flow-run context. The easiest way in Prefect 3 is:
Copy code
python
from prefect.runtime import flow_run

root_id = flow_run.root_flow_run_id
A few important details: *
flow_run.id
= current flow run id *
flow_run.parent_flow_run_id
= immediate parent flow run id, if this is a subflow *
flow_run.root_flow_run_id
= top-level/root flow run id So if you're inside a subflow,
root_flow_run_id
gives you the original parent flow run at the top of the chain. Example:
Copy code
python
from prefect import flow
from prefect.runtime import flow_run

@flow
def child():
    print("current:", flow_run.id)
    print("parent:", flow_run.parent_flow_run_id)
    print("root:", flow_run.root_flow_run_id)

@flow
def parent():
    child()
If by “plugin” you mean a Prefect startup/environment plugin (
setup_environment
etc.), then no — those run outside any flow-run context, so there is no flow run id to inspect there. If by “plugin” you mean code invoked during execution, like a hook, then yes, you can access it there. Relevant APIs: * Runtime flow run attributes:
prefect.runtime.flow_run
* Run context:
prefect.context.get_run_context()
Docs: * Runtime context * State change hooks If you want, I can also show the best pattern for getting the root flow run id specifically inside a custom hook/plugin callback.
n
@Marvin I mean a setup_environment plugin
m
thought for 7.1 seconds
Got it — for a
setup_environment
plugin, no, you cannot get the root flow run id there. Why: *
setup_environment
runs during Prefect environment/plugin setup * it does not run inside a flow run context * so runtime values like
flow_run.id
or
flow_run.root_flow_run_id
are not available In other words, this will not work reliably in `setup_environment`:
Copy code
python
from prefect.runtime import flow_run

root_id = flow_run.root_flow_run_id
because there is no active flow run yet. What
setup_environment
is meant for is things like: * setting env vars * preparing process-level configuration * wiring logging/configuration/bootstrap behavior —not reading per-run execution context. If your real goal is to associate something with the root flow run, you’ll need to do it from code that executes during the run, for example: * inside the flow itself * in a task * in a flow hook * in custom infrastructure/runtime logic that has access to run context Docs: * Runtime context * State change hooks If you want, tell me what you’re trying to do with the root flow run id in
setup_environment
, and I can suggest the closest Prefect 3-compatible pattern.
n
@Marvin doesn't setup_environment receive a context object as input?
m
thought for 6.5 seconds
Yes — but it’s plugin setup context, not flow-run context. In
setup_environment(self, *, ctx=...)
, the
ctx
object gives plugin/bootstrap information such as things like: * Prefect version * API URL * logger factory It does not include: * current flow run id * parent flow run id * root flow run id * flow/task run context So the distinction is: *
setup_environment(..., ctx=HookContext)
→ Prefect/plugin initialization context *
prefect.runtime.flow_run.*
or
get_run_context()
→ active execution context during a flow/task run So if your question is “can I get the root flow run id from the
ctx
passed to
setup_environment
?” the answer is no. If helpful, I can sketch: 1. what fields are actually on that plugin
ctx
, or 2. an alternative design for propagating the root flow run id to wherever your plugin needs it.
n
@Marvin tell me what fields ctx contains
m
thought for 74.7 seconds
setup_environment
receives a
HookContext
, and in Prefect 3 that context contains these fields: *
prefect_version
*
api_url
*
logger_factory
Conceptually:
Copy code
python
class HookContext:
    prefect_version: str
    api_url: str | None
    logger_factory: LoggerFactory
So a plugin can do things like:
Copy code
python
from prefect.plugins import register_hook

class MyPlugin:
    @register_hook
    def setup_environment(self, *, ctx):
        logger = ctx.logger_factory(__name__)
        <http://logger.info|logger.info>(
            "Prefect version=%s api_url=%s",
            ctx.prefect_version,
            ctx.api_url,
        )
What’s not in `ctx`: * flow run id * task run id * parent/root flow run id * deployment id * parameters * tags * run name/state So
ctx
is strictly environment/bootstrap metadata, not execution metadata. If you want, I can also show you the exact
HookContext
/
SetupResult
shape used by Prefect plugins.