{"record":{"id":"dc3564412f52670c","repo":"Lightning-AI/pytorch-lightning","slug":"num-nodes-must-be-a-positive-integer-but-got-n","errorCode":null,"errorMessage":"`num_nodes` must be a positive integer, but got {num_nodes}.","messagePattern":"`num_nodes` must be a positive integer, but got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/connector.py","lineNumber":300,"sourceCode":"                if self._strategy_flag.parallel_devices[0].type == \"cpu\":\n                    if self._accelerator_flag and self._accelerator_flag not in (\"auto\", \"cpu\"):\n                        raise ValueError(\n                            f\"CPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,\"\n                            f\" but accelerator set to {self._accelerator_flag}, please choose one device type\"\n                        )\n                    self._accelerator_flag = \"cpu\"\n                if self._strategy_flag.parallel_devices[0].type == \"cuda\":\n                    if self._accelerator_flag and self._accelerator_flag not in (\"auto\", \"cuda\", \"gpu\"):\n                        raise ValueError(\n                            f\"GPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,\"\n                            f\" but accelerator set to {self._accelerator_flag}, please choose one device type\"\n                        )\n                    self._accelerator_flag = \"cuda\"\n                self._parallel_devices = self._strategy_flag.parallel_devices\n\n    def _check_device_config_and_set_final_flags(self, devices: Union[list[int], str, int], num_nodes: int) -> None:\n        if not isinstance(num_nodes, int) or num_nodes < 1:\n            raise ValueError(f\"`num_nodes` must be a positive integer, but got {num_nodes}.\")\n\n        self._num_nodes_flag = num_nodes\n        self._devices_flag = devices\n\n        if self._devices_flag in ([], 0, \"0\"):\n            accelerator_name = (\n                self._accelerator_flag.__class__.__qualname__\n                if isinstance(self._accelerator_flag, Accelerator)\n                else self._accelerator_flag\n            )\n            raise ValueError(\n                f\"`Fabric(devices={self._devices_flag!r})` value is not a valid input\"\n                f\" using {accelerator_name} accelerator.\"\n            )\n\n    @staticmethod\n    def _choose_auto_accelerator() -> str:\n        \"\"\"Choose the accelerator type (str) based on availability when ``accelerator='auto'``.\"\"\"","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/connector.py#L282-L318","documentation":"Fabric validates `num_nodes` immediately in the connector: it must be a Python int and >= 1. Passing 0, a negative number, a float like 2.0, or a string raises this error before any setup runs.","triggerScenarios":"Fabric(num_nodes=0), Fabric(num_nodes=-1), Fabric(num_nodes=\"2\"), or Fabric(num_nodes=2.0) (a bool True passes isinstance but 2 nodes values from config parsing often don't).","commonSituations":"Reading num_nodes from env vars or YAML/JSON config as a string; arithmetic that yields 0 (e.g. world_size // devices_per_node with tiny values); typos in argparse defaults like num_nodes=\"1\".","solutions":["Pass an explicit positive int, e.g. num_nodes=1","Cast config-sourced values: num_nodes=int(raw_value) and validate >= 1 before constructing Fabric","Fix upstream computations that can produce 0 or negative node counts"],"exampleFix":"# before\nfabric = Fabric(num_nodes=os.environ.get(\"NUM_NODES\", \"1\"))\n\n# after\nnum_nodes = int(os.environ.get(\"NUM_NODES\", \"1\"))\nfabric = Fabric(num_nodes=max(1, num_nodes))","handlingStrategy":"type-guard","validationCode":"def coerce_num_nodes(v):\n    n = int(v)\n    if n < 1:\n        raise ValueError(f\"num_nodes must be >= 1, got {n}\")\n    return n\n\nnum_nodes = coerce_num_nodes(os.environ.get(\"NUM_NODES\", 1))\nfabric = Fabric(num_nodes=num_nodes)","typeGuard":"def is_valid_num_nodes(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":null,"preventionTips":["Cast env/config values to int at the boundary","Use argparse type=int for CLI flags"],"tags":["lightning","fabric","num-nodes","argument-validation","distributed"],"backgroundTag":"invalid-argument-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}