{"record":{"id":"d8fc4a27566ddc4d","repo":"Lightning-AI/pytorch-lightning","slug":"there-is-no-frame-available-while-being-required","errorCode":null,"errorMessage":"There is no `frame` available while being required.","messagePattern":"There is no `frame` available while being required\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/parsing.py","lineNumber":165,"sourceCode":"    obj: Any,\n    *args: Any,\n    ignore: Optional[Union[Sequence[str], str]] = None,\n    frame: Optional[types.FrameType] = None,\n    given_hparams: Optional[dict[str, Any]] = None,\n) -> None:\n    \"\"\"See :meth:`~lightning.pytorch.LightningModule.save_hyperparameters`\"\"\"\n\n    if len(args) == 1 and not isinstance(args, str) and not args[0]:\n        # args[0] is an empty container\n        return\n\n    if not frame:\n        current_frame = inspect.currentframe()\n        # inspect.currentframe() return type is Optional[types.FrameType]: current_frame.f_back called only if available\n        if current_frame:\n            frame = current_frame.f_back\n    if not isinstance(frame, types.FrameType):\n        raise AttributeError(\"There is no `frame` available while being required.\")\n\n    if given_hparams is not None:\n        init_args = given_hparams\n    elif is_dataclass(obj):\n        obj_fields = fields(obj)\n        init_args = {f.name: getattr(obj, f.name) for f in obj_fields if f.init}\n    else:\n        init_args = {}\n\n        from lightning.pytorch.core.mixins import HyperparametersMixin\n\n        for local_args in collect_init_args(frame, [], classes=(HyperparametersMixin,)):\n            init_args.update(local_args)\n\n    if ignore is None:\n        ignore = []\n    elif isinstance(ignore, str):\n        ignore = [ignore]","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/parsing.py#L147-L183","documentation":"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.","triggerScenarios":"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.","commonSituations":"Exotic runtimes, code executed via exec with custom globals, or environments compiled without frame support.","solutions":["Pass the frame explicitly: save_hyperparameters(frame=inspect.currentframe().f_back) from the caller","Call save_hyperparameters() directly inside __init__ of the LightningModule (normal supported path)","If frames are unavailable, pass explicit hparams: save_hyperparameters({'lr': 1e-3})"],"exampleFix":"# before\nsave_hyperparameters()  # in a context without an inspectable frame\n# after\nimport inspect\nsave_hyperparameters(frame=inspect.currentframe().f_back)","handlingStrategy":"fallback","validationCode":"import inspect\nframe = inspect.currentframe()\nassert frame is not None and frame.f_back is not None","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call save_hyperparameters() as the first line of __init__","Pass frame explicitly when calling from helper utilities"],"tags":["hyperparameters","introspection","lightning","attributeerror"],"backgroundTag":"stack-frame-unavailable","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}