Skip to content

Adding DAG bundle_name to AirflowParsingContext - #77552

Open
jroachgolf84 wants to merge 7 commits into
apache:mainfrom
jroachgolf84:feature/add-bundle-name-to-parsing-context
Open

jroachgolf84 wants to merge 7 commits into
apache:mainfrom
jroachgolf84:feature/add-bundle-name-to-parsing-context

Conversation

@jroachgolf84

@jroachgolf84 jroachgolf84 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Description

DAG code running at parse time can call get_parsing_context() to read dag_id and task_id, but it has no way to find out which DAG bundle it is being parsed from.

This PR adds a bundle_name property to AirflowParsingContext. BundleDagBag sets it before parsing, so it is available on every path that parses a file from a bundle: the Dag processor (including callbacks), a task re-parsing its DAG file before running, and CLI commands such as airflow dags test and airflow dags reserialize. It is None when a file is not loaded from a bundle.

Like dag_id and task_id, the value stays set after parsing, so it is also available while the task runs. Because bundle_name is a property rather than a new tuple field, AirflowParsingContext keeps its shape and existing code that unpacks it positionally (dag_id, task_id = get_parsing_context()) keeps working.

Example Use Case

from airflow.sdk import Asset, AssetWatcher, get_parsing_context

bundle_name = get_parsing_context().bundle_name

asset = Asset(
    "my_asset",
    watchers=[
        AssetWatcher(
            name="my_watcher",
            trigger=MyEventTrigger(bundle_name=bundle_name),  # Pass in the bundle name to be used, as needed
        )
    ],
)

Testing

These changes were tested via unit tests added to the following files:

  • airflow-core/tests/unit/dag_processing/test_dagbag.py: BundleDagBag sets the bundle name for Dag code to read, and clears a value left over from an earlier parse when no bundle name is given.
  • task-sdk/tests/task_sdk/definitions/test_context.py: bundle_name reads the current value, and the context still unpacks into dag_id, task_id.
Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: [Claude Opus 5.5] following the guidelines

… parsing context to it can be used outside of get_current_context
@jroachgolf84 jroachgolf84 changed the title Adding DAG bundle_name to Parsing Context Adding DAG bundle_name to AirflowParsingContext Nov 24, 2026
@jroachgolf84

Copy link
Copy Markdown
Collaborator Author

cc: @jedcunningham

@amoghrajesh amoghrajesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you set the context inside BundleDagBag, and expose bundle_name as a property instead of a tuple field? That fixes the backcompat issue and the issue where it is None on cli path

@jroachgolf84

Copy link
Copy Markdown
Collaborator Author

@amoghrajesh, I went ahead and implemented your feedback, should be all set to re-review.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants