Skip to content

Commit 02665ba

Browse files
committed
gguf_writer.py: add_array() should not add to kv store if empty
1 parent 140074b commit 02665ba

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

gguf-py/gguf/gguf_writer.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ def add_string(self, key: str, val: str) -> None:
312312
self.add_key_value(key, val, GGUFValueType.STRING)
313313

314314
def add_array(self, key: str, val: Sequence[Any]) -> None:
315+
if not val:
316+
return
315317
self.add_key_value(key, val, GGUFValueType.ARRAY)
316318

317319
@staticmethod
@@ -845,7 +847,14 @@ def _pack_val(self, val: Any, vtype: GGUFValueType, add_vtype: bool) -> bytes:
845847
encoded_val = val.encode("utf-8") if isinstance(val, str) else val
846848
kv_data += self._pack("Q", len(encoded_val))
847849
kv_data += encoded_val
848-
elif vtype == GGUFValueType.ARRAY and isinstance(val, Sequence) and val:
850+
elif vtype == GGUFValueType.ARRAY:
851+
852+
if not isinstance(val, Sequence):
853+
raise ValueError("Invalid GGUF metadata array, expecting sequence")
854+
855+
if not val:
856+
raise ValueError("Invalid GGUF metadata array. Empty array")
857+
849858
if isinstance(val, bytes):
850859
ltype = GGUFValueType.UINT8
851860
else:

0 commit comments

Comments
 (0)