{"record":{"id":"b4f60cef2185ec82","repo":"Lightning-AI/pytorch-lightning","slug":"m-format-on-step-on-step-fx-name-fx-config-a","errorCode":null,"errorMessage":"m.format(\"on_step\", on_step, fx_name, fx_config[\"allowed_on_step\"])","messagePattern":"m\\.format\\(\"on_step\", on_step, fx_name, fx_config\\[\"allowed_on_step\"\\]\\)","errorType":"validation","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/logger_connector/fx_validator.py","lineNumber":185,"sourceCode":"    def get_default_logging_levels(\n        cls, fx_name: str, on_step: Optional[bool], on_epoch: Optional[bool]\n    ) -> tuple[bool, bool]:\n        \"\"\"Return default logging levels for given hook.\"\"\"\n        fx_config = cls.functions[fx_name]\n        assert fx_config is not None\n        on_step = fx_config[\"default_on_step\"] if on_step is None else on_step\n        on_epoch = fx_config[\"default_on_epoch\"] if on_epoch is None else on_epoch\n        return on_step, on_epoch\n\n    @classmethod\n    def check_logging_levels(cls, fx_name: str, on_step: bool, on_epoch: bool) -> None:\n        \"\"\"Check if the logging levels are allowed in the given hook.\"\"\"\n        fx_config = cls.functions[fx_name]\n        assert fx_config is not None\n        m = \"You can't `self.log({}={})` inside `{}`, must be one of {}.\"\n        if on_step not in fx_config[\"allowed_on_step\"]:\n            msg = m.format(\"on_step\", on_step, fx_name, fx_config[\"allowed_on_step\"])\n            raise MisconfigurationException(msg)\n\n        if on_epoch not in fx_config[\"allowed_on_epoch\"]:\n            msg = m.format(\"on_epoch\", on_epoch, fx_name, fx_config[\"allowed_on_epoch\"])\n            raise MisconfigurationException(msg)\n\n    @classmethod\n    def check_logging_and_get_default_levels(\n        cls, fx_name: str, on_step: Optional[bool], on_epoch: Optional[bool]\n    ) -> tuple[bool, bool]:\n        \"\"\"Check if the given hook name is allowed to log and return logging levels.\"\"\"\n        cls.check_logging(fx_name)\n        on_step, on_epoch = cls.get_default_logging_levels(fx_name, on_step, on_epoch)\n        cls.check_logging_levels(fx_name, on_step, on_epoch)\n        return on_step, on_epoch\n","sourceCodeStart":167,"sourceCodeEnd":200,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/logger_connector/fx_validator.py#L167-L200","documentation":"Each logging-enabled hook defines allowed_on_step / allowed_on_epoch sets. When self.log(..., on_step=X) is called with a value not permitted for the current hook (e.g. on_step=True in validation_step, whose default machinery is epoch-based), check_logging_levels raises this MisconfigurationException with the allowed values listed in the message.","triggerScenarios":"self.log('x', x, on_step=True) inside validation_step or test_step where allowed_on_step is False; more generally any on_step value outside fx_config['allowed_on_step'] for the active hook.","commonSituations":"Copying a training_step logging line into validation_step and forgetting to flip on_step; setting on_step=True, on_epoch=False for metrics that Lightning only supports as epoch aggregates; predict_step logging.","solutions":["Use the value from the error message's allowed set, e.g. in validation_step: self.log('x', x, on_step=False, on_epoch=True)","Omit on_step/on_epoch to accept the hook's defaults via check_logging_and_get_default_levels","Move step-level logging of that metric into training_step where on_step=True is allowed"],"exampleFix":"# before\ndef validation_step(self, batch, batch_idx):\n    self.log('val_loss', loss, on_step=True)  # not allowed\n\n# after\ndef validation_step(self, batch, batch_idx):\n    self.log('val_loss', loss, on_step=False, on_epoch=True)","handlingStrategy":"validation","validationCode":"from lightning.pytorch.trainer.connectors.logger_connector.fx_validator import _FxValidator\ncfg = _FxValidator.functions[hook_name]\nassert on_step in cfg[\"allowed_on_step\"], f\"allowed: {cfg['allowed_on_step']}\"","typeGuard":"def step_allowed(hook_name: str, on_step: bool) -> bool:\n    from lightning.pytorch.trainer.connectors.logger_connector import fx_validator\n    cfg = fx_validator._FxValidator.functions[hook_name]\n    return cfg is not None and on_step in cfg[\"allowed_on_step\"]","tryCatchPattern":null,"preventionTips":["In validation/test steps always use on_step=False, on_epoch=True","Omit flags to take hook defaults","Keep a cheat sheet: training_step -> on_step True; validation_step -> on_epoch True"],"tags":["pytorch-lightning","logging","on-step","misconfiguration"],"backgroundTag":"invalid-logging-level-combination","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}