Lightning-AI/pytorch-lightning · error · ValueError

Invalid seed specified via PL_GLOBAL_SEED: {repr(env_seed)}

Error message

Invalid seed specified via PL_GLOBAL_SEED: {repr(env_seed)}

What it means

`seed_everything` reads the `PL_GLOBAL_SEED` environment variable to restore a previously set seed (e.g. when `reset_seed()` is called in spawned workers). The value must be parseable as an integer; if `int(env_seed)` raises ValueError, Lightning re-raises with this message showing the invalid string.

Source

Thrown at src/lightning/fabric/utilities/seed.py:49

            not in bounds or cannot be cast to int, a ValueError is raised.
        workers: if set to ``True``, will properly configure all dataloaders passed to the
            Trainer with a ``worker_init_fn``. If the user already provides such a function
            for their dataloaders, setting this argument will have no influence. See also:
            :func:`~lightning.fabric.utilities.seed.pl_worker_init_function`.
        verbose: Whether to print a message on each rank with the seed being set.

    """
    if seed is None:
        env_seed = os.environ.get("PL_GLOBAL_SEED")
        if env_seed is None:
            seed = 0
            if verbose:
                rank_zero_warn(f"No seed found, seed set to {seed}")
        else:
            try:
                seed = int(env_seed)
            except ValueError:
                raise ValueError(f"Invalid seed specified via PL_GLOBAL_SEED: {repr(env_seed)}")
    elif not isinstance(seed, int):
        seed = int(seed)

    if not (min_seed_value <= seed <= max_seed_value):
        raise ValueError(f"{seed} is not in bounds, numpy accepts from {min_seed_value} to {max_seed_value}")

    if verbose:
        log.info(rank_prefixed_message(f"Seed set to {seed}", _get_rank()))

    os.environ["PL_GLOBAL_SEED"] = str(seed)
    random.seed(seed)
    if _NUMPY_AVAILABLE:
        import numpy as np

        np.random.seed(seed)
    torch.manual_seed(seed)

    os.environ["PL_SEED_WORKERS"] = f"{int(workers)}"

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Unset or fix the env var: `PL_GLOBAL_SEED=42` (integer only) or `unset PL_GLOBAL_SEED`
  2. Check launch scripts/CI for anything that writes PL_GLOBAL_SEED from an empty or non-integer source
  3. Let `seed_everything(seed)` set the var itself instead of setting it manually

Example fix

# before
PL_GLOBAL_SEED=$MY_SEED  # MY_SEED empty -> ""
python train.py

# after
PL_GLOBAL_SEED=${MY_SEED:-42}
python train.py
Defensive patterns

Strategy: validation

Validate before calling

env_seed = os.environ.get("PL_GLOBAL_SEED")
if env_seed is not None:
    try:
        int(env_seed)
    except ValueError:
        del os.environ["PL_GLOBAL_SEED"]  # or fix it

Prevention

When it happens

Trigger: Setting `PL_GLOBAL_SEED` to a non-numeric value (e.g. `PL_GLOBAL_SEED=random`, `PL_GLOBAL_SEED=1.5`, or an empty/whitespace string), then calling `seed_everything(...)` or anything that calls `reset_seed()` like Lightning's spawned dataloader workers.

Common situations: CI pipelines or launcher scripts exporting PL_GLOBAL_SEED from another variable that is empty; external tools writing float or arbitrary strings into the env var; copy-paste typos in launch commands.

Related errors


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