huggingface/transformers · error · KeyError

Key {key} not found in SizeDict.

Error message

Key {key} not found in SizeDict.

What it means

Error "Key {key} not found in SizeDict." thrown in huggingface/transformers.

Source

Thrown at src/transformers/image_utils.py:1028


@dataclass()
class SizeDict:
    """
    Hashable dictionary to store image size information.
    """

    height: int | None = None
    width: int | None = None
    longest_edge: int | None = None
    shortest_edge: int | None = None
    max_height: int | None = None
    max_width: int | None = None

    def __getitem__(self, key):
        if hasattr(self, key):
            return getattr(self, key)
        raise KeyError(f"Key {key} not found in SizeDict.")

    def get(self, key, default=None):
        if hasattr(self, key) and getattr(self, key) is not None:
            return getattr(self, key)
        return default

    def __iter__(self):
        # Yield only non-None (key, value) pairs so dict(self) excludes missing values.
        for f in fields(self):
            val = getattr(self, f.name)
            if val is not None:
                yield f.name, val

    def __hash__(self):
        return hash((self.height, self.width, self.longest_edge, self.shortest_edge, self.max_height, self.max_width))

    def __contains__(self, key):
        return hasattr(self, key) and getattr(self, key) is not None

View on GitHub (pinned to a597f97485)

Solutions

  1. Access only keys present in the SizeDict (e.g. via `size_dict['height']` after checking).
  2. Normalize the size input with `convert_to_size_dict` so required keys exist.

When it happens

Trigger: Raised in SizeDict attribute/item access when the requested key does not exist.

Common situations: Accessing a misspelled or unsupported key such as size_dict['short_edge'] on a SizeDict.


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/3a7d47e69c83c608. Report an issue: GitHub.