This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push: new ca755fbcac0 Explain f-string conflicts with Jinja templating (#48712) ca755fbcac0 is described below commit ca755fbcac055bb8bcd567873282d0dce848adc7 Author: Kacper Kulczak <17738154+kkulc...@users.noreply.github.com> AuthorDate: Tue Apr 8 16:31:25 2025 +0200 Explain f-string conflicts with Jinja templating (#48712) * Explain f-string conflicts with Jinja templating * Fixed spaces * Update operators.rst. - removed traling spaces * Update operators.rst Doubled back ticks --------- Co-authored-by: Kacper <> --- airflow-core/docs/core-concepts/operators.rst | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/airflow-core/docs/core-concepts/operators.rst b/airflow-core/docs/core-concepts/operators.rst index 524a43e04a5..0d66ba3f61d 100644 --- a/airflow-core/docs/core-concepts/operators.rst +++ b/airflow-core/docs/core-concepts/operators.rst @@ -304,3 +304,30 @@ If you upgrade your environment and get the following error: AttributeError: 'str' object has no attribute '__module__' change name from ``params`` in your operators. + +Templating Conflicts with f-strings +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +When constructing strings for templated fields (like ``bash_command`` in ``BashOperator``) using Python f-strings, be mindful of the interaction between f-string interpolation and Jinja templating syntax. Both use curly braces (``{}``). + +Python f-strings interpret double curly braces (``{{`` and ``}}``) as escape sequences for literal single braces (``{`` and ``}``). However, Jinja uses double curly braces (``{{ variable }}``) to denote variables for templating. + +If you need to include a Jinja template expression (e.g., ``{{ ds }}``) literally within a string defined using an f-string, so that Airflow's Jinja engine can process it later, you must escape the braces for the f-string by doubling them *again*. This means using **four** curly braces: + +.. code-block:: python + + t1 = BashOperator( + task_id="fstring_templating_correct", + bash_command=f"echo Data interval start: {{{{ ds }}}}", + dag=dag, + ) + + python_var = "echo Data interval start:" + + t2 = BashOperator( + task_id="fstring_templating_simple", + bash_command=f"{python_var} {{{{ ds }}}}", + dag=dag, + ) + +This ensures the f-string processing results in a string containing the literal double braces required by Jinja, which Airflow can then template correctly before execution. Failure to do this is a common issue for beginners and can lead to errors during DAG parsing or unexpected behavior at runtime when the templating does not occur as expected.