open-mmlab/mmdetection · error · TypeError

type must be a str or valid type, but got {type(obj_type)}

Error message

type must be a str or valid type, but got {type(obj_type)}

What it means

Albu.albu_builder requires each transform entry's 'type' to be either a string (albumentations transform name) or a class. Any other type (int, dict, list, None) raises TypeError.

Source

Thrown at mmdet/datasets/transforms/transforms.py:1665

        Args:
            cfg (dict): Config dict. It should at least contain the key "type".

        Returns:
            obj: The constructed object.
        """

        assert isinstance(cfg, dict) and 'type' in cfg
        args = cfg.copy()
        obj_type = args.pop('type')
        if is_str(obj_type):
            if albumentations is None:
                raise RuntimeError('albumentations is not installed')
            obj_cls = getattr(albumentations, obj_type)
        elif inspect.isclass(obj_type):
            obj_cls = obj_type
        else:
            raise TypeError(
                f'type must be a str or valid type, but got {type(obj_type)}')

        if 'transforms' in args:
            args['transforms'] = [
                self.albu_builder(transform)
                for transform in args['transforms']
            ]

        return obj_cls(**args)

    @staticmethod
    def mapper(d: dict, keymap: dict) -> dict:
        """Dictionary mapper. Renames keys according to keymap provided.

        Args:
            d (dict): old dict
            keymap (dict): {'old_key':'new_key'}
        Returns:

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Set 'type' to a valid albumentations transform name string, e.g. 'HorizontalFlip'
  2. Or pass the actual class object: dict(type=albumentations.HorizontalFlip)
  3. Check YAML quoting of the type value

Example fix

# before
dict(type=Albu, transforms=[dict(type=dict(name='Blur'), p=0.1)])
# after
dict(type='Albu', transforms=[dict(type='Blur', p=0.1)])
Defensive patterns

Strategy: type-guard

Validate before calling

for t in albu_cfg['transforms']:
    assert isinstance(t['type'], str) or inspect.isclass(t['type']), f"bad type: {t['type']!r}"

Type guard

import inspect
def valid_albu_type(v):
    return isinstance(v, str) or inspect.isclass(v)

Try / catch

try:
    Albu(transforms=cfg)
except TypeError as e:
    if 'type must be a str' in str(e):
        fix_and_retry()

Prevention

When it happens

Trigger: Writing dict(type=3), type=None, type=[...], or a nested dict in the 'type' field of an entry inside Albu transforms or keymap-driven builder input.

Common situations: Hand-editing Albu configs and putting a dict/list into 'type'; YAML quoting issues turning a name into another structure.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/fc3cdcbff5775345. Report an issue: GitHub.