Fixed: "Cannot create DagRun for DAG … because the dag is not serialized" in Apache Airflow®
I ran a Dag file with python to try the Dag out with dag.test() in Apache Airflow® 3.4.0, and it stopped with Cannot create DagRun for DAG say_hello because the dag is not serialized. The Dag was created only inside the file's if __name__ == "__main__": block, where Airflow's own reading of the file never sees it. Here's how I found that out, and the small change that fixed it.
What I Was Doing
dag.test() runs a whole Dag once, in a single Python process, without the scheduler or any other part of Airflow running. Airflow's debugging guide suggests adding it at the bottom of a Dag file, under if __name__ == "__main__":, and running the file with python; How to Run a Dag in One Python Process with dag.test() in Apache Airflow® shows it step by step. I first got this error that way, while trying out a Dag for a guide to BashOperator. For this post, I made the same mistake again with a smaller Dag, in a fresh Airflow folder: Airflow 3.4.0, its virtual environment active, AIRFLOW_HOME set to the folder, and its database created with airflow db migrate.
The Dag file:
from airflow.sdk import dag, task
@dag(schedule=None)
def say_hello():
@task
def hello():
print("Hello from dag.test()")
hello()
if __name__ == "__main__":
say_hello().test()
say_hellois a Dag with one task,hello, which prints a line.if __name__ == "__main__":runs the code under it only when the file itself is run, as withpython dags/say_hello.py, and not when another program imports the file.say_hello().test()creates the Dag by callingsay_hello(), and runs it once with.test().
I ran the file:
python dags/say_hello.py
What I Expected
The hello task running and printing Hello from dag.test().
What I Got
2026-12-04T13:27:47.972903Z [info ] DAG bundles loaded: dags-folder [airflow.dag_processing.bundles.manager.DagBundlesManager] loc=manager.py:344
2026-12-04T13:27:47.974294Z [info ] Added new DAG bundle dags-folder to the database [airflow.dag_processing.bundles.manager.DagBundlesManager] loc=manager.py:418
2026-12-04T13:27:47.978194Z [info ] Filling up the DagBag from /Users/you/source/dagtest-demo/dags [airflow.dag_processing.dagbag.BundleDagBag] loc=dagbag.py:583
Traceback (most recent call last):
File "/Users/you/source/dagtest-demo/dags/say_hello.py", line 14, in <module>
say_hello().test()
~~~~~~~~~~~~~~~~^^
airflow.sdk.exceptions.AirflowException: Cannot create DagRun for DAG say_hello because the dag is not serialized

The task never ran, and the command ended with exit code 1. Between the say_hello.py lines and the last line, the traceback passes through Airflow's own files, and the log line creating dag run comes before it; I left those out.
What I Checked
What Airflow had stored. Airflow keeps a copy of every Dag in its database. Its Dag Serialization page explains that Airflow "parses the Dag files, serializes them in JSON format and saves them in the Metadata DB", so "serialized" means stored there. airflow dags list lists the Dags stored in the database:
airflow dags list
No data found
The command also printed two FileType is deprecated warnings first, which I left out; Fixed: FileType is deprecated explains them. Airflow had no stored Dag at all, say_hello included.
What the documentation says. The debugging guide lists two things dag.test() needs: "an initialized metadata database (airflow db migrate)", and "a Dag that Airflow can serialize, which means the Dag must be defined in a file inside a configured Dag bundle — by default, your Dags folder." It adds: "dag.test() re-parses the bundle and serializes the Dag for you." My database was there, and my file was in the Dags folder, so the question was why the re-parse didn't find the Dag.
What Airflow's re-parse sees. I added one line near the top of the file, which prints the name Python gives the file's code while it runs:
from airflow.sdk import dag, task
print("__name__ is", __name__)
To be sure no Dag was stored from an earlier run, I ran it again in the same folder, with a freshly created database:
python dags/say_hello.py
__name__ is __main__
2026-12-04T13:30:31.455986Z [info ] Filling up the DagBag from /Users/you/source/dagtest-demo/dags [airflow.dag_processing.dagbag.BundleDagBag] loc=dagbag.py:583
__name__ is unusual_prefix_500767428a4fc1b28aab46308cb02bf0c71970ef_say_hello
airflow.sdk.exceptions.AirflowException: Cannot create DagRun for DAG say_hello because the dag is not serialized

The file ran twice. The first time, python ran it, and __name__ was __main__. The second time, Airflow imported it while filling its DagBag, Airflow's collection of the Dags it finds in the Dags folder, and __name__ was unusual_prefix_…_say_hello. The Python documentation says "When a Python module or package is imported, __name__ is set to the module's name", and __main__ only when the file is run itself. So during Airflow's import, the if __name__ == "__main__": block didn't run, and say_hello() was never called.
airflow dags list --local shows the same thing from the outside. Its help says it "Shows local parsed DAGs and their import errors, ignores content serialized in DB": it reads the Dags folder itself, and for my file it also printed No data found, with no import error.
The code. dag.test() fills a DagBag from each Dag bundle, writes the Dags it found to the database, and stops when the database has a version of the Dag being tested:
for bundle in bundles_to_sync:
if not bundle.is_initialized:
bundle.initialize()
dagbag = BundleDagBag(
dag_folder=bundle.path,
bundle_path=bundle.path,
bundle_name=bundle.name,
)
sync_bag_to_db(dagbag, bundle.name, bundle.version)
if DagVersion.get_version(self.dag_id):
break
My file added no Dag to the DagBag, so nothing was written, and creating the run failed with the error.
Why It Happens
dag.test() creates a real Dag run, and Airflow creates runs only for Dags it has stored in its database. To store the Dag, dag.test() imports every file in the Dags folder the way Airflow's Dag processor does, and keeps the Dags the files create. My file created say_hello only when python ran it, inside the __main__ block, so Airflow's import found no Dag, nothing was stored, and the run couldn't be created.
The Fix
Create the Dag at the top level of the file, and keep only the .test() call inside the __main__ block:
from airflow.sdk import dag, task
@dag(schedule=None)
def say_hello():
@task
def hello():
print("Hello from dag.test()")
hello()
hello_dag = say_hello()
if __name__ == "__main__":
hello_dag.test()
hello_dag = say_hello()creates the Dag every time the file is read, whetherpythonruns it or Airflow imports it, and keeps it inhello_dag.hello_dag.test()runs that Dag once, and only when the file is run withpython.
Then run the file again:
python dags/say_hello.py
After the Fix
2026-12-04T13:30:43.061752Z [info ] Filling up the DagBag from /Users/you/source/dagtest-demo/dags [airflow.dag_processing.dagbag.BundleDagBag] loc=dagbag.py:583
2026-12-04T13:30:43.075938Z [info ] Creating ORM DAG for say_hello [airflow.dag_processing.collection] loc=collection.py:104
2026-12-04T13:30:43.324188Z [info ] [DAG TEST] starting task_id=hello map_index=-1 [airflow.sdk.definitions.dag] loc=dag.py:1540
Hello from dag.test()
2026-12-04T13:30:45.719180Z [info ] [DAG TEST] end task task_id=hello map_index=-1 [airflow.sdk.definitions.dag] loc=dag.py:1613
2026-12-04T13:30:45.720394Z [info ] Marking run <DagRun say_hello @ 2026-12-04 13:30:42.963395+00:00: manual__2026-12-04T13:30:43.055506+00:00, state:running, queued_at: None. run_type: manual> successful [airflow.models.dagrun.DagRun] loc=dagrun.py:1381

This time, Airflow's import found say_hello and stored it (Creating ORM DAG for say_hello), the hello task ran and printed its line, and the run was marked successful. The command ended with exit code 0.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Creating the Dag only inside the __main__ block, as in say_hello().test() | Airflow's import finds no Dag, and dag.test() fails with this error | Create the Dag at the top level, and call .test() on it inside the block |
| Running a Dag file that isn't in the Dags folder | The documentation says such a Dag "is not in any bundle". A copy of the fixed file in another folder, with the Dag ID say_hi, failed with Cannot create DagRun for DAG say_hi because the dag is not serialized | Keep the file in the Dags folder |
| Taking a run without the error as proof that the file is right | If Airflow stored the Dag earlier, dag.test() finds that copy and runs, even with the Dag only inside the __main__ block | Create the Dag at the top level anyway |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Cannot create DagRun for DAG <dag id> because the dag is not serialized | The Dag is created only inside the __main__ block, or the file isn't in the Dags folder | Create the Dag at the top level of a file in the Dags folder |
airflow dags list --local prints No data found for a folder with Dag files | The files create no Dag when Airflow imports them | Create each Dag at the top level of its file |
FAQ
Why did the same file run without the error another time?
Because Airflow had already stored the Dag. After the fixed file had run once, I put the old version back, with say_hello().test() inside the __main__ block and the print line from above, and ran it: it printed both names, then the task ran and the run succeeded. The database still held the say_hello that the earlier run had stored, so the run could be created. That's why I checked the module names on a fresh database.
Does dag.test() need Airflow running?
No. Nothing else of Airflow was running for these runs: dag.test() runs the tasks itself, in one Python process. It needs the database, though, which airflow db migrate creates.
Conclusion
dag.test() stores the Dag by importing the Dags folder, and a Dag created only inside if __name__ == "__main__": doesn't exist during that import, so Airflow 3.4.0 stopped with Cannot create DagRun for DAG say_hello because the dag is not serialized. Creating the Dag at the top level of the file, hello_dag = say_hello(), and calling hello_dag.test() inside the block fixed it: Airflow stored the Dag, and the task ran.
Additional Resources
- Debugging Airflow Dags —
dag.test()and what it needs. - Dag Serialization — how Airflow stores Dags in its database.
__main__— Top-level code environment — when__name__is__main__.
Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.
