sameerpandey17 commented on issue #64649:
URL: https://github.com/apache/airflow/issues/64649#issuecomment-6140389736

   Still reproducible on main as of today. I also hit it with a notifier that 
defines `template_fields`, so it doesn't look limited to notifiers with none.
   
   I tested on main at `7f0d50cffc` (Breeze, Python 3.11.16). After fetching, 
`git diff HEAD upstream/main` shows no differences in `notifier.py` or 
`templater.py`, so current main should behave the same.
   
   Setup is a Dag with a `BaseNotifier` subclass as `on_failure_callback`, run 
with `airflow dags test <dag_id> 2026-01-01`. The task fails as expected, then 
the callback crashes:
   
   ```
   File "task-sdk/src/airflow/sdk/bases/notifier.py", line 92, in 
render_template_fields
       jinja_env = self.get_template_env(dag=dag)
   File "task-sdk/src/airflow/sdk/definitions/_internal/templater.py", line 80, 
in get_template_env
       return dag.get_template_env(force_sandboxed=False)
   AttributeError: 'SerializedDAG' object has no attribute 'get_template_env'
   ```
   
   I got the same error with a bare-bones notifier and with this one that has a 
templated field:
   
   <details>
   <summary>Dag with a templated notifier</summary>
   
   ```python
   from __future__ import annotations
   
   from datetime import datetime
   
   from airflow.sdk import DAG, task
   from airflow.sdk.bases.notifier import BaseNotifier
   
   
   class TemplatedNotifier(BaseNotifier):
       template_fields = ("message",)
   
       def __init__(self, message: str):
           super().__init__()
           self.message = message
   
       def notify(self, context):
           self.log.info("Rendered message: %s", self.message)
   
   
   with DAG(
       dag_id="templated_notifier_test",
       start_date=datetime(2026, 1, 1),
       schedule=None,
       catchup=False,
       on_failure_callback=TemplatedNotifier(message="Dag {{ dag.dag_id }} 
failed"),
   ) as dag:
   
       @task
       def failing_task():
           raise Exception("designed to fail")
   
       failing_task()
   ```
   
   </details>
   
   As an experiment I changed `Templater.get_template_env` to `if dag and 
hasattr(dag, "get_template_env"):` so it falls back to the default 
`SandboxedEnvironment`. With that, both Dags ran their callbacks without 
crashing, and the templated message rendered as `Dag templated_notifier_test 
failed`. I reverted it afterwards.
   
   One thing I didn't test is Dag-level custom filters or macros 
(`user_defined_macros`). I'd expect the fallback environment to be missing 
those, but that's just my reading of the code, not something I ran.
    
   I noticed #64993 and #67862 both take this approach (#67862 was closed 
because of the open-PR limit, not on its merits). I hope this reproduction 
helps. If #64993 has stalled and the maintainers are fine with it, I’d be glad 
to pick this up.
   
   (I also used AI in the process of understanding and testing)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to