You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow usage of Jinja templating inside task param #31801
Say my_sql_script.sql is a script that let you copy data from S3 bucket to Postgres. It's pretty common and we would like to reuse it here and there. But in our case, we get the s3_file_name from the xcom of the previous task, so to get that filename we need to use {{ ti.xcom_pull(task_ids='my_previous_task') }} and we would like to use it in params that way :
But it's not possible, we thus need to hard-code {{ ti.xcom_pull(task_ids='my_previous_task') }} inside my_sql_script.sql script.
But later in the code, we don't want s3_file_name to come from xcom but rather from a python variable, and it's impossible because it's hard coded in the sql template. We have to create an other sql file with the exact same code except for s3_file_name.
Related issues
This issue is related to the problem : #7910
It has been marked as resolved but the 2 PRs associated are closed
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
It's a good feature! If you truly need it and you want to become an Airflow contributor, I encourage you to take the initiative and implement it yourself. Otherwise, you'll have to wait for someone else to pick up the issue and implement it.
select
*,
date('{logical_date}')
from toto_{something}
@raphaelauv Great! Didn't know about it, but is weird we need to use {logical_date}, and not {{logical_date}}. Why is that? I thought Jinja worked all time with double curly brackets
Hi, I'd like to work on this. My proposed approach: extend template rendering so that string values inside the task params dict are rendered with the task context (the same Jinja environment used for template_fields), e.g. in BaseOperator.render_template_fields / the template preparation path, walking params values recursively for dicts/lists. This would allow params={"s3_file_name": "{{ ti.xcom_pull(task_ids='my_previous_task') }}"} as requested.
One design question before I start: should params rendering be always-on, or opt-in per task (to avoid breaking existing Dags whose params contain literal {{ }} text)? Happy to follow maintainer preference and add tests either way.
Activity
boring-cyborg commentedon Jun 8, 2023
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
hussein-awala commentedon Jun 8, 2023
It's a good feature! If you truly need it and you want to become an Airflow contributor, I encourage you to take the initiative and implement it yourself. Otherwise, you'll have to wait for someone else to pick up the issue and implement it.
elephantmetropolis commentedon Jun 8, 2023
Would love to do it !
hussein-awala commentedon Jun 8, 2023
Great! I assigned you
raphaelauv commentedon Jun 8, 2023
same than #18812
another missing templating are the mapped parameters of Dynamic Task Mapping tasks -> #29660 (comment)
Bowrna commentedon Jun 11, 2023
could i try adding templating for the mapped parameters of Dynamic Task mapping?
raphaelauv commentedon Jul 24, 2023
@elephantmetropolis
hack to use templating on templated values
sql file
toto.sqlin same path than the dag filegive
raphaelauv commentedon Nov 19, 2024
since airlfow 2.10.0
you can do
sql file toto.sql in same path than the dag file
iAlevato commentedon Jan 8, 2025
To parse
paramsattribute, I've been taking the following approach.render_template_fieldsto parse theparamsattribute in the context.I've been using it for quite a while, no issues so far. This is an example with PostgresOperator.
set92 commentedon Feb 4, 2025
@raphaelauv Great! Didn't know about it, but is weird we need to use {logical_date}, and not {{logical_date}}. Why is that? I thought Jinja worked all time with double curly brackets
raphaelauv commentedon Feb 4, 2025
@set92 the logical_date is "templated" with a
formatnot by jinja, so my templating "pipeline" is{logical_date} -> format -> {{ds}} -> jinja -> 2024-01-03
--
WHY ?
cause I don't want to put airflow jinja macro in my sql ( separation of concern )
dasarijashwanth commentedon Nov 27, 2026
Hi, I'd like to work on this. My proposed approach: extend template rendering so that string values inside the task
paramsdict are rendered with the task context (the same Jinja environment used fortemplate_fields), e.g. inBaseOperator.render_template_fields/ the template preparation path, walkingparamsvalues recursively for dicts/lists. This would allowparams={"s3_file_name": "{{ ti.xcom_pull(task_ids='my_previous_task') }}"}as requested.One design question before I start: should
paramsrendering be always-on, or opt-in per task (to avoid breaking existing Dags whose params contain literal{{ }}text)? Happy to follow maintainer preference and add tests either way.