How to pass a value from Xcom to another operator? - airflow

DockerOperator has a parameter xcom_push which when set, pushes the output of the Docker container to Xcom:
t1 = DockerOperator(task_id='run-hello-world-container',
image='hello-world',
xcom_push=True, xcom_all=True,
dag=dag)
In the admin interface under Xcom, I can see these values with key return_value. However, how can I access them in the DAG?
If I try:
t1_email_output = EmailOperator(task_id='t1_email_output',
to='user#example.com',
subject='Airflow sent you an email!',
html_content={{ ti.xcom_pull(task_ids='return_value') }},
dag=dag)
I get Broken DAG: [PATH] name 'ti' is not defined.
If I try:
t1_email_output = EmailOperator(task_id='t1_email_output',
to='user#example.com',
subject='Airflow sent you an email!',
html_content=t1.xcom_pull(task_ids='return_value'),
dag=dag)
I get Broken DAG: [PATH] xcom_pull() missing 1 required positional argument: 'context'.

You need to pass the task id from which you are pulling the xcom and not the variable name
In your example it would be
{{ ti.xcom_pull('run-hello-world-container') }}
Also in the second snippet it should be "ti" instead of "t1"
html_content=ti.xcom_pull('run-hello-world-container'),

I found the problem - turns out I was missing a quote and my parameter was also wrong:
t1_email_output = EmailOperator(task_id='t1_email_output',
to='user#example.com',
subject='Airflow sent you an email!',
html_content="{{ ti.xcom_pull(key='return_value') }}",
dag=dag)
Sends an email with the Docker container's output like I expect.
I think what is happening is that the {{ }} syntax gets processed as a Jinja template by Airflow when the DAG is run, but not when it is loaded. So if I don't put the quotes around it, Airflow gets Python exceptions when it tries to detect and load the DAG, because the template hasn't been rendered yet. But if the quotes are added, the templated expression is treated as a string, and ignored by Python interpreter when being loaded by Airflow. However when the EmailOperator is actually triggered during a DAG run, the template is rendered into actual references to the relevant data.

Related

Vertex AI Airflow Operators don't render XCom pulls (specifically CreateBatchPredictionJobOperator)

I am trying to run a batch predict job task using the Vertex AI Airflow Operator CreateBatchPredictionJobOperator. This requires pulling a model id from XCom which was pushed by a previous custom container training job. However, CreateBatchPredictionJobOperator doesn't seem to render Xcom pulls as expected.
I am running Airflow 2.3.0 on my local machine.
My code looks something like this:
batch_job_task = CreateBatchPredictionJobOperator(
gcp_conn_id="gcp_connection",
task_id="batch_job_task",
job_display_name=JOB_DISPLAY_NAME,
model_name="{{ ti.xcom_pull(key='model_conf')['model_id'] }}",
predictions_format="bigquery",
bigquery_source=BIGQUERY_SOURCE,
region=REGION,
project_id=PROJECT_ID,
machine_type="n1-standard-2",
bigquery_destination_prefix=BIGQUERY_DESTINATION_PREFIX,
This results in a value error when the task runs:
ValueError: Resource {{ ti.xcom_pull(key='model_conf')['model_id'] }} is not a valid resource id.
The expected behaviour would be to pull that variable by key and render it as a string.
I can also confirm that I am able to see the model id (and other info) in XCom by navigating there in the UI. I attempted using the same syntax with xcom_pull with a PythonOperator and it works.
def print_xcom_value(value):
print("VALUE:", value)
print_xcom_value_by_key = PythonOperator(
task_id="print_xcom_value_by_key", python_callable=print_xcom_value,
op_kwargs={"value": "{{ ti.xcom_pull(key='model_conf')['model_id'] }}" },
provide_context=True,
)
> [2022-12-15, 13:11:19 UTC] {logging_mixin.py:115} INFO - VALUE: 3673414612827265024
CreateBatchPredictionJobOperator does not accept provide_context as a variable. I assumed it would render xcom pulls by default since xcom pulls are used in the CreateBatchPredictionJobOperator in an example on the Airflow docs (link here).
Is there any way I can provide context to this Vertex AI Operator to pull from the XCom storage?
Is something wrong with the syntax that I am not seeing? Anything I a misunderstanding in the docs?
UPDATE:
One thing that confuses me is that model_name is a templated field according to the Airflow docs (link here) but the field is not rendering the XCom template.
Did you set render_template_as_native_obj=True in your DAG definition?
What version of apache-airflow-providers-google do you use?
====
From OP:
Your answer was a step in the right direction.
The solution was to upgrade apache-airflow-providers-google to the latest version (at the moment, this is 8.6.0). I'm not able to pinpoint exactly where in the changelog this fix is mentioned.
Setting render_template_as_native_obj=True was not useful for this issue since it rendered the id pulled from XCom as an int, and I found no proper way to convert it to str when passed into CreateBatchPredictionJobOperator in the model_name arg.

Error "'Proxy object' has no attribute ..." when rendering Airflow variable `prev_execution_date_success`

During the rendering of a template that uses the Airflow variable prev_execution_date_success, like this:
rendered_and_formatted = "{{ prev_execution_date_success.strftime('%Y%m%d') }}"
... I get the error:
jinja2.exceptions.UndefinedError: 'Proxy object' has no attribute 'strftime'
As prev_execution_date_success variable represents the "execution date from prior successful dag run", your error will happen when no "prior successful dag run" exists yet, for example when it is the first run ever of your DAG.

How to set a default dag trigger configuration json

When triggering a dag in airflow, there is a window, with which I am able to parameters to the dag in a json format. This looks like the following:
This json is always empty and I do have to know which parameters I can pass to the dag. Instead I would like to be able to prefill this json, so that when another user tries to trigger the dag he can simply change to values of the json, instead of having to look at the dags code first.
Is there any way to do this in the current version (2.0.0) of airflow?
On Airflow 2.1.0 it is possible to set default arguments as follow:
dag = DAG(dag_id="my_dag",
schedule_interval=None,
default_args={'retries': 3, 'retry_delay': timedelta(seconds=20)},
catchup=False,
tags=['maintenance'],
params={"description": ""} #Set parameters as a dictionary
)
In the thrigger UI it looks like this:
When writing my feature request i actually found a pull request, which is already merged and seems to exactly do as described:
https://github.com/apache/airflow/pull/10839
An improvement of the this feature also seems to be planned. See:
https://github.com/apache/airflow/issues/11054
No, it is currently not supported -- at least for Airflow 2.0.0

Can Airflow Macros be used with the CloudSqlInstanceExportOperator?

We're using Airflow to schedule daily database exports using the CloudSqlInstanceExportOperator. This doesn't appear to work with Airflow Macros. We are trying to export 1 day of data using the execution date macro or {{ ds }} in the where clause. It's important to use the macro because we want our DAG to backfill.
The sample code is made of of two parts. First we define the export context:
export_body = {
"exportContext": {
"fileType": "csv",
"uri": "gs://"+GCP_BUCKET+'/'data.csv',
"databases":["database"],
"csvExportOptions": {
"selectQuery": """
select * from table
where datetime BETWEEN "{{ ds }} 00:00:00"
AND "{{ ds }} 23:59:59
"""
}
}
}
Next, pass the export context to the task:
cloudsql_export_task = CloudSqlInstanceExportOperator(
project_id=PROJECT_ID,
body = export_body,
instance='instance',
task_id='cloudsql_export_task',
dag=dag)
The task runs and get's marked as a success, however, the Google Cloud Storage file created has no data in it. When we hard code the date, the query works as expected. As a result, we know the problem is being caused by the macro value not being populated.
Any suggestions would be appreciated. Either how to fix this task or an alternative way to achieve the same objective (note: query is large and uses too much memory for MySqlToGoogleCloudStorageOperator to work)
Make sure the operator includes body in template_fields.
You can also use Jinja templating with nested fields, as long as these
nested fields are marked as templated in the structure they belong to:
fields registered in template_fields property will be submitted to
template substitution
More info about templating: https://airflow.readthedocs.io/en/stable/concepts.html#jinja-templating
You can extend the operator like the following
class CloudSqlInstanceExportTemplatedOperator(CloudSqlInstanceExportOperator):
template_fields = CloudSqlInstanceExportOperator.template_fields + ('body',)
shankshera answer is correct however you are using deprecated operator. In the updated version there is no need for the suggested modification.
The CloudSqlInstanceExportOperator was renamed to CloudSQLExportInstanceOperator and moved to providers.
For Airflow <2.0 you will need to install backport providers :
pip install apache-airflow-backport-providers-google
For Airflow >=2.0 you will need to install providers:
pip install apache-airflow-providers-google
The you can import the operator as:
from airflow.providers.google.cloud.operators.cloud_sql import CloudSQLExportInstanceOperator
Since the operator already has body listed in the templated fields you are good to go.

Using XCom with a BashOperator: Pushing multi-line sdout output to other tasks?

According to the documentation, "If xcom_push is True, the last line written to stdout will also be pushed to an XCom when the bash command completes."
However, I need to return a multi-line string that results from a python script being executed from the terminal. I would like to subsequently use this string in an EmailOperator.
So my question is: is it possible to push more than the last line via xcom_push? Ideally, it would be arbitrarily long. I would really appreciate your help, thanks!
EDIT: I have gotten around this problem by using a PythonOperator and calling the script, but I'm still curious if it's possible to push multi-line data to XCom from a BashOperator
As clearly stated in the source code, only the last line of the BashOperator is being pushed if xcom_push = True.
:param xcom_push: If xcom_push is True, the last line written to stdout
will also be pushed to an XCom when the bash command completes.
However, you could easily create a custom operator inheriting from the BashOperator and implement the double xcom_push.
See the plugins doc on how to build custom operators with Airflow plugins.

Resources