Lightning-AI/pytorch-lightning · error · TypeError

`name` must be a str, found {name}

Error message

`name` must be a str, found {name}

What it means

StrategyRegistry.register validates that the registered strategy's `name` is a string (or None). Passing any non-str name (int, tuple, etc.) raises this TypeError immediately, before any registration occurs.

Source

Thrown at src/lightning/fabric/strategies/registry.py:63

        self,
        name: str,
        strategy: Optional[Callable] = None,
        description: Optional[str] = None,
        override: bool = False,
        **init_params: Any,
    ) -> Callable:
        """Registers a strategy mapped to a name and with required metadata.

        Args:
            name : the name that identifies a strategy, e.g. "deepspeed_stage_3"
            strategy : strategy class
            description : strategy description
            override : overrides the registered strategy, if True
            init_params: parameters to initialize the strategy

        """
        if not (name is None or isinstance(name, str)):
            raise TypeError(f"`name` must be a str, found {name}")

        if name in self and not override:
            raise ValueError(f"'{name}' is already present in the registry. HINT: Use `override=True`.")

        data: dict[str, Any] = {}
        data["description"] = description if description is not None else ""

        data["init_params"] = init_params

        def do_register(strategy: Callable) -> Callable:
            data["strategy"] = strategy
            data["strategy_name"] = name
            self[name] = data
            return strategy

        if strategy is not None:
            return do_register(strategy)

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass a plain string name: register('my_strategy', MyStrategy)
  2. Coerce: register(str(name), ...) if the name comes from dynamic input

Example fix

# before
register(name=123, strategy=MyStrategy)
# after
register(name='my_strategy', strategy=MyStrategy)
Defensive patterns

Strategy: type-guard

Validate before calling

assert name is None or isinstance(name, str), f'name must be str, got {type(name)}'

Type guard

def is_valid_registry_name(name) -> bool:
    return name is None or isinstance(name, str)

Prevention

When it happens

Trigger: Calling StrategyRegistry.register(name=123, strategy=...) or any non-string name; a custom plugin/extension registering strategies computed names of the wrong type.

Common situations: Third-party extensions or user scripts registering custom strategies with typos like name=('ddp', 'custom') or variables that evaluate to non-strings.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/16b8ff6e9ef25fbf. Report an issue: GitHub.