<@ULVA73B9P> from the TaskRun object, how can I re...
# ask-marvin
s
@Marvin from the TaskRun object, how can I retrieve the parameters that were provided to the task when launched ?
m
thought for 466.3 seconds
Great question — there are two common ways to get a task’s parameters, depending on where you’re running the code. 1) Inside the running task (at execution time) - Use the run context or runtime module; both give you the fully resolved parameters as a dict.
Copy code
from prefect.context import get_run_context

@task
def my_task():
    ctx = get_run_context()
    params = ctx.parameters  # dict[str, Any] of resolved inputs
    ...
or
Copy code
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:
Copy code
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).