Skip to content

Add support for get_current_context in Task SDK #45234

Closed

Description

@kaxil

The following task should work. This task is part of tutorial_taskflow_templates DAG

    from airflow.providers.standard.operators.python import get_current_context

    @task(
        # Causes variables that end with `.sql` to be read and templates
        # within to be rendered.
        templates_exts=[".sql"],
    )
    def template_test(sql, test_var, data_interval_end):
        context = get_current_context()

        # Will print...
        # select * from test_data
        # where 1=1
        #     and run_id = 'scheduled__2024-10-09T00:00:00+00:00'
        #     and something_else = 'param_from_task'
        print(f"sql: {sql}")

        # Will print `scheduled__2024-10-09T00:00:00+00:00`
        print(f"test_var: {test_var}")

        # Will print `2024-10-10 00:00:00+00:00`.
        # Note how we didn't pass this value when calling the task. Instead
        # it was passed by the decorator from the context
        print(f"data_interval_end: {data_interval_end}")

        # Will print...
        # run_id: scheduled__2024-10-09T00:00:00+00:00; params.other_param: from_dag
        template_str = "run_id: {{ run_id }}; params.other_param: {{ params.other_param }}"
        rendered_template = context["task"].render_template(
            template_str,
            context,
        )
        print(f"rendered template: {rendered_template}")

        # Will print the full context dict
        print(f"context: {context}")

Activity

  1. okirialbert commented on Dec 30, 2024

    @okirialbert
    Contributor

    Hi @kaxil, I'd like to work on this.

  2. self-assigned this
    on Jan 8, 2025
  3. kaxil commented on Jan 8, 2025

    @kaxil
    MemberAuthor

    Hey @okirialbert , thanks for your interest in getting this one but since we are in time crunch for Airflow 3.0 and since I am already on it, I will pick this up for now

  4. moved this from Next 2 Weeks to In Progress in AIP-72 - Task Execution Interface and SDKon Jan 8, 2025
  5. changed the title [-]Add support for `ti.get_current_context` in Task SDK[/-] [+]Add support for `get_current_context` in Task SDK[/+] on Jan 8, 2025
  6. 1 remaining item

  7. added this to the Airflow 3.0.0 milestone on Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions