Lightning-AI/pytorch-lightning · error · AttributeError

There is no `frame` available while being required.

Error message

There is no `frame` available while being required.

What it means

save_hyperparameters() inspects the caller's stack frame to collect init args. If no frame can be determined (frame argument missing and inspect.currentframe() unavailable or its f_back is None, e.g. restricted environments/interpreters), it raises AttributeError.

Source

Thrown at src/lightning/pytorch/utilities/parsing.py:165

    obj: Any,
    *args: Any,
    ignore: Optional[Union[Sequence[str], str]] = None,
    frame: Optional[types.FrameType] = None,
    given_hparams: Optional[dict[str, Any]] = None,
) -> None:
    """See :meth:`~lightning.pytorch.LightningModule.save_hyperparameters`"""

    if len(args) == 1 and not isinstance(args, str) and not args[0]:
        # args[0] is an empty container
        return

    if not frame:
        current_frame = inspect.currentframe()
        # inspect.currentframe() return type is Optional[types.FrameType]: current_frame.f_back called only if available
        if current_frame:
            frame = current_frame.f_back
    if not isinstance(frame, types.FrameType):
        raise AttributeError("There is no `frame` available while being required.")

    if given_hparams is not None:
        init_args = given_hparams
    elif is_dataclass(obj):
        obj_fields = fields(obj)
        init_args = {f.name: getattr(obj, f.name) for f in obj_fields if f.init}
    else:
        init_args = {}

        from lightning.pytorch.core.mixins import HyperparametersMixin

        for local_args in collect_init_args(frame, [], classes=(HyperparametersMixin,)):
            init_args.update(local_args)

    if ignore is None:
        ignore = []
    elif isinstance(ignore, str):
        ignore = [ignore]

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass the frame explicitly: save_hyperparameters(frame=inspect.currentframe().f_back) from the caller
  2. Call save_hyperparameters() directly inside __init__ of the LightningModule (normal supported path)
  3. If frames are unavailable, pass explicit hparams: save_hyperparameters({'lr': 1e-3})

Example fix

# before
save_hyperparameters()  # in a context without an inspectable frame
# after
import inspect
save_hyperparameters(frame=inspect.currentframe().f_back)
Defensive patterns

Strategy: fallback

Validate before calling

import inspect
frame = inspect.currentframe()
assert frame is not None and frame.f_back is not None

Prevention

When it happens

Trigger: Calling save_hyperparameters() from an environment without frame support (some frozen/embedded interpreters, optimized runtimes), or calling it in a context where the CPython frame chain is unavailable.

Common situations: Exotic runtimes, code executed via exec with custom globals, or environments compiled without frame support.

Related errors


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