Skip to content

Spacingd crashes when the data dict has a non-dict key starting with '<key>_' (regression in 1.6.0) #9165

Closed

Description

@garciadias

Describe the bug

Since #8269 (in 1.6.0), Spacingd.__call__ updates every dictionary entry whose key starts with f"{key}_", assuming each one is a metadata dict:

meta_keys = [k for k in d.keys() if k is not None and k.startswith(f"{key}_")]
for meta_key in meta_keys:
    if "filename_or_obj" in d[key].meta and is_supported_format(...):
        d[meta_key].update(d[key].meta)

Any other value that happens to share the prefix (for example a string image_path, or an int image_index) makes the transform raise AttributeError: 'str' object has no attribute 'update' for NIfTI inputs.

This breaks MONAI Label in practice. Its inference pipeline always sets data["image_path"], and its radiology app uses Spacingd("image", ...), so NIfTI inference fails on MONAI 1.6.x.

To Reproduce

import numpy as np, tempfile, nibabel as nib
from monai.transforms import Compose, LoadImaged, EnsureChannelFirstd, Spacingd

f = tempfile.NamedTemporaryFile(suffix=".nii.gz").name
nib.save(nib.Nifti1Image(np.random.rand(16, 16, 8).astype(np.float32), np.eye(4)), f)
t = Compose([LoadImaged("image"), EnsureChannelFirstd("image"), Spacingd("image", pixdim=(2.0, 2.0, 2.0))])
t({"image": f, "image_path": f})
  • MONAI 1.5.1: OK, output shape (1, 8, 8, 4)
  • MONAI 1.6.1 and current dev: RuntimeError: applying transform Spacingd ... caused by AttributeError: 'str' object has no attribute 'update'

Expected behavior

Only metadata dictionaries should be updated. Entries that are not mappings should be left untouched.

Suggested fix

Only update entries that are mappings, e.g. if isinstance(d[meta_key], MutableMapping) and .... I'm happy to open a PR.

Environment

MONAI 1.6.1 (also reproduced on dev), Python 3.12, torch 2.x CPU.

Activity

  1. caitlin-leonard commented on Oct 13, 2026

    @caitlin-leonard
    Contributor

    Hi @garciadias , thanks for the detailed report. I saw you offered to open a PR; are you still planning to? If not, I’d be happy to take it. One extra thing I noticed: k.startswith(...) would also fail if the dict has a non-string key, so the fix could check isinstance(k, str) as well as isinstance(d[k], dict).

  2. added a commit that references this issue on Oct 13, 2026
    e74ef7d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions