{"record":{"id":"0c4c97b9f1e77c5b","repo":"unslothai/unsloth","slug":"cannot-resolve-image-type-image-data","errorCode":null,"errorMessage":"Cannot resolve image: {type(image_data)}","messagePattern":"Cannot resolve image: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"studio/backend/utils/datasets/format_conversion.py","lineNumber":816,"sourceCode":"                from huggingface_hub import hf_hub_download\n                from utils.hf_cache_settings import active_hf_hub_cache\n\n                local_path = hf_hub_download(\n                    dataset_name,\n                    _image_lookup[image_data],\n                    repo_type = \"dataset\",\n                    cache_dir = active_hf_hub_cache(),\n                )\n                return Image.open(local_path).convert(\"RGB\")\n            else:\n                return Image.open(image_data).convert(\"RGB\")\n        if isinstance(image_data, dict) and (\"bytes\" in image_data or \"path\" in image_data):\n            if image_data.get(\"bytes\"):\n                from io import BytesIO\n                return Image.open(BytesIO(image_data[\"bytes\"])).convert(\"RGB\")\n            if image_data.get(\"path\"):\n                return Image.open(image_data[\"path\"]).convert(\"RGB\")\n        raise ValueError(f\"Cannot resolve image: {type(image_data)}\")\n\n    def _convert_single_sample(sample):\n        \"\"\"Convert one ShareGPT+image sample to standard VLM format.\"\"\"\n        pil_image = _resolve_image(sample[image_column])\n        conversation = sample[messages_column]\n\n        new_messages = []\n        for msg in conversation:\n            role_raw = msg.get(\"from\") or msg.get(\"role\", \"user\")\n            role = _ROLE_MAP.get(role_raw.lower(), role_raw.lower())\n            text = msg.get(\"value\") or msg.get(\"content\") or \"\"\n\n            # Interleave text and image blocks around <image>\n            if \"<image>\" in text:\n                parts = text.split(\"<image>\")\n                content = []\n                for i, part in enumerate(parts):\n                    part = part.strip()","sourceCodeStart":798,"sourceCodeEnd":834,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/utils/datasets/format_conversion.py#L798-L834","documentation":"Raised by the internal _resolve_image helper during ShareGPT+image conversion when a sample's image value matches none of the supported shapes. Supported inputs are: a string path/URL (including HF hub repo refs resolved via cache), or a dict with 'bytes' or 'path' keys. Anything else (int, float, list, None, dict without those keys) hits the terminal ValueError with the offending type name.","triggerScenarios":"The image column of a ShareGPT-format dataset contains values like None, integers (class IDs), lists (bounding boxes), or dicts whose keys are neither 'bytes' nor 'path' (e.g. {'url': ...} or {'image': ...}).","commonSituations":"Loading a classification dataset (label column instead of image), a detection dataset with list annotations, or datasets using non-HF image encodings such as {'url': ...} raw dicts; schema drift after a dataset revision changed the image column format.","solutions":["Inspect sample[image_column] types across the dataset (df['image'].map(type).value_counts()) to find the unexpected shape.","Pre-map unsupported dicts to supported ones, e.g. {'url': x} -> {'path': x} or download bytes into 'bytes'.","Drop rows with None/invalid image values before conversion.","If the column is actually labels/boxes, pick the correct image column for conversion.","For dict-based HF image features, ensure the dataset was loaded with the image feature intact (not decoded to raw dicts by a transform)."],"exampleFix":"# before\n# image column contains {'url': 'https://...'} -> ValueError: Cannot resolve image: <class 'dict'>\n\n# after\nds = ds.map(lambda r: {\"image\": {\"path\": r[\"image\"][\"url\"]}})\nconverted = convert_sharegpt_images(ds)","handlingStrategy":"type-guard","validationCode":"from collections import Counter\n\ndef audit_image_column(ds, image_column):\n    \"\"\"Report value types in the image column before ShareGPT conversion.\"\"\"\n    return Counter(type(r).__name__ for r in ds[image_column])\n\n# only proceed when audit shows dict/str only:\n# audit_image_column(ds, 'images') -> {'dict': 1000} is fine; {'int': 500} is not","typeGuard":"def is_supported_image_value(value) -> bool:\n    \"\"\"Mirror _resolve_image's supported shapes.\"\"\"\n    if isinstance(value, str) and value:\n        return True\n    if isinstance(value, dict) and (\"bytes\" in value or \"path\" in value):\n        return bool(value.get(\"bytes\") or value.get(\"path\"))\n    return False","tryCatchPattern":"converted = []\nfor sample in ds:\n    if is_supported_image_value(sample[image_column]):\n        converted.append(_convert_single_sample(sample))  # skip unsupported rows\n# then handle empty `converted` explicitly","preventionTips":["Type-audit the image column (str or {'bytes'/'path'} dict) before conversion.","Pre-normalize non-HF dict encodings like {'url': ...} to {'path': ...}.","Filter null image rows with ds.filter(lambda r: r[image_column] is not None).","Freeze dataset revisions so schema drift cannot silently change image encodings."],"tags":["dataset","sharegpt","vlm","type-validation","image-resolution"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}