Skip to content

template variable start_date doesn't exist #62457

Description

@obarisk

Apache Airflow version

3.1.7

If "Other Airflow 3 version" selected, which one?

No response

What happened?

the template variable start_date listed in document doesn't exist

an example dag

@dag
def report_no_start_date():
    @task
    def print_context(**context):
        print(context["start_date"])

    @task(templates_exts=[".sql"])
    def print_template(sql):
        print(sql)

    print_context()
    print_template("demo.sql")

report_no_start_date()

with a template file demo.sql

select '{{ start_date }}';

What you think should happen instead?

at least two options

a. remove start_date from the template document.
dag developer could find start_date from dag_run.start_date or task_instance.start_date (the type is datetime.datetime)

b. add start_date (pendulum.DateTime) into context

How to reproduce

use the dag in the description.

Operating System

debian

Versions of Apache Airflow Providers

No response

Deployment

Other Docker-based deployment

Deployment details

No response

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. Lee-W commented on Feb 25, 2026

    @Lee-W
    Member

    @obarisk I like a. Is is something supported in Airflow 2?

  2. obarisk commented on Feb 25, 2026

    @obarisk
    ContributorAuthor

    seems new for airflow 3.

    in the latest airflow 2 document, there's no start_date

    https://airflow.apache.org/docs/apache-airflow/2.11.1/templates-ref.html

  3. Ajay9704 commented on Feb 25, 2026

    @Ajay9704
    Contributor

    Analysis of the start_date Inconsistency

    I wanted to provide a clear summary of the inconsistency around start_date in the template context.

    What Changed in Commit 518287c

    The commit “Runtime context shouldn't have start_date as a key (#46961)” removed start_date from the runtime template context produced by get_template_context(). However, the following places were not updated and still reference it:

    task-sdk/src/airflow/sdk/definitions/context.py
    (This still defines start_date: DateTime in the Context TypedDict)

    airflow-core/docs/templates-ref.rst
    (This still documents {{ start_date }} as an available template field)

    Because these areas did not get updated, they no longer match runtime behavior.

    Current State (Misaligned)

    The current behavior looks like this:

    Runtime context (get_template_context())
    Does not include start_date
    This is correct, as the commit intentionally removed it.

    TypedDict (Context, located in task-sdk/src/airflow/sdk/definitions/context.py)
    Still lists start_date
    This is incorrect, because the runtime no longer provides it.

    Documentation (airflow-core/docs/templates-ref.rst)
    Still documents {{ start_date }}
    This is incorrect, because using it now raises an error.

    User Impact

    Right now, if a user follows the documentation and writes:

    {{ start_date }}

    they receive:

    KeyError: 'start_date'

    The only currently valid ways to access it are:

    ti.start_date
    task_instance.start_date

    So documentation, type hints, and runtime behavior do not match.

    Two Possible Fix Approaches

    I see two logical directions to resolve this inconsistency. Both are technically valid; the maintainers' intention will decide which is appropriate.

    Option A: Remove start_date from TypedDict and Documentation

    (Aligns with the intent of commit 518287c)

    This requires:

    Removing the start_date entry from the Context TypedDict
    File: task-sdk/src/airflow/sdk/definitions/context.py

    Removing the reference to {{ start_date }} from the documentation
    File: airflow-core/docs/templates-ref.rst

    This would make type hints and documentation match the current runtime behavior.

    Option B: Add start_date Back to the Runtime Context
    (If the project prefers keeping the old template behavior)

    This would require updating the template context assembly in:

    task-sdk/src/airflow/sdk/execution_time/task_runner.py

    Specifically, restoring start_date inside the cached template context.
    This would make runtime, documentation, and TypedDict consistent again, but it would partially undo commit 518287c.

  4. Ajay9704 commented on Feb 25, 2026

    @Ajay9704
    Contributor

    @Lee-W , @obarisk I think this might help in understanding the issue better.

  5. obarisk commented on Feb 26, 2026

    @obarisk
    ContributorAuthor

    Cool. I believe you already have enough information to create a PR.
    Personaly I prefer option A as start_date is already somewhere else in the context. It's not cool have duplicate data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:corekind:bugThis is a clearly a bugneeds-triagelabel for new issues that we didn't triage yet

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions