7

I was reading the API Docs, and was unclear what the context argument is for BaseOperator.xcom_pull.

I thought it would be dag.default_args, but I receive KeyError: 'ti'

I performed an xcom_push within a prior task following the push() example here.

1 Answer 1

13

The context is a set of keyword arguments containing reference objects related to a task instance, such as dag, dag_run, run_id, execution_date, etc. (including the task instance ti itself).

The default context is generated when task instance runs, and is defined here.

In the example you mentioned, the way context is passed in isn't super obvious: if the provide_context arg is set to True, Airflow will pass the generated context to the python callable. You can then access the task instance's xcom_pull method by calling kwargs['ti'].xcom_pull().

So that's why for this to work, you will need to define **kwargs in your python callable's header, and set the operator's provide_context arg to True.

I am not sure how to directly get hold of the context reference though.

Sign up to request clarification or add additional context in comments.

1 Comment

the official documents about xcom and provide_context is really confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.