Syméon del Marmol
10/23/2025, 8:13 AMMarvin
10/23/2025, 8:13 AMMarvin
10/23/2025, 8:21 AMfrom prefect.context import get_run_context
@task
def my_task():
ctx = get_run_context()
params = ctx.parameters # dict[str, Any] of resolved inputs
...
or
from prefect.runtime import task_run
@task
def my_task():
params = task_run.parameters()
...
Docs: Runtime context
2) From a TaskRun object (outside the task)
- What you have on the TaskRun Pydantic object is metadata. There are two relevant fields:
- `task_inputs`: the raw input references (not resolved; can include references to other runs/futures)
- `task_parameters_id`: when tasks are executed by task workers (e.g., via .delay()), Prefect persists the resolved parameters and stores an identifier here so they can be fetched later
If you want the actual resolved parameters and the task was run by a task worker, use the result store helpers:
from prefect.results import get_result_store
from prefect.task_worker import read_parameters
# assuming you already have a TaskRun object `tr`
# e.g. via `async with get_client() as c: tr = await c.read_task_run(task_run_id)`
store = get_result_store()
if tr.task_parameters_id is not None:
params = read_parameters(store, tr.task_parameters_id) # dict[str, Any]
else:
# Fallback: only raw references are available on `task_inputs`
params = None # or inspect `tr.task_inputs` (unresolved)
Notes and caveats:
- If task_parameters_id is None, the task likely was not executed by a task worker (e.g., it ran in-process), so there may be no persisted resolved-parameter blob to fetch. In that case, you can still inspect tr.task_inputs, but they are raw references, not the final Python values.
- The REST read for a task run is here: Read Task Run
If you can share whether you need this inside the task at runtime or after the fact from another process, I can tailor the example further (including async client code to fetch the TaskRun first).