Skip to content

Task SDK: no supported way to list Variable keys/values (replacement for ORM Variable.all) in Airflow 3 #61166

Closed

Description

@akhundovte

Description

In Airflow 2.x we were able to bulk-read all Variables from the metastore using ORM:

from airflow.utils.session import create_session
from airflow.models import Variable

with create_session() as session:
    airflow_vars_from_db = session.query(Variable).all()

In Airflow 3.x this pattern fails (in our case the error happens during DAG parsing) because direct access to the metastore via the ORM is not allowed and results in:
RuntimeError: Direct database access via the ORM is not allowed in Airflow 3.0

Task SDK provides Variable.get(key) but does not provide any supported way to list Variable keys. This makes it impossible to implement patterns where variable keys must be discovered dynamically (e.g. list all keys or keys by prefix) when the list of keys is not known ahead of time.

Use case/motivation

We have multiple DAGs configured via many Airflow Variables created/updated by external systems. The set of variable keys is not known at DAG authoring time and can change over time.

We want to collect Variables at DAG parse time and pass the collected “snapshot” into a task (e.g. PythonOperator) via op_kwargs, so the task can operate on a consistent set of configuration values.

Minimal example showing the desired pattern:

from __future__ import annotations

from pendulum import datetime
from airflow.sdk import dag, Variable
from airflow.providers.standard.operators.python import PythonOperator


def use_vars_snapshot(vars_snapshot: dict[str, str], **context):
    print(f"Loaded {len(vars_snapshot)} variables")


@dag(start_date=datetime(2025, 1, 1), schedule=None, catchup=False)
def demo_pythonoperator_pass_all_vars():
    # Desired: discover ALL Variable keys (or keys by prefix) during DAG parsing.
    # Problem: Task SDK has only Variable.get(key) and no supported key listing API.
    all_keys = ???  # keys unknown ahead of time; no supported way to discover them

    vars_snapshot = {k: Variable.get(k) for k in all_keys}

    PythonOperator(
        task_id="use_vars",
        python_callable=use_vars_snapshot,
        op_kwargs={"vars_snapshot": vars_snapshot},
    )


demo_pythonoperator_pass_all_vars()

Workarounds tried
Public REST API v2 GET /api/v2/variables:

  • introduces a network dependency and pagination logic

Proposed solution
Provide a supported API to list Variable keys and/or bulk retrieval of values

Environment
Apache Airflow 3.1.0

Related issues

No response

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:feature
    needs-triage
    on Jan 28, 2026
  2. boring-cyborg commented on Jan 28, 2026

    @boring-cyborg

    Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

  3. justinpakzad commented on Jan 28, 2026

    @justinpakzad
    Contributor

    I would like to work on this one. Cheers.

  4. jroachgolf84 commented on Jan 29, 2026

    @jroachgolf84
    Collaborator

    This comment is outside the scope of the actual feature request. But I would strongly discourage adding "top-level" code to your DAG (retrieving those Variables when parsing the DAG). This can put a strain on your scheduler/DAG processor, and is not Airflow-best practice.

    https://airflow.apache.org/docs/apache-airflow/stable/best-practices.html#top-level-python-code

  5. jroachgolf84 commented on Jan 29, 2026

    @jroachgolf84
    Collaborator

    However, I do like the idea of being able to retrieve all Variables.

  6. added and removed
    needs-triage
    on Jan 29, 2026
  7. justinpakzad commented on Feb 1, 2026

    @justinpakzad
    Contributor

    Hey, so I've been working on an implementation and got a draft working locally, but there are some architectural decisions I'm uncertain about - for example, whether adding a way to list keys (or keys by prefix) to the Variable class makes sense given it represents a single k/v pair. Happy for someone else to give this a go. I can share what I had so far or help review if useful.

  8. shashbha14 commented on Feb 2, 2026

    @shashbha14
    Contributor

    take

  9. jroachgolf84 commented on Feb 3, 2026

    @jroachgolf84
    Collaborator

    @justinpakzad, can you share a link to the draft PR?

  10. justinpakzad commented on Feb 3, 2026

    @justinpakzad
    Contributor

    @jroachgolf84 , I will open a draft PR as soon as I have some time and link it here so you can have a look. Thanks.

  11. justinpakzad commented on Feb 7, 2026

    @justinpakzad
    Contributor

    @jroachgolf84, I linked a draft PR.

  12. potiuk commented on Mar 2, 2026

    @potiuk
    Member

    @justinpakzad We are unassigning you from this issue as part of our updated assignment policy.

    This is not meant to discourage your contribution — quite the opposite! You are still very welcome to work on this issue and submit a PR for it. Simply comment that you are working on it and open a PR when ready.

    We found that formal assignments were not working well, as they often prevented others from contributing when the assignee was not actively working on the issue.

  13. junyeong0619 commented on Apr 28, 2026

    @junyeong0619
    Contributor

    Hi @jroachgolf84!
    I've implemented the proposed Variable.list() following the Airflow 3 Task SDK architecture.
    I’d love to get your thoughts on the implementation!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions