Lightning-AI/pytorch-lightning · info · RuntimeError
Unable to determine the source of the trainer.
Error message
Unable to determine the source of the trainer.
What it means
Raised when the litmodels registry integration (litmodels >=0.1.7 installed and trainer._model_registry set) tries to determine which package the Trainer class came from via inspect.getmodule, but the module or its __package__ cannot be resolved. The integration must know whether it is lightning or pytorch_lightning to pick the correct LightningModelCheckpoint vs PytorchLightningModelCheckpoint class.
Source
Thrown at src/lightning/pytorch/trainer/connectors/callback_connector.py:102
self.trainer.callbacks.extend(_load_external_callbacks("lightning.pytorch.callbacks_factory"))
_validate_callbacks_list(self.trainer.callbacks)
# push all model checkpoint callbacks to the end
# it is important that these are the last callbacks to run
self.trainer.callbacks = self._reorder_callbacks(self.trainer.callbacks)
def _configure_checkpoint_callbacks(self, enable_checkpointing: bool) -> None:
if self.trainer.checkpoint_callbacks:
if not enable_checkpointing:
raise MisconfigurationException(
"Trainer was configured with `enable_checkpointing=False`"
" but found `ModelCheckpoint` in callbacks list."
)
elif enable_checkpointing:
if RequirementCache("litmodels >=0.1.7") and self.trainer._model_registry:
trainer_source = inspect.getmodule(self.trainer)
if trainer_source is None or not isinstance(trainer_source.__package__, str):
raise RuntimeError("Unable to determine the source of the trainer.")
# this need to imported based on the actual package lightning/pytorch_lightning
if "pytorch_lightning" in trainer_source.__package__:
from litmodels.integrations.checkpoints import PytorchLightningModelCheckpoint as LitModelCheckpoint
else:
from litmodels.integrations.checkpoints import LightningModelCheckpoint as LitModelCheckpoint
model_checkpoint = LitModelCheckpoint(model_registry=self.trainer._model_registry)
else:
# Defer the litmodels tip until loggers are set up (in _attach_model_callbacks)
self._pending_litmodels_tip = True
model_checkpoint = ModelCheckpoint()
self.trainer.callbacks.append(model_checkpoint)
def _configure_model_summary_callback(self, enable_model_summary: bool) -> None:
if not enable_model_summary:
return
model_summary_cbs = [type(cb) for cb in self.trainer.callbacks if isinstance(cb, ModelSummary)]View on GitHub (pinned to 9fed5c27d2)
Solutions
- Pass the checkpoint callback explicitly, bypassing the auto-detection: from litmodels.integrations.checkpoints import LightningModelCheckpoint and add it to callbacks
- Avoid setting _model_registry when running from dynamic-execution contexts
- Run the training script as a normal imported module instead of exec/REPL
- If packaging with PyInstaller, ensure hidden imports are declared so inspect can resolve modules
Example fix
# before trainer = Trainer(max_epochs=2) # _model_registry set by subclass, module not resolvable # after from litmodels.integrations.checkpoints import LightningModelCheckpoint trainer = Trainer(max_epochs=2, callbacks=[LightningCheckpoint := LightningModelCheckpoint()])
Defensive patterns
Strategy: fallback
Validate before calling
import inspect mod = inspect.getmodule(Trainer) assert mod is not None and isinstance(mod.__package__, str), "Trainer module not resolvable; set callbacks manually"
Prevention
- Avoid _model_registry in dynamic-exec contexts (REPL/exec/notebook %run)
- Pass the litmodels checkpoint callback explicitly to skip introspection
- Test your entrypoint as an imported module, matching production
When it happens
Trigger: Trainer(_model_registry=...) (or a subclass setting it) with litmodels installed, where the Trainer instance is created from a dynamically executed/reloaded module, a REPL, or an environment where inspect cannot map the class to an importable module with a string __package__.
Common situations: Running training from a notebook, jupyter cell with %run, exec'd scripts, frozen/compiled binaries (PyInstaller), or unusual import machinery (custom loaders) that break inspect module resolution.
Related errors
- Filter should be a dictionary, given {filter!r}
- The filter keys {filter.keys() - state} are not present in t
- Expected `fabric.save(filter=...)` for key {k!r} to be a cal
- The provided lr scheduler `{scheduler.__class__.__name__}` i
- Trainer was configured with `enable_checkpointing=False` but
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/9f245ad9526086cd.
Report an issue: GitHub.