{"record":{"id":"48de6af832361a42","repo":"roboflow/supervision","slug":"expected-names-to-be-a-list-or-dict-in-data-yaml","errorCode":null,"errorMessage":"Expected 'names' to be a list or dict in data.yaml at '{file_path}', got {type(names).__name__}.","messagePattern":"Expected 'names' to be a list or dict in data\\.yaml at '(.+?)', got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/dataset/formats/yolo.py","lineNumber":134,"sourceCode":"            return False\n\n        int_like = [_is_int_like(k) for k in keys]\n        if any(int_like) and not all(int_like):\n            mixed_numeric = [k for k, il in zip(keys, int_like) if il][:3]\n            mixed_other = [k for k, il in zip(keys, int_like) if not il][:3]\n            raise ValueError(\n                f\"Expected 'names' dict in data.yaml at '{file_path}' to have either \"\n                f\"all numeric or all non-numeric keys, got a mix: \"\n                f\"numeric {mixed_numeric} and non-numeric {mixed_other} keys.\"\n            )\n        if all(int_like):\n            sorted_keys = sorted(keys, key=lambda k: int(k))\n        else:\n            sorted_keys = sorted(keys, key=str)\n        return [str(names[key]) for key in sorted_keys]\n    if isinstance(names, list):\n        return [str(name) for name in names]\n    raise ValueError(\n        \"Expected 'names' to be a list or dict in data.yaml at \"\n        f\"'{file_path}', got {type(names).__name__}.\"\n    )\n\n\ndef _image_name_to_annotation_name(image_name: str) -> str:\n    base_name, _ = os.path.splitext(image_name)\n    return base_name + \".txt\"\n\n\ndef yolo_annotations_to_detections(\n    lines: list[str],\n    resolution_wh: tuple[int, int],\n    with_masks: bool,\n    is_obb: bool = False,\n) -> Detections:\n    if len(lines) == 0:\n        return Detections.empty()","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/dataset/formats/yolo.py#L116-L152","documentation":"Raised by the YOLO data.yaml class-name loader when the 'names' entry is neither a list nor a dict (e.g. a plain string or a number). Ultralytics-style data.yaml must define names either as a list in class-index order or as a mapping of index/name; anything else cannot be mapped to ordered class names, so supervision raises with the actual type found.","triggerScenarios":"DetectionDataset.from_yolo(data_yaml_path=...) where the YAML contains e.g. `names: person` (single bare string), `names: 80`, `names: null`, or a nested structure instead of a list/dict.","commonSituations":"Typo in data.yaml (missing `-` bullet or braces); minimal hand-written yaml that gives a scalar class name; a YAML indentation mistake that turns a list into a string; using a non-Ultralytics yaml schema.","solutions":["Open the data.yaml at the path in the error and inspect the `names:` entry.","Rewrite names as a list (order = class index): names: [person, car, dog]","Or as an explicit index map: names: {0: person, 1: car, 2: dog}.","Re-run DetectionDataset.from_yolo; if it still fails, validate the yaml loads as expected with PyYAML first."],"exampleFix":"# before (data.yaml)\nnames: person\n# after (data.yaml)\nnames:\n  - person\n  - car","handlingStrategy":"type-guard","validationCode":"import yaml\n\ndef load_names(data_yaml: str) -> list[str]:\n    \"\"\"Validate the names entry of a YOLO data.yaml before use.\"\"\"\n    names = yaml.safe_load(open(data_yaml)).get('names')\n    assert isinstance(names, (list, dict)), type(names)\n    return list(names) if isinstance(names, list) else [names[k] for k in sorted(names)]","typeGuard":"def is_valid_names(names: object) -> bool:\n    \"\"\"names must be a list or a dict to be usable as class names.\"\"\"\n    return isinstance(names, (list, dict))","tryCatchPattern":"try:\n    dataset = sv.DetectionDataset.from_yolo(data_yaml_path='data.yaml')\nexcept ValueError as e:\n    if \"Expected 'names'\" in str(e):\n        raise SystemExit(f'Fix data.yaml: {e}') from e\n    raise","preventionTips":["Use the list form names: [a, b] in data.yaml — least ambiguity.","Validate yaml with a linter or PyYAML load before dataset operations.","Copy data.yaml structure from a known-good Ultralytics dataset."],"tags":["yolo","dataset","yaml","config"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}