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. 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_path[RawDataPath](rawdatapath.md?sid=flyte_models_rawdatapath)The path to the raw data storage location for the current task execution.
input_path`strNone` = null
output_pathstrThe file path or URI where the task's output data should be written.
run_base_dirstrThe base directory on the local file system for the current task run.
report[Report](../report/report/report.md?sid=flyte_report__report_report)The report object used to log or visualize task execution details.
group_data`GroupDataNone` = null
checkpoint_paths`CheckpointPathsNone` = null
code_bundle`CodeBundleNone` = null
compiled_image_cache`ImageCacheNone` = null
dataDict[str, Any] = {}A dictionary for storing arbitrary execution data and cached objects like checkpoints.
modeLiteral["local", "remote", "hybrid"] = "remote"The execution environment mode, which can be local, remote, or hybrid.
interactive_modebool = falseA flag indicating whether the task is running in 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.
disable_run_cachebool = falseA flag to explicitly disable caching for the current task execution.
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](actionid.md?sid=flyte_models_actionid),
version: str,
raw_data_path: [RawDataPath](rawdatapath.md?sid=flyte_models_rawdatapath),
input_path: str | None = None,
output_path: str,
run_base_dir: str,
report: [Report](../report/report/report.md?sid=flyte_report__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] = field(default_factory=dict),
mode: Literal['local', 'remote', 'hybrid'] = remote,
interactive_mode: bool = False,
custom_context: Dict[str, str] = field(default_factory=dict),
disable_run_cache: bool = False,
in_driver_literal_conversion: bool = False
) - > null

Parameters

NameTypeDescription
action[ActionID](actionid.md?sid=flyte_models_actionid)The action ID of the current execution.
versionstrThe version of the executed task.
raw_data_path[RawDataPath](rawdatapath.md?sid=flyte_models_rawdatapath)The path for raw data storage.
input_path`strNone` = None
output_pathstrThe path where task outputs should be stored.
run_base_dirstrThe base directory for the current run.
report[Report](../report/report/report.md?sid=flyte_report__report_report)The report object for the task.
group_data`GroupDataNone` = None
checkpoint_paths`CheckpointPathsNone` = None
code_bundle`CodeBundleNone` = None
compiled_image_cache`ImageCacheNone` = None
dataDict[str, Any] = field(default_factory=dict)A dictionary for storing arbitrary contextual data.
modeLiteral['local', 'remote', 'hybrid'] = remoteThe execution mode of the task.
interactive_modebool = FalseWhether the task is running in interactive mode.
custom_contextDict[str, str] = field(default_factory=dict)Context metadata for the action passed to sub-actions.
disable_run_cachebool = FalseWhether to disable the run cache for this task.
in_driver_literal_conversionbool = FalseFlag indicating if the runtime is performing nested-task literal marshalling.

Methods


replace()

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

Creates a new TaskContext instance with updated attributes, merging the provided data dictionary with existing context data.

Parameters

NameTypeDescription
**kwargsAnyKey-value pairs representing the TaskContext attributes to update, such as 'data', 'mode', or 'custom_context'.

Returns

TypeDescription
[TaskContext](taskcontext.md?sid=flyte_models_taskcontext)A new TaskContext instance containing the updated configuration and merged data.

is_in_cluster()

@classmethod
def is_in_cluster() - > bool

Check if the task is running in a cluster.

Returns

TypeDescription
boolTrue if the execution mode is 'remote', indicating it is running within a cluster environment.

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

TypeDescription
Optional[[Checkpoint](../checkpoint/checkpoint.md?sid=flyte__checkpoint_checkpoint)]A lazily constructed Checkpoint object for managing state persistence, or None if no checkpoint path is configured.

attempt_number()

@classmethod
def attempt_number() - > int

Get the attempt number for the current task.

Returns

TypeDescription
intThe integer representation of the current task execution attempt, sourced from environment variables.