Skip to main content

TaskTemplate

Task template is a template for a task that can be executed. It defines various parameters for the task, which can be defined statically at the time of task definition or dynamically at the time of task invocation using the override method.

Attributes

AttributeTypeDescription
namestrThe name of the task (defaults to the function name)
interface[NativeInterface](../models/nativeinterface.md?sid=flyte_models_nativeinterface)Defines the input and output types for the task to ensure type safety during execution.
short_namestrOptional override for the short name of the task, defaulting to the task name if not provided.
task_typestr = pythonRouter type for the task, this is used to determine how the task will be executed.
task_type_versionint = 0Version number of the task type used for compatibility and plugin routing.
image`Union[str, Image, Literal["auto"]]None` = auto
resourcesOptional[[Resources](../resources/resources.md?sid=flyte__resources_resources)] = nullThe resources to use for the task
cacheCacheRequest = disableThe cache policy for the task, defaults to auto, which will cache the results of the task.
interruptiblebool = falseThe interruptible policy for the task, defaults to False, which means the task will not be scheduled on interruptible nodes.
retriesUnion[int, [RetryStrategy](../retry/retrystrategy.md?sid=flyte__retry_retrystrategy)] = 0The number of retries for the task, defaults to 0, which means no retries.
reusableUnion[[ReusePolicy](../reusable/environment/reusepolicy.md?sid=flyte__reusable_environment_reusepolicy), None] = nullThe reusability policy for the task, defaults to None, which means the task environment will not be reused across task invocations.
docsOptional[Documentation] = nullThe documentation for the task, if not provided the function docstring will be used.
env_varsOptional[Dict[str, str]] = nullThe environment variables to set for the task.
secretsOptional[SecretRequest] = nullThe secrets that will be injected into the task at runtime.
timeoutOptional[TimeoutType] = nullThe timeout for the task.
pod_templateOptional[Union[str, [PodTemplate](../pod/podtemplate.md?sid=flyte__pod_podtemplate)]] = nullThe pod template to use for the task.
reportbool = falseWhether to report the task execution to the Flyte console, defaults to False.
queueOptional[str] = nullThe queue to use for the task. If not provided, the default queue will be used.
debuggablebool = falseWhether the task supports debugging capabilities, defaults to False.
entrypointbool = falseFlag indicating whether this task serves as the primary entrypoint for execution.
parent_envOptional[weakref.ReferenceType[[TaskEnvironment](environment/taskenvironment.md?sid=flyte__task_environment_taskenvironment)]] = nullA weak reference to the environment that created the task, used to avoid circular references during pickling.
parent_env_nameOptional[str] = nullThe name of the parent environment associated with this task template.
refbool = falseInternal flag used to determine if the task is a reference to another task.
max_inline_io_bytesint = MAX_INLINE_IO_BYTESMaximum allowed size (in bytes) for all inputs and outputs passed directly to the task (e.g., primitives, strings, dicts).
triggersTuple[[Trigger](../remote/trigger/trigger.md?sid=flyte_remote__trigger_trigger), ...] = ()A collection of triggers that define when the task should be automatically executed.
linksTuple[[Link](../app/types/link.md?sid=flyte_app__types_link), ...] = ()A collection of external links associated with the task for documentation or tracking purposes.

Constructor

Signature

def TaskTemplate(
name: str,
interface: [NativeInterface](../models/nativeinterface.md?sid=flyte_models_nativeinterface),
short_name: str = "",
task_type: str = "python",
image: Union[str, [Image](../image/image.md?sid=flyte__image_image), Literal["auto"]] = "auto",
resources: Optional[[Resources](../resources/resources.md?sid=flyte__resources_resources)] = null,
cache: CacheRequest = "disable",
interruptible: bool = false,
retries: Union[int, [RetryStrategy](../retry/retrystrategy.md?sid=flyte__retry_retrystrategy)] = 0,
reusable: Union[[ReusePolicy](../reusable/environment/reusepolicy.md?sid=flyte__reusable_environment_reusepolicy), None] = null,
docs: Optional[Documentation] = null,
env_vars: Optional[Dict[str, str]] = null,
secrets: Optional[SecretRequest] = null,
timeout: Optional[TimeoutType] = null,
pod_template: Optional[Union[str, [PodTemplate](../pod/podtemplate.md?sid=flyte__pod_podtemplate)]] = null,
report: bool = false,
queue: Optional[str] = null,
debuggable: bool = false,
max_inline_io_bytes: int = MAX_INLINE_IO_BYTES
) - > null

Parameters

NameTypeDescription
namestrThe name of the task (defaults to the function name).
interface[NativeInterface](../models/nativeinterface.md?sid=flyte_models_nativeinterface)The interface defining inputs and outputs of the task.
short_namestr = ""A shorter identifier for the task.
task_typestr = "python"Router type used to determine the execution plugin.
imageUnion[str, [Image](../image/image.md?sid=flyte__image_image), Literal["auto"]] = "auto"The container image to use for the task.
resourcesOptional[[Resources](../resources/resources.md?sid=flyte__resources_resources)] = nullThe compute resources (CPU, memory, etc.) required.
cacheCacheRequest = "disable"The cache policy for the task results.
interruptiblebool = falseWhether the task can be scheduled on interruptible nodes.
retriesUnion[int, [RetryStrategy](../retry/retrystrategy.md?sid=flyte__retry_retrystrategy)] = 0The number of retries or retry strategy for the task.
reusableUnion[[ReusePolicy](../reusable/environment/reusepolicy.md?sid=flyte__reusable_environment_reusepolicy), None] = nullPolicy for environment reusability across invocations.
docsOptional[Documentation] = nullDocumentation for the task.
env_varsOptional[Dict[str, str]] = nullEnvironment variables to set for the task.
secretsOptional[SecretRequest] = nullSecrets to be injected at runtime.
timeoutOptional[TimeoutType] = nullThe maximum execution time for the task.
pod_templateOptional[Union[str, [PodTemplate](../pod/podtemplate.md?sid=flyte__pod_podtemplate)]] = nullThe pod template to use for the task.
reportbool = falseWhether to report execution to the Flyte console.
queueOptional[str] = nullThe specific queue to use for task execution.
debuggablebool = falseWhether the task supports debugging capabilities.
max_inline_io_bytesint = MAX_INLINE_IO_BYTESMaximum size for inputs/outputs passed directly.

Methods


source_file()

@classmethod
def source_file() - > str | null

Retrieves the path to the source file where the task is defined.

Returns

TypeDescription
`strnull`

pre()

@classmethod
def pre(
args: Any,
kwargs: Any
) - > dict

This is the preexecute function that will be called before the task is executed

Parameters

NameTypeDescription
argsAnyPositional arguments passed to the task
kwargsAnyKeyword arguments passed to the task

Returns

TypeDescription
dictA dictionary of values to be used during task execution

execute()

@classmethod
def execute(
args: Any,
kwargs: Any
) - > Any

This is the pure python function that will be executed when the task is called.

Parameters

NameTypeDescription
argsAnyPositional arguments for the task logic
kwargsAnyKeyword arguments for the task logic

Returns

TypeDescription
AnyThe result of the task execution

post()

@classmethod
def post(
return_vals: Any
) - > Any

This is the postexecute function that will be called after the task is executed

Parameters

NameTypeDescription
return_valsAnyThe raw output produced by the execute method

Returns

TypeDescription
AnyThe processed return values

config()

@classmethod
def config(
sctx: [SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)
) - > dict

Returns additional configuration for the task. This is a set of key-value pairs that can be used to configure the task execution environment at runtime. This is usually used by plugins.

Parameters

NameTypeDescription
sctx[SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)The context used to manage serialization state

Returns

TypeDescription
dictA dictionary of configuration keys and values

custom_config()

@classmethod
def custom_config(
sctx: [SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)
) - > dict

Returns additional configuration for the task. This is a set of key-value pairs that can be used to configure the task execution environment at runtime. This is usually used by plugins.

Parameters

NameTypeDescription
sctx[SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)The context used to manage serialization state

Returns

TypeDescription
dictA dictionary of plugin-specific configuration settings

data_loading_config()

@classmethod
def data_loading_config(
sctx: [SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)
) - > DataLoadingConfig

This configuration allows executing raw containers in Flyte using the Flyte CoPilot system

Parameters

NameTypeDescription
sctx[SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)The context used to manage serialization state

Returns

TypeDescription
DataLoadingConfigThe configuration defining how inputs and outputs are side-loaded

container_args()

@classmethod
def container_args(
sctx: [SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)
) - > list

Returns the container args for the task. This is a set of key-value pairs that can be used to configure the task execution environment at runtime. This is usually used by plugins.

Parameters

NameTypeDescription
sctx[SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)The context used to manage serialization state

Returns

TypeDescription
listA list of command-line arguments for the container

sql()

@classmethod
def sql(
sctx: [SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)
) - > str | null

Returns the SQL for the task. This is a set of key-value pairs that can be used to configure the task execution environment at runtime. This is usually used by plugins.

Parameters

NameTypeDescription
sctx[SerializationContext](../models/serializationcontext.md?sid=flyte_models_serializationcontext)The context used to manage serialization state

Returns

TypeDescription
`strnull`

native_interface()

@classmethod
def native_interface() - > [NativeInterface](../models/nativeinterface.md?sid=flyte_models_nativeinterface)

Provides access to the native interface definition of the task.

Returns

TypeDescription
[NativeInterface](../models/nativeinterface.md?sid=flyte_models_nativeinterface)The interface object defining inputs and outputs

aio()

@classmethod
def aio(
args: Any,
kwargs: Any
) - > Coroutine | Any

The aio function allows executing "sync" tasks, in an async context. This helps with migrating v1 defined sync tasks to be used within an asyncio parent task.

Parameters

NameTypeDescription
argsAnyPositional arguments for the task
kwargsAnyKeyword arguments for the task

Returns

TypeDescription
`CoroutineAny`

forward()

@classmethod
def forward(
args: Any,
kwargs: Any
) - > Coroutine | Any

Think of this as a local execute method for your task. This function will be invoked by the call method when not in a Flyte task execution context.

Parameters

NameTypeDescription
argsAnyArguments for local execution
kwargsAnyKeyword arguments for local execution

Returns

TypeDescription
`CoroutineAny`

override()

@classmethod
def override(
short_name: str | null,
resources: Resources | null,
cache: CacheRequest | null,
retries: int | RetryStrategy,
timeout: TimeoutType | null,
reusable: ReusePolicy | str | null,
env_vars: dict | null,
secrets: SecretRequest | null,
max_inline_io_bytes: int | null,
pod_template: str | PodTemplate | null,
queue: str | null,
interruptible: bool | null,
entrypoint: bool | null,
links: tuple
) - > [TaskTemplate](tasktemplate.md?sid=flyte__task_tasktemplate)

Override various parameters of the task template. This allows for dynamic configuration of the task when it is called, such as changing the image, resources, cache policy, etc.

Parameters

NameTypeDescription
short_name`strnull`
resources`Resourcesnull`
cache`CacheRequestnull`
retries`intRetryStrategy`
timeout`TimeoutTypenull`
reusable`ReusePolicystr
env_vars`dictnull`
secrets`SecretRequestnull`
max_inline_io_bytes`intnull`
pod_template`strPodTemplate
queue`strnull`
interruptible`boolnull`
entrypoint`boolnull`
linkstupleOptional override for external links associated with the task

Returns

TypeDescription
[TaskTemplate](tasktemplate.md?sid=flyte__task_tasktemplate)A new TaskTemplate instance with the overridden parameters