airflow icon indicating copy to clipboard operation
airflow copied to clipboard

merge_dicts raises AttributeError when dict2's value is a dict but dict1's value for the same key is not

Open Sanjays2402 opened this issue 2 months ago • 6 comments

airflow.utils.helpers.merge_dicts documents: "Items in dict2 overwrite those also found in dict1." But when a key exists in both dicts and dict2's value is a dict while dict1's value is a scalar, it recurses unconditionally:

from airflow.utils.helpers import merge_dicts
merge_dicts({'a': 1}, {'a': {'b': 2}})
# AttributeError: 'int' object has no attribute 'copy'

Line 177 checks isinstance(v, dict) but never checks that merged[k] is also a dict before recursing into merge_dicts(merged.get(k, {}), v), which calls dict1.copy() on the scalar.

Expected: {'a': {'b': 2}} (dict2's value wins, per the docstring).

This is reachable in practice: airflow/utils/log/json_formatter.py:57 calls merge_dicts(record_dict, self.extras), so a user-configured log extra that collides with a scalar log-record attribute under the same name crashes log formatting.

Seen on main at 8dff0303 (2026-09-25).

Sanjays2402 avatar Sep 25 '26 09:09 Sanjays2402

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.

boring-cyborg[bot] avatar Sep 25 '26 09:09 boring-cyborg[bot]

I'd like to work on this. I'll also cover the case where dict1's value is None, which fails the same way.

ccoliu avatar Sep 25 '26 10:09 ccoliu

Hi @Sanjays2402, Can I work on this issue ?

@SYED-IRTEZA-HUSSAIN-RIZVI Sure, go ahead. I'm not a maintainer here so I can't assign it to you, could you check with one of the Airflow maintainers to get it assigned?

Sanjays2402 avatar Nov 13 '26 21:11 Sanjays2402

Hi @kaxil, I looked into the history of this issue and wanted to check which direction you'd prefer before opening anything.

The bug

merge_dicts (airflow/utils/helpers.py) recurses when dict2's value is a dict without checking that dict1's value is also a dict. It is still unchanged on main:

if k in merged and isinstance(v, dict):
    merged[k] = merge_dicts(merged.get(k, {}), v)

History

Three PRs made the same change and none was merged:

  • #70349: opened 2026-07-24, no reviews, closed by the stale bot on 2026-11-04.
  • #70457: opened 2026-07-26, parametrized test over str/list/None/int, closed on 2026-10-01 in favor of #70349.
  • #73718: opened 2026-09-25, closed on 2026-10-01 since the other two already had the same guard.

The change: recurse only when both values are dicts, otherwise let dict2's value overwrite, as the docstring says ("Items in dict2 overwrite those also found in dict1"). No review comment on any of them objected to it, but #70349 was closed without a review.

What I checked

I ran every caller of merge_dicts in the repo against both main and the change (calling the real methods, 7-10 scenarios each):

  • json_formatter, ECS executor, Batch executor, HTTP operator, Vault hook, ODBC hook: every case that raised AttributeError/TypeError before (a dict over a str/int/bool/None/list) now overwrites. Normal dict merges, new keys, scalar over scalar, and a dict replaced by a scalar or None give identical results.
  • gcs_to_bigquery, bigquery_to_gcs and the common SQL operator merge OpenLineage facet objects, not dicts (checked with the real classes and get_facets_from_bq_table), so results are identical before and after.
  • The existing tests of each caller give the same results with and without the change (the failures I saw locally are identical on main).

One behavior change beyond the crash fix, and why I think it is reasonable

When dict2's value is an empty dict {} and dict1's value is a list, main silently kept the list; with the change the list is overwritten by {}:

  • ECS exec_config containerOverrides: {}: previously ignored, now KeyError: No such container found ....
  • Batch exec_config containerOverrides.environment: {}: previously ignored, now AttributeError: 'dict' object has no attribute 'append'.
  • HTTP pagination data (nested): {"a": [1]} with {"a": {}} previously stayed [1], now becomes {}.

Why I think this is the right behavior:

  1. The old result was accidental. For an empty dict, the recursive call loops zero times and returns list.copy(); for a non-empty dict over the same list it raises TypeError. So main ignored {} but crashed on {"a": 1}, and {} over a str/int/None also raised. Nothing in the code or docs describes "ignore an empty dict".
  2. It matches what is documented. The docstring says dict2 overwrites; the ECS docs describe exec_config as run_task_kwargs.update(exec_config) applied recursively; HttpOperator._merge_next_page_parameters says items in next_page_params override the previous request.
  3. It is consistent with the other direction. A scalar over a dict already overwrites on main and in the tests of the three PRs, so every type mismatch now behaves the same way.
  4. Every case requires passing {} where a list is expected, i.e. an invalid value for that field. For ECS the task then fails with an explicit error (the executor catches it, retries, and marks the task failed with the reason) instead of silently running with defaults that ignore what the user configured.

The trade-offs: the Batch error message is not very clear, and the HTTP case changes the request payload without an error. I did not change them here; any validation of those fields would belong in the executors/operator, not in this helper. I also found no use of MappingProxyType in airflow-core, task-sdk or the providers above (a non-dict Mapping on the left is merged by main but overwritten with the change).

Would you prefer reopening #70349, or is a fresh PR with the same guard and a parametrized test (without a newsfragment) fine?


Drafted-by: Claude Code (Sonnet 5.5); reviewed by @xchwan before posting

xchwan avatar Nov 19 '26 21:11 xchwan

Closing as #70349 was merged

shahar1 avatar Dec 02 '26 06:12 shahar1