PaddlePaddle/PaddleOCR · warning · Exception

You cannot use ``setdefault`` on a {self.__class__.__name__}

Error message

You cannot use ``setdefault`` on a {self.__class__.__name__} instance.

What it means

ModelOutput blocks setdefault because it could insert keys that are not declared dataclass fields, breaking the invariant that the dict view mirrors the typed attributes. The call always raises.

Source

Thrown at ppocr/modeling/heads/rec_unimernet_head.py:111

                        break
                    setattr(self, element[0], element[1])
                    if element[1] is not None:
                        self[element[0]] = element[1]
            elif first_field is not None:
                self[class_fields[0].name] = first_field
        else:
            for field in class_fields:
                v = getattr(self, field.name)
                if v is not None:
                    self[field.name] = v

    def __delitem__(self, *args, **kwargs):
        raise Exception(
            f"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."
        )

    def setdefault(self, *args, **kwargs):
        raise Exception(
            f"You cannot use ``setdefault`` on a {self.__class__.__name__} instance."
        )

    def pop(self, *args, **kwargs):
        raise Exception(
            f"You cannot use ``pop`` on a {self.__class__.__name__} instance."
        )

    def update(self, *args, **kwargs):
        raise Exception(
            f"You cannot use ``update`` on a {self.__class__.__name__} instance."
        )

    def __getitem__(self, k):
        if isinstance(k, str):
            inner_dict = dict(self.items())
            return inner_dict[k]
        else:

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Use explicit conditional assignment on attributes: if output.key is None: output.key = default
  2. Use output[k] if k in output else default when reading
  3. Convert to a plain dict before running dict-oriented utilities

Example fix

# before
out.setdefault('loss', 0.0)
# after
if out.loss is None:
    out.loss = 0.0  # or construct a new ModelOutput with loss=0.0
Defensive patterns

Strategy: type-guard

Validate before calling

def safe_get(output, key, default=None):
    return output[key] if key in output else default
# use safe_get(out, 'loss', 0.0) instead of out.setdefault('loss', 0.0)

Type guard

def is_model_output(obj) -> bool:
    return hasattr(obj, '__dataclass_fields__') and hasattr(obj, 'to_tuple')

Try / catch

null  # design guard; use conditional attribute assignment instead

Prevention

When it happens

Trigger: Calling output.setdefault('key', default) on a ModelOutput returned by the UniMERNet head, e.g. in generic code that normalizes dict-like results.

Common situations: Shared post-processing helpers that call setdefault on anything dict-like; merging outputs from multiple models; caching wrappers that ensure a key exists before use.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/5777858c50e69d01. Report an issue: GitHub.