Skip to content

Commit 8a85533

Browse files
committed
Fix KeyError on empty __metadata__ in safetensors headers
Signed-off-by: noobhappylife <aratar1991@hotmail.com>
1 parent aed9416 commit 8a85533

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

fastsafetensors/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def __init__(
212212
self.framework = framework
213213
ser = json.loads(string, object_pairs_hook=OrderedDict)
214214
self.metadata = ser.get("__metadata__", "")
215-
if self.metadata:
215+
if "__metadata__" in ser:
216216
del ser["__metadata__"]
217217
self.tensors: Dict[str, TensorFrame] = {}
218218
self.header_length = header_length

tests/unit/test_fastsafetensors.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,23 @@ def dribbling_read(fd: int, length: int) -> bytes:
950950
assert meta.tensors["a0"].shape == [4, 8]
951951

952952

953+
def test_from_file_empty_metadata(tmp_dir, framework) -> None:
954+
# Regression test: a safetensors file whose header carries an empty
955+
# __metadata__: {} object must parse without raising. Previously,
956+
# __metadata__ was stripped only when truthy, so {} survived into the
957+
# tensor sort and raised KeyError: 'data_offsets' (see e.g. loading
958+
# MiniMaxAI/MiniMax-M3-MXFP8 via vLLM --load-format fastsafetensors).
959+
device, _ = get_and_check_device(framework)
960+
filename = os.path.join(tmp_dir, "empty_metadata.safetensors")
961+
a0 = framework.randn((4, 8), device=device, dtype=DType.F32)
962+
save_safetensors_file({"a0": a0.get_raw()}, filename, {}, framework)
963+
964+
meta = SafeTensorsMetadata.from_file(filename, framework)
965+
assert "a0" in meta.tensors
966+
assert meta.tensors["a0"].shape == [4, 8]
967+
assert meta.metadata == {}
968+
969+
953970
def test_no_module_level_torch_import_outside_frameworks() -> None:
954971
# Policy: framework-specific imports live behind the frameworks
955972
# abstraction. Only the torch backend (frameworks/_torch.py) may import

0 commit comments

Comments
 (0)