Skip to content

Commit 38d2d21

Browse files
committed
fix(dataset): raise a clear error when custom dataset_info is not a list
register_dataset_info loads the --custom_dataset_info JSON and then does `for d_info in dataset_info: _register_d_info(d_info)`. When a user writes a single entry as a top-level object `{...}` instead of the documented one-element list `[{...}]`, `for d_info in dataset_info` iterates the dict keys (strings), and _preprocess_d_info then does `d_info['preprocess_func'] = ...` on a str, raising a cryptic `TypeError: 'str' object does not support item assignment`. The built-in dataset_info.json is a list and the docs state it "is a list of metadata for datasets", so a non-list is a user mistake that deserves a clear message. Validate the loaded dataset_info is a list and raise a helpful ValueError otherwise. Valid list inputs are unaffected.
1 parent 225b1a0 commit 38d2d21

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

swift/dataset/register.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def register_dataset_info(dataset_info: Union[str, List[str], None] = None) -> L
103103
dataset_info = json.load(f)
104104
else:
105105
dataset_info = json.loads(dataset_info) # json
106+
if not isinstance(dataset_info, list):
107+
raise ValueError(
108+
f'`dataset_info` should be a JSON list of dataset entries (e.g. `[{{...}}]`), but got '
109+
f'type `{type(dataset_info).__name__}`. Please wrap the entry in a list.')
106110
if len(dataset_info) == 0:
107111
return []
108112
res = []

tests/general/test_dataset.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ def test_cls():
7979
_test_dataset(['simpleai/HC3-Chinese:baike_cls'])
8080

8181

82+
def test_register_dataset_info_rejects_non_list():
83+
import json
84+
import tempfile
85+
86+
from swift.dataset import register_dataset_info
87+
88+
# A single entry written as a top-level object instead of a one-element list is a
89+
# common mistake; it must fail with a clear message rather than a cryptic
90+
# "'str' object does not support item assignment" from iterating the dict keys.
91+
with tempfile.NamedTemporaryFile('w', suffix='.json', delete=False, encoding='utf-8') as f:
92+
json.dump({'ms_dataset_id': 'swift/self-cognition'}, f)
93+
path = f.name
94+
95+
try:
96+
register_dataset_info(path)
97+
except ValueError as e:
98+
assert 'list' in str(e)
99+
else:
100+
raise AssertionError('expected ValueError for a non-list dataset_info')
101+
102+
82103
if __name__ == '__main__':
83104
# test_sft()
84105
# test_agent()

0 commit comments

Comments
 (0)