Nicholas Pini
05/27/2026, 5:13 PMMarvin
05/27/2026, 5:13 PMMarvin
05/27/2026, 5:14 PMpython
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:
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.Nicholas Pini
05/27/2026, 5:15 PMMarvin
05/27/2026, 5:15 PMMarvin
05/27/2026, 5:15 PMsetup_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`:
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.Nicholas Pini
05/27/2026, 5:16 PMMarvin
05/27/2026, 5:16 PMMarvin
05/27/2026, 5:16 PMsetup_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.Nicholas Pini
05/27/2026, 5:17 PMMarvin
05/27/2026, 5:17 PMMarvin
05/27/2026, 5:18 PMsetup_environment receives a HookContext, and in Prefect 3 that context contains these fields:
* prefect_version
* api_url
* logger_factory
Conceptually:
python
class HookContext:
prefect_version: str
api_url: str | None
logger_factory: LoggerFactory
So a plugin can do things like:
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.