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
action[ActionID](actionid.md?sid=flyte_models_actionid)The action ID of the current execution, which is always set within a run.
versionstrThe version of the executed task, set when the task is executed by an action and propagated to all sub-actions.
raw_data_path[RawDataPath](rawdatapath.md?sid=flyte_models_rawdatapath)The path to raw data used by the task.
input_path`strNone` = None
output_pathstrThe path where the task's output data will be stored.
run_base_dirstrThe base directory for the current task run.
report[Report](../report/report/report.md?sid=flyte_report__report_report)An object for generating reports related to the task execution.
group_data`GroupDataNone` = None
checkpoint_paths`CheckpointPathsNone` = None
code_bundle`CodeBundleNone` = None
compiled_image_cache`ImageCacheNone` = None
dataDict[str, Any] = {}A dictionary to store arbitrary key-value data for the task context.
modeLiteral["local", "remote", "hybrid"] = remoteThe execution mode of the task, which can be 'local', 'remote', or 'hybrid'.
interactive_modebool = falseA boolean indicating if the task is running in interactive mode.
custom_contextDict[str, str] = {}Context metadata for the action, which is automatically passed to any spawned actions but not used for cache key computation.
disable_run_cachebool = falseA boolean flag to disable the run cache for the current task execution.
in_driver_literal_conversionbool = falseTrue while converting literals for nested-task / driver orchestration (not task-body I/O), indicating that type transformers should omit non-essential side effects (e.g. duplicate HTML tabs) when set.

Methods


replace()

@classmethod
def replace(
kwargs: **kwargs
) - > [TaskContext](taskcontext.md?sid=flyte_models_taskcontext)

Replaces or updates attributes of the TaskContext instance. If 'data' is provided, it merges the new data with the existing data.

Parameters

NameTypeDescription
kwargs**kwargsKeyword arguments representing the attributes to replace or update in the TaskContext. If 'data' is included, its values are merged with the existing 'data' dictionary.

Returns

TypeDescription
[TaskContext](taskcontext.md?sid=flyte_models_taskcontext)A new TaskContext instance with the specified attributes replaced or updated.

is_in_cluster()

@classmethod
def is_in_cluster() - > bool

Check if the task is running in a cluster.

Returns

TypeDescription
boolTrue if the task's mode is 'remote', indicating it is running in a cluster; otherwise, False.

checkpoint()

@classmethod
def checkpoint() - > Optional[[Checkpoint](../checkpoint/checkpoint.md?sid=flyte__checkpoint_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 Checkpoint object if checkpoint paths are configured, otherwise None.

attempt_number()

@classmethod
def attempt_number() - > int

Get the attempt number for the current task.

Returns

TypeDescription
intThe attempt number, retrieved from the 'FLYTE_ATTEMPT_NUMBER' environment variable, defaulting to 0 if not set.