# PyTorch torch.load weights_only default True safe globals custom class UnpicklingError 2.6

Status: verified-page-ready
Product: PyTorch
Last verified: 2026-06-25
Canonical HTML: https://gitdocs.org/fix/pytorch-torch-load-weights-only-default-true-safe-globals-custom-class-unpicklingerror-2-6
Machine JSON: https://gitdocs.org/api/fixes/pytorch-torch-load-weights-only-default-true-safe-globals-custom-class-unpicklingerror-2-6.json

## Exact Symptom

- WeightsUnpickler error: Unsupported global: GLOBAL __main__.MyModel was not an allowed global by default
- _pickle.UnpicklingError: Weights only load failed

## Diagnosis

The checkpoint contains Python objects, not only tensors. The narrow repair is to allowlist the exact trusted classes or migrate the checkpoint to `state_dict` instead of setting a broad unsafe default.

## Fix

```
from myapp.models import MyModel
from torch.serialization import safe_globals

with safe_globals([MyModel]):
    model = torch.load("model.pt", weights_only=True)
```

## Avoid

- Do not use `weights_only=False` for user-supplied or untrusted checkpoints. That re-enables arbitrary pickle execution.

## Observed Codex Queries

- PyTorch torch.load weights_only default True safe globals custom class UnpicklingError 2.6
- pytorch torch.load weights_only safe_globals documentation
- https://pytorch.org/docs/stable/generated/torch.load.html
- https://docs.pytorch.org/docs/stable/generated/torch.load.html
- 'weights_only' in https://docs.pytorch.org/docs/2.12/generated/torch.load.html

## Sources

- PyTorch 2.6 release: https://pytorch.org/blog/pytorch2-6/
- Official reference opened by Codex: https://pytorch.org/docs/stable/generated/torch.load.html
- Official reference opened by Codex: https://docs.pytorch.org/docs/stable/generated/torch.load.html
- Authoritative source: https://pytorch.org/docs/stable/notes/serialization.html#torch-load-with-weights-only-true
