snowflake.core.task.context.TaskContext

class snowflake.core.task.context.TaskContext(session: Session)

Bases: object

Represents the context in a Snowflake Task.

When a Task’s definition is a StoredProcedureCall, the handler of the Stored Procedure can use this class to set the return value of the Task so the immediate successor tasks can use it.

Methods

__init__(session: Session) None

Initialize a TaskContext object.

Parameters:

session – a Snowpark session.

get_current_root_task_name() str

Return the current task’s root task name.

get_current_root_task_uuid() str

Return the current task’s root task UUID.

get_current_task_graph_original_schedule() str

Return the current task’s original schedule for this run or its root task’s schedule.

get_current_task_graph_run_group_id() str

Return the current task’s run group.

get_current_task_name() str

Return the name of the task currently executing.

This method can only be called in a Snowflake task.

Example

>>> def task_handler(session: Session) -> None:
>>>     from snowflake.core.task.context import TaskContext
>>>     context = TaskContext(session)
>>>     task_name = context.get_current_task_name()
Copy
get_current_task_short_name() str

Return the task name under the context of a DAG.

If a snowflake.core.task.dagv1.DAGTask is created under a snowflake.core.task.dagv1.DAG object, the real task name is in format {dag name}${task name}. This API returns the task name part. get_current_task_name() returns the real task.

get_last_successful_task_graph_original_schedule() str | None

Return the last successful task run schedule.

get_last_successful_task_graph_run_group_id() str | None

Return the last successful task run group id.

get_predecessor_return_value(task_name: str | None = None) str

Retrieve the return value from the predecessor task in a DAG of tasks.

The return value is explicitly set by the predecessor task using set_return_value(). This method can only be called in a Snowflake task.

See SYSTEM$GET_PREDECESSOR_RETURN_VALUE for details.

Parameters:

task_name

The task name of the predecessor task that sets the return value to be retrieved.

  • If the task has only one predecessor task that is enabled, the argument is optional. If this argument is omitted, the function retrieves the return value for the only enabled predecessor task.

  • If the task has multiple predecessor tasks that are enabled, this argument is required.

Example

>>> def task_handler(session: Session) -> None:
>>>     from snowflake.core.task.context import TaskContext
>>>     context = TaskContext(session)
>>>     pred_return_value = context.get_predecessor_return_value("pred_task_name")
Copy
get_runtime_info(property_name: str) str | datetime | None

Return the runtime information of the current task.

You usually don’t need to call this function. Call the other get_* functions instead.

get_task_graph_config() Dict[str, Any] | None

Return the graph config of the task.

get_task_graph_config_property(property_name: str) Any | None

Return the graph config of the task.

set_return_value(value: Any) None

Explicitly sets the return value for a task.

An immediate successor task can then use get_predecessor_return_value() to retrieve it. See SYSTEM$SET_RETURN_VALUE for details. This method can only be called in a Snowflake task.

Parameters:

value – The return value for a task. It will be converted to a str when the underlying SQL system function is called.

Example

>>> def task_handler(session: Session) -> None:
>>>     from snowflake.core.task.context import TaskContext
>>>     context = TaskContext(session)
>>>     # this return value can be retrieved by successor Tasks.
>>>     context.set_return_value("predecessor_return_value")
Copy