Repository navigation
Description
Apache Airflow version
Other Airflow 2 version (please specify below)
What happened
I'm using version composer-2.1.15-airflow-2.5.1 using composer-dev to run it.
Using a MappedArgument with GCSToBigQueryOperator returns an error: ` TypeError: Object of type MappedArgument is not JSON serializable
I opened a discussion here: #31452, but I wonder if it might actually be a bug.
What you think should happen instead
It should work as with a regular XCOM Argument.
How to reproduce
Disclaimer, i'm relatively new to airflow 2 and taskflow. I'm trying to migrate a codebase written with airflow 1, so there might be some glaring problems with how I'm addressing the problem.
The Issue i'm having is with the schema_fields property passed to GCSToBigQueryOperator which is a MappedArgument instead of being resolve to a list of dicts like I expected.
As a first step, the DAG loads the metadata from GCS with a task named
get_export_metadata that returns a dict with the shape {"databases":Dict[str, List[str]], "schemas": Dict[str, List[Dict[str, str]]]} (multiple_outputs)
example:
{
"databases": {
"test": [
"table_1",
"table_2"
]
},
"schemas": {
"table_1": [
{
"mode": "NULLABLE",
"name": "id",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "creation_date",
"type": "STRING"
},
]
}
}Here's the task defined for my DAG
@task
def get_table_names(database_map, db_name)
return database_map[db_name]
@task
def build_args(schema_map, table_name, instance_name, instance_pool, db_name):
return {
"instance_name": instance_name,
"instance_pool": instance_pool,
"schema_fields": schema_map[table_name],
"db_name": db_name,
"table_name": table_name,
}
@task_group
def extract_data_csv_dump(table_name, instance_name, db_name, instance_pool, schema_fields):
@task
def get_export_query(table_name: str) -> str:
return f"SELECT * FROM {table_name};"
@task
def get_bq_dump_table(db_name, table_name):
return f"{settings.GCP_PROJECT_ID}.{db_name}.{table_name}"
@task
def get_dump_object_path(db_name, table_name):
dump_object_name = f"{table_name}__airflow_dump*"
return (
f"{settings.REPORTING_PATH}/{GCS_SYNC_FOLDER}/{db_name}/{dump_object_name}"
)
export_query = get_export_query(table_name)
bq_dump_table_name = get_bq_dump_table(db_name, table_name)
dump_object_path = get_dump_object_path(db_name, table_name)
load_dump_task = GCSToBigQueryOperator(
bucket=settings.AIRFLOW_GCS_BUCKET,
source_objects=[dump_object_path],
schema_fields=schema_fields, <<< This is where it fails
destination_project_dataset_table=bq_dump_table_name,
write_disposition="WRITE_TRUNCATE",
source_format="CSV",
allow_quoted_newlines=True,
task_id="load_csv_dump",
pool=settings.BIG_QUERY_TASK_POOL,
)
db_data = { "conn_id": "cloudsql_mws",
"connection_pool": "my_connection",
"instance_pool": "my_pool",
}
instance_name="my_instance_name"
db_name="my_db_name"
export_metadata = get_export_metadata()
table_names = get_table_names(export_metadata["databases"], db_name)
instance_pool = db_data["instance_pool"]
kw_args_list = build_args.partial(
schema_map=export_metadata["schemas"],
instance_name=instance_name,
instance_pool=instance_pool,
db_name=db_name
).expand(table_name=table_names)
extract_data_csv_dump.expand_kwargs(kw_args_list)Operating System
Composer dev image (linux I assume)
Versions of Apache Airflow Providers
apache-airflow-providers-apache-beam @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_apache_beam-4.3.0-py3-none-any.whl
apache-airflow-providers-cncf-kubernetes @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_cncf_kubernetes-6.0.0-py3-none-any.whl
apache-airflow-providers-common-sql @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_common_sql-1.4.0-py3-none-any.whl
apache-airflow-providers-dbt-cloud @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_dbt_cloud-3.1.0-py3-none-any.whl
apache-airflow-providers-ftp @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_ftp-3.3.1-py3-none-any.whl
apache-airflow-providers-google @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_google-2023.4.13%2Bcomposer-py3-none-any.whl
apache-airflow-providers-hashicorp @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_hashicorp-3.3.1-py3-none-any.whl
apache-airflow-providers-http @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_http-4.3.0-py3-none-any.whl
apache-airflow-providers-imap @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_imap-3.1.1-py3-none-any.whl
apache-airflow-providers-mysql @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_mysql-4.0.2-py3-none-any.whl
apache-airflow-providers-postgres @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_postgres-5.4.0-py3-none-any.whl
apache-airflow-providers-sendgrid @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_sendgrid-3.1.0-py3-none-any.whl
apache-airflow-providers-sqlite @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_sqlite-3.3.1-py3-none-any.whl
apache-airflow-providers-ssh @ file:///usr/local/lib/airflow-pypi-dependencies-2.5.1/python3.8/apache_airflow_providers_ssh-3.6.0-py3-none-any.whl
Deployment
Google Cloud Composer
Deployment details
This is running locally and I use the dag.test() command to execute it
Anything else
N/A
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
boring-cyborg commentedon May 23, 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.
CharchitSaraswat commentedon May 29, 2023
I am new to airflow since it is a good first issue, I would like to work on it.
potiuk commentedon May 29, 2023
Assigned you.
quantumlicht commentedon May 30, 2023
The issue with this specifically is that
schema_fieldsis not part of the template_fields for that operator. I was able to work around my issue usingschema_object, but that's annoying because I need to save my schema to GCS, when I already have it in memory.I have another issue with branching in task groups with mapped arguments, but I'll open a separate issue.
I still think this one is worth fixing if possible.
CharchitSaraswat commentedon May 30, 2023
Sure @quantumlicht. I am on it. Just to be super-clear and to know if I understood the issue correctly, the issue you are facing is that the schema_fields here is a
MappedArgumentbut having it in{"databases":Dict[str, List[str]], "schemas": Dict[str, List[Dict[str, str]]]}would be convenient and easy to use?quantumlicht commentedon May 30, 2023
No the issue is that I cannot use my schema _fields variable in the GCSToBigQuery operator, because that field is not templated in that operator. At least that's what I think the problem is.
CharchitSaraswat commentedon May 31, 2023
I am working on the issue. Did code fixes, got a free tier of google cloud and added credentials for bigquery, have run the tests. Not trying to check if the issue still exists.
CharchitSaraswat commentedon May 31, 2023
I added the schema_fields to template_fields of the operator. I ran the tests for providers/google/cloud/test_gcs_to_bigquery.py with and without schema_fields in template_fields. The tests run GCSToBigQueryOperator with schema_fields as list of dicts. The tests pass in both cases i.e. with and without schema_fields in template_fields. I do not think that is the issue here @quantumlicht @potiuk.
MichailKaramanos commentedon Nov 15, 2023
Anyone looking into this?
Seem's a bit annoying not to be able to use task group parameters into classic operators. The majority are classic ones, so it would be important to use this pattern on them for obvious reasons…
This is not related to templated fields as stated early, but happens with custom operators and the providers ones (Google, Databricks, etc).
arsunny commentedon Dec 20, 2023
Do we have any update on this issue?
potiuk commentedon Dec 20, 2023
@arsunny -> evidently not and it waits for someone to pick it up. Would you llke to take on the task ?
Locustv2 commentedon May 31, 2024
Hey guys, so i encountered a similar issue with another Operator, and it seem that the issue is with the
expandpart.So when we are creating a dynamic task mapping on another operator directly, it works...
Example:
So in this example, we have:
my_datathat just produce some dataall_my_taskswith 2 printing tasksmy_taskwhich is a taskflow functionmy_operatorwhich is a subclass ofBaseOperatormy_taskworks just fine and is able to print an item from the list of datamy_operatorhowever prints an object of typeMappedArgumentHowever if we do like this:
The above works and
my_operatoris able to print item from the list of data.So the issue is when an instance of
BaseOperatoris used in task_group which has been created using dynamic task mapping..This is my issue: #39927
nigarcia-univision commentedon Jan 31, 2025
Hello there,
Are there any updates on this issue? I am facing the same issue, I had my schema defined in memory and using TaskFlow I returned it, but when I try to pass it to the GCSToBigQueryOperator in the schema_fields parameter I get the following error:
TypeError: 'XComArg' object is not iterableAs someone mentioned before, the schema_fields is not part of the template_fields of the operator, or is this error rooted more deeply?
Thanks.
VladaZakharova commentedon Apr 9, 2025
Hi there!
Thanks for raising the issue
did you try to run the same on the latest Composer/Airflow version to run it?
VladaZakharova commentedon Oct 17, 2025
@CharchitSaraswat Hi there, did you have a chance to check this issue?