Skip to content

Conversation

@amoghrajesh
Copy link
Contributor

closes: #53018

The data returned from the api comes in without deserialisation as the contract between task sdk and the api server is simple: json objects.

So the returned data must be deserialised.

DAG used:

from airflow.sdk import DAG
from airflow.providers.standard.operators.python import PythonOperator

from datetime import datetime, timedelta

twos_day = datetime.now() - timedelta(days=2)

def native_obj_false(x, y):
    return x, y


def check_type(**context):
    ti = context["ti"]
    pulled_value = ti.xcom_pull(
        task_ids="py_native_obj_false", key="return_value"
    )
    print(f"This is the datatype for the pulled xcom value: {type(pulled_value)}")
    print(f"This is datatype for the 0th index of the xcom value: {type(pulled_value[0])}")
    print(f"This is datatype for the 1st index of the xcom value: {type(pulled_value[0])}")
    assert type(pulled_value[0]) == str and type(pulled_value[1]) == str


default_args = {"owner": "airflow", "depends_on_past": True}

with DAG(
    dag_id="native_obj_false",
    start_date=twos_day,
    schedule=None,
    # when set to false only the __str__() method is returned
    render_template_as_native_obj=False,
    tags=["core", "negative"],
    user_defined_macros={
        "says_hi": "Hello World!",
        "the_planets": [
            "Mercury",
            "Venus",
            "Earth",
            "Mars",
            "Jupiter",
            "Saturn",
            "Neptune",
        ],
        "ints": [1, 2, 3, 4, 5],
    },
) as dag:

    t1 = PythonOperator(
        task_id="py_native_obj_false",
        python_callable=native_obj_false,
        op_args=["{{ says_hi }}", "{{ ints }}"],
    )

    t2 = PythonOperator(
        task_id="check_xcoms",
        python_callable=check_type,
    )

t1 >> t2

Before:
image

After changes, dag run:
image


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@eladkal
Copy link
Contributor

eladkal commented Jul 8, 2025

Should we have a test for it?

@amoghrajesh
Copy link
Contributor Author

Should we have a test for it?

yep, was just finishing it off.

@amoghrajesh amoghrajesh requested a review from ashb July 8, 2025 15:16
@amoghrajesh amoghrajesh changed the title Deserialize response of get_all for multiple xcoms Deserialize response of get_all when we call XCom.get_all Jul 8, 2025
@vatsrahul1001
Copy link
Contributor

Verified re-running failed Dags works fine now

@amoghrajesh amoghrajesh self-assigned this Jul 9, 2025
@vatsrahul1001 vatsrahul1001 added the backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch label Jul 9, 2025
Copy link
Member

@kaxil kaxil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code change looks good to me. Test works but can be improved later on

https://github.com/apache/airflow/pull/53020/files#r2194788728

@amoghrajesh
Copy link
Contributor Author

@kaxil please merge this if all's good :)

@amoghrajesh amoghrajesh merged commit bdc9cd1 into apache:main Jul 9, 2025
76 checks passed
@amoghrajesh amoghrajesh deleted the quick-fix-for-xcom branch July 9, 2025 18:54
github-actions bot pushed a commit that referenced this pull request Jul 9, 2025
…all` (#53020)

(cherry picked from commit bdc9cd1)

Co-authored-by: Amogh Desai <amoghrajesh1999@gmail.com>
@github-actions
Copy link

github-actions bot commented Jul 9, 2025

Backport successfully created: v3-0-test

Status Branch Result
v3-0-test PR Link

kaxil pushed a commit that referenced this pull request Jul 9, 2025
…all` (#53020) (#53102)

(cherry picked from commit bdc9cd1)

Co-authored-by: Amogh Desai <amoghrajesh1999@gmail.com>
kaxil pushed a commit that referenced this pull request Jul 9, 2025
…all` (#53020) (#53102)

(cherry picked from commit bdc9cd1)

Co-authored-by: Amogh Desai <amoghrajesh1999@gmail.com>
HsiuChuanHsu pushed a commit to HsiuChuanHsu/airflow that referenced this pull request Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XCom pull is failing due to KeyError

5 participants