Skip to content

DAG fails to be parsed while trying to fetch a Variable that doesn't exist #49543

Closed as not planned

Description

@tatiana

Apache Airflow version

main (development)

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

No response

What happened?

In Airflow 2.x, the following DAG used to work (when specifying other mandatory fields):

import json

from airflow import DAG
from airflow.operators.empty import EmptyOperator
from airflow.models import Variable

try:
    variable_value = Variable.get("some-variable", deserialize_json=True)
except (json.decoder.JSONDecodeError, KeyError):
    variable_value = {"task_id": "default_task_name"}


with DAG(dag_id="example_dag_variable_access") as dag:
    task_from_var = EmptyOperator(task_id=variable_value.get("task_id"))
    task_from_var

In Airflow 3 (commit dda86263e8a9c171d91729f14668028408568022), if we try to parse it using:

airflow dag-processor

It fails and logs:

================================================================================
DAG File Processing Stats

Bundle       File Path                       PID    Current Duration      # DAGs    # Errors  Last Duration    Last Run At
-----------  ------------------------------  -----  ------------------  --------  ----------  ---------------  -------------------
dags-folder  example_dag_variable_access.py                                    0           1  0.62s            2025-04-22T11:02:54
================================================================================
[2025-04-22T12:03:25.160+0100] {manager.py:523} INFO - Not time to refresh bundle dags-folder
[2025-04-22T12:03:25.338+0100] {_client.py:1025} INFO - HTTP Request: GET http://in-process.invalid./variables/some-variable "HTTP/1.1 404 Not Found"
2025-04-22 12:03:25 [warning  ] Server error                   [airflow.sdk.api.client] detail={'detail': {'reason': 'not_found', 'message': "Variable with key 'some-variable' not found"}}
2025-04-22 12:03:25 [error    ] Variable not found             [airflow.sdk.api.client] detail={'detail': {'reason': 'not_found', 'message': "Variable with key 'some-variable' not found"}} key=some-variable status_code=404

Changing the DAG to capture ServerResponseError doesn't work either, and I observe the same issue:

import json

from airflow import DAG
from airflow.operators.empty import EmptyOperator
from airflow.models import Variable
from airflow.sdk.api.client import ServerResponseError

try:
    variable_value = Variable.get("some-variable", deserialize_json=True)
except (json.decoder.JSONDecodeError, KeyError, ServerResponseError):
    variable_value = {"task_id": "default_task_name"}


with DAG(dag_id="example_dag_variable_access") as dag:
    task_from_var = EmptyOperator(task_id=variable_value.get("task_id"))
    task_from_var

It is interesting that the DAG can be serialized if I run the following command, without issues:

airflow dags reserialize

What you think should happen instead?

Airflow should continue to allow DAGS to retrieve Variables and allow DAG authors to capture the exception if a variable does not exist, without raising errors during DAG parsing and failing to render the DAG.

I'd expect the behaviour seen in airflow dags reserialize to be consistent with what happens when we run airflow dag-processor.

Finally, Airflow should not log "Server Error" and error-level messages because a variable was not found.

How to reproduce

Described previously

Operating System

Tested in MacOS 15.4

Versions of Apache Airflow Providers

N/A

Deployment

Other

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. added
    kind:bug
    needs-triage
    on Apr 22, 2025
  2. tatiana commented on Apr 22, 2025

    @tatiana
    ContributorAuthor

    After speaking to @ashb and @amoghrajesh , I was able to get the DAG to work by capturing airflow.sdk.exceptions.AirflowRuntimeError:

    import json
    
    from airflow import DAG
    from airflow.operators.empty import EmptyOperator
    from airflow.models import Variable
    from airflow.sdk.exceptions import AirflowRuntimeError
    
    try:
        variable_value = Variable.get("some-variable", deserialize_json=True)
    except (json.decoder.JSONDecodeError, KeyError, AirflowRuntimeError):
        variable_value = {"task_id": "default_task_name"}
    
    
    with DAG(dag_id="example_dag_variable_access") as dag:
        task_from_var = EmptyOperator(task_id=variable_value.get("task_id"))
        task_from_var
    

    Airflow still logs [warning ] Server error and [error ] Variable not found - while applications / DAG authors can often intentionally try to fetch a variable that does not exist:

    dag-processor | 2025-04-22 12:25:50 [warning ] Server error          [airflow.sdk.api.client] detail={'detail': {'reason': 'not_found', 'message': "Variable with key 'some-variable' not found"}}
    
    dag-processor | 2025-04-22 12:25:50 [error  ] Variable not found       [airflow.sdk.api.client] detail={'detail': {'reason': 'not_found', 'message': "Variable with key 'some-variable' not found"}} key=some-variable status_code=404
    

    @amoghrajesh agreed logs can be improved.

    It is also not clear to me why the behaviour is inconsistent when running airflow dags reserialize versus airflow dag-processor - I'd expect either for both of them to fail or work with the original DAG.

  3. changed the title [-]DAG processor unable to parse DAG that retrieves Variable[/-] [+]Error & Warning logs when DAG attempts to fetch a Variable that doesn't exist[/+] on Apr 22, 2025
  4. changed the title [-]Error & Warning logs when DAG attempts to fetch a Variable that doesn't exist[/-] [+]DAG fails to be parsed while trying to fetch a Variable that doesn't exist[/+] on Apr 22, 2025
  5. kaxil commented on Apr 24, 2025

    @kaxil
    Member

    @tatiana Curious why not use the following i.e. set a default:

    variable_value = Variable.get("some-variable", default={"task_id": "default_task_name"}, deserialize_json=True)

    instead of

    try:
        variable_value = Variable.get("some-variable", deserialize_json=True)
    except (json.decoder.JSONDecodeError, KeyError):
        variable_value = {"task_id": "default_task_name"}
  6. tatiana commented on Apr 28, 2025

    @tatiana
    ContributorAuthor

    @kaxil, unfortunately not.

    To explain the problem, I created a simple DAG that illustrates the original issue that I found when porting Cosmos to Airflow 3. In the real case, we are not interested in defining a default value for the variable.

    The use case we're covering was initially implemented in astronomer/astronomer-cosmos#1014. If the variable exists, we'll leverage it to build the DAG dynamically. Otherwise, we'll compute the DAG topology using other means.

  7. SergeyZ06 commented on Jul 3, 2025

    @SergeyZ06

    The same issue I have:
    airflow.sdk.exceptions.AirflowRuntimeError: VARIABLE_NOT_FOUND

  8. amoghrajesh commented on Jul 4, 2025

    @amoghrajesh
    Contributor

    @SergeyZ06 can you explain what you are trying in your dag?

  9. SergeyZ06 commented on Jul 4, 2025

    @SergeyZ06

    @SergeyZ06 can you explain what you are trying in your dag?

    @amoghrajesh hello!

    I have some variable in my Airflow UI Variables page. And I got this error while trying to access variable value via my Python code:
    from airflow.sdk import Variable
    my_awesome_variable = Variable.get("MY_AWESOME_VARIABLE")

    BTW I'm using Airflow 3.0.2

  10. github-actions commented on Jul 5, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 365 days without any activity. There has been several Airflow releases since last activity on this issue. Kindly asking to recheck the report against latest Airflow version and let us know if the issue is reproducible. The issue will be closed in next 30 days if no further activity occurs from the issue author.

  11. github-actions commented on Aug 5, 2026

    @github-actions
    Contributor

    This issue has been closed because it has not received response from the issue author.

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

    Stale Bug Reportarea:corekind:bugneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions