Skip to main content

TaskContext

A context class to hold the current task executions context. This can be used to access various contextual parameters in the task execution by the user.

Attributes​

AttributeTypeDescription
actionActionIDThe action ID of the current execution. This is always set, within a run.
versionstrThe version of the executed task. This is set when the task is executed by an action and will be set on all sub-actions.
raw_data_pathRawDataPathStorage location prefix used for reading and writing raw task data.
input_path`strNone` = None
output_pathstrPath where task execution outputs and results are written.
run_base_dirstrRoot base directory allocated on the local filesystem for the current execution run.
reportReportReport collector used to capture artifacts and render execution summaries.
group_data`GroupDataNone` = None
checkpoint_paths`CheckpointPathsNone` = None
code_bundle`CodeBundleNone` = None
compiled_image_cache`ImageCacheNone` = None
dataDict[str, Any] = {}Dictionary of runtime data and cached helpers shared across the task lifecycle.
modeLiteral["local", "remote", "hybrid"] = "remote"Execution environment mode representing where the task is run. Must be one of 'local', 'remote', or 'hybrid'.
interactive_modebool = FalseFlag indicating whether the task is executing within an interactive session.
custom_contextDict[str, str] = {}Context metadata for the action. If an action receives context, it'll automatically pass it to any actions it spawns. Context will not be used for cache key computation.
disable_run_cachebool = FalseFlag specifying whether caching across task runs is disabled.
in_driver_literal_conversionbool = FalseSet by the runtime during nested-task literal marshalling; type transformers may use it to skip duplicate side effects (e.g. report tabs) outside true task-body I/O.

Constructor​

Signature​

def TaskContext(
action: ActionID,
version: str,
raw_data_path: RawDataPath,
input_path: str | None = None,
output_path: str,
run_base_dir: str,
report: Report,
group_data: GroupData | None = None,
checkpoint_paths: CheckpointPaths | None = None,
code_bundle: CodeBundle | None = None,
compiled_image_cache: ImageCache | None = None,
data: Dict[str, Any] = <factory: dict>,
mode: Literal['local', 'remote', 'hybrid'] = "remote",
interactive_mode: bool = False,
custom_context: Dict[str, str] = <factory: dict>,
disable_run_cache: bool = False,
in_driver_literal_conversion: bool = False
) -> None

Parameters​

NameTypeDescription
actionActionIDThe action ID of the current execution. Always set within a run.
versionstrThe version of the executed task.
raw_data_pathRawDataPathThe path for raw data.
input_path`strNone` = None
output_pathstrThe output path for the task execution.
run_base_dirstrThe base directory for the run.
reportReportThe report object for the execution.
group_data`GroupDataNone` = None
checkpoint_paths`CheckpointPathsNone` = None
code_bundle`CodeBundleNone` = None
compiled_image_cache`ImageCacheNone` = None
dataDict[str, Any] = < factory: dict >Dictionary holding contextual execution data.
modeLiteral['local', 'remote', 'hybrid'] = "remote"The execution mode of the task.
interactive_modebool = FalseFlag indicating whether interactive mode is enabled.
custom_contextDict[str, str] = < factory: dict >Context metadata for the action.
disable_run_cachebool = FalseFlag indicating whether run caching is disabled.
in_driver_literal_conversionbool = FalseFlag indicating whether the runtime is converting literals for nested-task / driver orchestration.

Methods​


replace()​

def replace(self, **kwargs) -> TaskContext: ...

Creates a new copy of the TaskContext with specified fields replaced or updated, merging any provided data dictionary with the existing context data.

Parameters​

NameTypeDescription
**kwargsAnyField values to replace or update on the context instance.

Returns​

TypeDescription
[TaskContext](taskcontext.md?sid=flyte_models_taskcontext)A new TaskContext instance containing the updated field values.

is_in_cluster()​

def is_in_cluster(self): ...

Check if the task is running in a cluster.

Returns​

TypeDescription
boolTrue if the task is running in remote mode on a cluster, False otherwise.

checkpoint()​

@property
def checkpoint(self) -> Optional[Checkpoint]: ...

Task checkpoint helper for the runtime checkpoint_path / prev_checkpoint prefixes. Returns a lazily constructed flyte.Checkpoint cached on flyte.models.TaskContext.data, or None when no checkpoint output prefix is configured. In async tasks use flyte.Checkpoint.load and flyte.Checkpoint.save; in sync tasks use flyte.Checkpoint.load_sync and flyte.Checkpoint.save_sync. For a single raw blob, pass bytes to save; after a successful load, the blob is at checkpoint.path / "payload" when the remote object is not a tarball.

Returns​

TypeDescription
Optional[[Checkpoint](../checkpoint/checkpoint.md?sid=flyte__checkpoint_checkpoint)]A lazily constructed Checkpoint instance cached on TaskContext.data, or None when no checkpoint output prefix is configured.

attempt_number()​

@property
def attempt_number(self) -> int: ...

Get the attempt number for the current task.

Returns​

TypeDescription
intThe attempt number.