nicolargo/glances · error · KeyError

'{self.__class__.__name__}' object has no key '{item}'

Error message

'{self.__class__.__name__}' object has no key '{item}'

What it means

KeyError raised by the plugin stats model's __getitem__ when code indexes a plugin instance with a key that is neither a key of its dict stats nor findable in list_to_dict(self.stats) for list-shaped stats. It is Glances' dict-style access protocol over the PluginModel in glances/plugins/plugin/model.py.

Source

Thrown at glances/plugins/plugin/model.py:165

        return str(self.stats)

    def __repr__(self):
        """Return the raw stats."""
        if isinstance(self.stats, list):
            return str(list_to_dict(self.stats))
        return str(self.stats)

    def __getitem__(self, item):
        """Return the stats item."""
        if isinstance(self.stats, dict) and item in self.stats:
            return self.stats[item]

        if isinstance(self.stats, list):
            ltd = list_to_dict(self.stats)
            if item in ltd:
                return ltd[item]

        raise KeyError(f"'{self.__class__.__name__}' object has no key '{item}'")

    def keys(self):
        """Return the keys of the stats."""
        if isinstance(self.stats, dict):
            return listkeys(self.stats)
        if isinstance(self.stats, list):
            return listkeys(list_to_dict(self.stats))
        return []

    def get(self, item, default=None):
        """Return the stats item or default if not found."""
        try:
            return self[item]
        except KeyError:
            return default

    def get_init_value(self):
        """Return a copy of the init value."""

View on GitHub (pinned to a240d8dfb3)

Solutions

  1. Inspect plugin.stats (or the REST API JSON for that plugin) to see actual available keys
  2. Guard with 'key in plugin' — the model implements __contains__ via keys()/dict protocol — before indexing
  3. Handle KeyError explicitly and skip/report the missing metric instead of crashing
  4. Pin/align the Glances version whose stat key names your code expects

Example fix

# before
val = plugin['user']
# after
val = plugin['user'] if 'user' in plugin else None
Defensive patterns

Strategy: type-guard

Validate before calling

keys = plugin.keys() if hasattr(plugin, 'keys') else []
value = plugin[item] if item in keys else None

Type guard

def stat_key_exists(plugin, key: str) -> bool:
    keys = plugin.keys() if callable(getattr(plugin, 'keys', None)) else []
    return key in keys

Try / catch

try:
    val = plugin[item]
except KeyError:
    val = None  # metric absent in this plugin/version

Prevention

When it happens

Trigger: plugin['nonexistent'] on a plugin whose stats dict lacks that key, or a key that doesn't match any item of the list form (list_to_dict maps list entries by their 'key'-like fields).

Common situations: Exporters or scripts assuming a field exists (e.g. stats['system'] on a plugin that only exposes 'user'/'iowait'); version changes renaming stat keys; iterating a plugin whose stats are still an empty list at startup.

Related errors


AI-assisted analysis of nicolargo/glances@a240d8dfb3 (2026-08-27). Data as JSON: /api/errors/1e895dda431d4156. Report an issue: GitHub.