Skip to content

Cannot create DagRun for DAG because the dag is not serialized when using Dag.test #60860

Description

@multimeric

Apache Airflow version

3.1.6

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

No response

What happened?

Traceback (most recent call last):
  File "/Users/milton.m/Programming/AirflowDagTest/main.py", line 27, in <module>
    my_dag_instance.test()
  File "/Users/milton.m/Programming/AirflowDagTest/.venv/lib/python3.12/site-packages/airflow/sdk/definitions/dag.py", line 1245, in test
    dr: DagRun = get_or_create_dagrun(
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milton.m/Programming/AirflowDagTest/.venv/lib/python3.12/site-packages/airflow/models/dagrun.py", line 2175, in get_or_create_dagrun
    dr = dag.create_dagrun(
         ^^^^^^^^^^^^^^^^^^
  File "/Users/milton.m/Programming/AirflowDagTest/.venv/lib/python3.12/site-packages/airflow/utils/session.py", line 98, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milton.m/Programming/AirflowDagTest/.venv/lib/python3.12/site-packages/airflow/serialization/serialized_objects.py", line 3342, in create_dagrun
    orm_dagrun = _create_orm_dagrun(
                 ^^^^^^^^^^^^^^^^^^^
  File "/Users/milton.m/Programming/AirflowDagTest/.venv/lib/python3.12/site-packages/airflow/utils/session.py", line 98, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/milton.m/Programming/AirflowDagTest/.venv/lib/python3.12/site-packages/airflow/serialization/serialized_objects.py", line 2379, in _create_orm_dagrun
    raise AirflowException(f"Cannot create DagRun for DAG {dag.dag_id} because the dag is not serialized")
airflow.exceptions.AirflowException: Cannot create DagRun for DAG my_dag because the dag is not serialized

What you think should happen instead?

Dag.test should appropriately serialize the DAG to prevent this issue. I can actually see some logic for doing this already in the code, but it doesn't seem to work:

scheduler_dag = SerializedDAG.deserialize_dag(SerializedDAG.serialize_dag(self))

How to reproduce

Here's the minimal example I can come up with:

import tempfile
import os
import pendulum

with tempfile.NamedTemporaryFile(suffix=".sqlite3", delete=False) as tmpfile:
    conn_str = f"sqlite:///{tmpfile.name}"
    os.environ["AIRFLOW__DATABASE__SQL_ALCHEMY_CONN"] = conn_str
    from airflow.utils.db import upgradedb

    upgradedb()

    from airflow.sdk import task, dag

    @dag(start_date=pendulum.datetime(2024, 1, 1))
    def my_dag():
        @task
        def hello_task():
            print("Hello, Airflow!")

        hello_task()

    my_dag_instance = my_dag()

    my_dag_instance.test()

Operating System

MacOS 15.6

Versions of Apache Airflow Providers

None are relevant

Deployment

Virtualenv installation

Deployment details

A simple pyproject.toml with:

[project]
name = "airflowdagtest"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "apache-airflow>=3.1.6",

Anything else?

The Dag.test code seems to be changed in the current main branch, but installing commit 1e2e29d doesn't solve the issue.

This does work fine in airflow 3.0.6, and is broken in 3.1.2, so this bug was introduced relatively recently. This also makes me think that it wasn't deliberate.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

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