{"record":{"id":"a56123963e3d6290","repo":"TheAlgorithms/Python","slug":"invalid-magic-number-magic-in-mnist-label-file","errorCode":null,"errorMessage":"Invalid magic number {magic} in MNIST label file: {f.name}","messagePattern":"Invalid magic number (.+?) in MNIST label file: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"neural_network/input_data.py","lineNumber":105,"sourceCode":"    \"\"\"Extract the labels into a 1D uint8 numpy array [index].\n\n    Args:\n      f: A file object that can be passed into a gzip reader.\n      one_hot: Does one hot encoding for the result.\n      num_classes: Number of classes for the one hot encoding.\n\n    Returns:\n      labels: a 1D uint8 numpy array.\n\n    Raises:\n      ValueError: If the bystream doesn't start with 2049.\n    \"\"\"\n    print(\"Extracting\", f.name)\n    with gzip.GzipFile(fileobj=f) as bytestream:\n        magic = _read32(bytestream)\n        if magic != 2049:\n            msg = f\"Invalid magic number {magic} in MNIST label file: {f.name}\"\n            raise ValueError(msg)\n        num_items = _read32(bytestream)\n        buf = bytestream.read(num_items)\n        labels = np.frombuffer(buf, dtype=np.uint8)\n        if one_hot:\n            return _dense_to_one_hot(labels, num_classes)\n        return labels\n\n\nclass _DataSet:\n    \"\"\"Container class for a _DataSet (deprecated).\n\n    THIS CLASS IS DEPRECATED.\n    \"\"\"\n\n    @deprecated(\n        None,\n        \"Please use alternatives such as official/mnist/_DataSet.py\"\n        \" from tensorflow/models.\",","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/neural_network/input_data.py#L87-L123","documentation":"Raised by _extract_labels while parsing a gzipped MNIST label file when the first 32-bit field (the magic number) is not 2049. The MNIST IDX format prefixes every file with a magic number that identifies its content type, so this check distinguishes label files (2049) from image files (2051). A mismatch means the byte stream being parsed is not a valid MNIST label file.","triggerScenarios":"Calling read_data_sets (which opens the downloaded train-labels/t10k-labels files) when the label file is corrupted, truncated, empty, or is actually an image file (magic 2051) opened by mistake. Also triggered by pointing SOURCE_URL/train_dir at a directory containing wrong or partial files.","commonSituations":"Interrupted or partial downloads, a mirror that serves an HTML error page instead of the .gz file, swapped image/label filenames in a custom mirror, or hand-crafted IDX files with an incorrect magic header.","solutions":["Delete the MNIST files in train_dir and let _maybe_download re-fetch them from a known-good mirror, then retry","Verify the file is valid gzip: gunzip -t train-labels-idx1-ubyte.gz","Check the first bytes after decompression: the 4-byte big-endian value must be 2049 (0x00000801); if it is 2051 you have an image file where a label file was expected","If using a custom SOURCE_URL, confirm the label-file URL actually points to the idx1 (labels) archive, not the idx3 (images) archive"],"exampleFix":"# before: wrong file was placed where labels are expected\nwith gfile.Open('train-images-idx3-ubyte.gz', 'rb') as f:\n    labels = _extract_labels(f)  # ValueError: magic is 2051\n\n# after: open the labels archive\nwith gfile.Open('train-labels-idx1-ubyte.gz', 'rb') as f:\n    labels = _extract_labels(f)","handlingStrategy":"validation","validationCode":"import gzip, struct\n\ndef is_valid_mnist_label_file(path) -> bool:\n    try:\n        with gzip.open(path, 'rb') as f:\n            return struct.unpack('>I', f.read(4))[0] == 2049\n    except (OSError, EOFError):\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    labels = _extract_labels(f, one_hot=True)\nexcept ValueError as e:\n    if 'magic number' in str(e):\n        raise RuntimeError(f'Corrupt MNIST label file {f.name}; re-download it') from e\n    raise","preventionTips":["Keep the download directory writable and re-download on any checksum or gzip integrity failure","Never mix image (magic 2051) and label (magic 2049) archives when preparing custom MNIST mirrors","Validate the 4-byte magic header before invoking the extraction helpers"],"tags":["mnist","data-validation","file-format","gzip"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}