Dmytro Stelmakh
05/19/2026, 2:23 PMMarvin
05/19/2026, 2:23 PMMarvin
05/19/2026, 2:25 PMget_run_context().flow_run.id inside a task.
In Prefect 3.x:
- get_run_context() inside a flow returns a flow context, so ctx.flow_run.id works there.
- get_run_context() inside a task returns a task context, so there is no ctx.flow_run.
So this will fail in a task:
python
from prefect.context import get_run_context
ctx = get_run_context()
flow_run_id = ctx.flow_run.id # won't work in a task
Use one of these instead from a task:
python
from prefect import task
from prefect.runtime import flow_run
@task
def my_task():
print(flow_run.id)
Or via the task run context:
python
from prefect import task
from prefect.context import get_run_context
@task
def my_task():
ctx = get_run_context()
print(ctx.task_run.flow_run_id)
Recommended: prefect.runtime.flow_run.id
Example:
python
from prefect import flow, task
from prefect.runtime import flow_run
@task
def my_task():
print(f"Task can see flow run id: {flow_run.id}")
@flow
def my_flow():
print(f"Flow run id: {flow_run.id}")
my_task()
my_flow()
So the short answer is:
- In a flow: get_run_context().flow_run.id ✅
- In a task: use flow_run.id or get_run_context().task_run.flow_run_id ✅
Docs: Runtime contextDmytro Stelmakh
05/20/2026, 1:27 PMMarvin
05/20/2026, 1:28 PMMarvin
05/20/2026, 1:28 PMpython
from prefect import task
from prefect.runtime import flow_run
@task
def my_task():
print(flow_run.name)
A couple notes:
- flow_run.name = the run name of the current flow run
- if you want the flow's definition name instead, use:
python
from prefect import task
from prefect.runtime import flow
@task
def my_task():
print(flow.name)
So from inside a task:
- current flow run name → prefect.runtime.flow_run.name
- current flow name → prefect.runtime.flow.name
Example:
python
from prefect import flow, task
from prefect.runtime import flow, flow_run
@task
def my_task():
print(f"Flow name: {flow.name}")
print(f"Flow run name: {flow_run.name}")
@flow(name="daily-etl")
def my_flow():
my_task()
my_flow()
Docs: Runtime contextDmytro Stelmakh
05/20/2026, 1:29 PMtask_name = get_run_context().task.name
flow_name = flow.nameMarvin
05/20/2026, 1:29 PMMarvin
05/20/2026, 1:30 PMpython
from prefect.context import get_run_context
from prefect.runtime import flow
ctx = get_run_context()
task_name = ctx.task.name
flow_name = flow.name
That’s a good pattern.
Quick rule of thumb:
- use get_run_context() for task-specific or flow-specific context objects
- ctx.task.name
- ctx.task_run.id
- use prefect.runtime for runtime metadata that should work cleanly across contexts
- flow.name
- flow_run.id
- flow_run.name
So for your example:
python
task_name = get_run_context().task.name
flow_name = flow.name
Yes, that’s correct.
One small nuance:
- ctx.task.name is the task definition name
- flow.name is the flow definition name
- if you wanted the flow run name, use flow_run.name
Example:
python
from prefect import task
from prefect.context import get_run_context
from prefect.runtime import flow, flow_run
@task
def my_task():
ctx = get_run_context()
task_name = ctx.task.name
flow_name = flow.name
flow_run_name = flow_run.name
print(task_name, flow_name, flow_run_name)
If you want, I can also give you a small cheat sheet for what to access from get_run_context() vs prefect.runtime inside tasks and flows.Dmytro Stelmakh
05/20/2026, 1:31 PMfrom prefect.runtime import flow - flow is underlined redMarvin
05/20/2026, 1:31 PMMarvin
05/20/2026, 1:33 PMfrom prefect.runtime import flow is not a valid import in Prefect 3.x.
Use one of these instead:
To get the flow's name from inside a task:
python
from prefect.runtime import flow_run
flow_name = flow_run.flow_name
Or via context:
python
from prefect.context import get_run_context
flow_name = get_run_context().flow.name
So your corrected version would be:
python
from prefect.context import get_run_context
from prefect.runtime import flow_run
ctx = get_run_context()
task_name = ctx.task.name
flow_name = flow_run.flow_name
Or, if you want to use context for both:
python
from prefect.context import get_run_context
ctx = get_run_context()
task_name = ctx.task.name
flow_name = ctx.flow.name
Recommendation:
- ctx.task.name for the task name
- flow_run.flow_name for the flow name
- flow_run.name for the flow run name
Docs: Runtime context