{"record":{"id":"141a6e758fb32089","repo":"BerriAI/litellm","slug":"unsupported-file-content-type-type-file-content","errorCode":null,"errorMessage":"Unsupported file content type: {type(file_content)}","messagePattern":"Unsupported file content type: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/prompt_templates/common_utils.py","lineNumber":818,"sourceCode":"            filename = Path(file_content).name\n        with open(file_content, \"rb\") as f:\n            content = f.read()\n    elif isinstance(file_content, io.IOBase):\n        # If it's a file-like object\n        # Try to get filename from file handle if not already set\n        if not filename and hasattr(file_content, \"name\"):\n            filename = Path(file_content.name).name\n\n        content = file_content.read()\n\n        if isinstance(content, str):\n            content = content.encode(\"utf-8\")\n        # Reset file pointer to beginning\n        file_content.seek(0)\n    elif isinstance(file_content, bytes):\n        content = file_content\n    else:\n        raise ValueError(f\"Unsupported file content type: {type(file_content)}\")\n\n    # Use provided content type or guess based on filename\n    if not content_type:\n        if filename:\n            guessed_type: Final = mimetypes.guess_type(filename)[0]\n            content_type = guessed_type if guessed_type else \"application/octet-stream\"\n        else:\n            content_type = \"application/octet-stream\"\n\n    return ExtractedFileData(\n        filename=filename,\n        content=content,\n        content_type=content_type,\n        headers=file_headers,\n    )\n\n\n# ---------------------------------------------------------------------------","sourceCodeStart":800,"sourceCodeEnd":836,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/prompt_templates/common_utils.py#L800-L836","documentation":"The tail of extract_file_data()'s type dispatch: file-like objects (anything with .read()) and bytes are handled, then a filename is guessed and content-type inferred - but every other type falls through to ValueError('Unsupported file content type: ...'). Notably bytearray and memoryview are NOT isinstance of bytes in Python, so buffer types land here too.","triggerScenarios":"Passing bytearray or memoryview buffers (common after transformations, compression, or zero-copy slicing), numpy arrays, dicts, or ints as file content in a multimodal message.","commonSituations":"Data piped through libraries that return bytearray (some CSV/crypto libs); PDF/image tooling yielding memoryview; intermediate dicts where bytes were expected; numpy array payloads from numeric pipelines.","solutions":["Coerce buffers: bytes(buf) for bytearray, buf.tobytes() for memoryview","Serialize structured data: json.dumps(obj).encode(\"utf-8\") for dicts","Log type(file_content) at the call site to identify which producer yields the unsupported type","Normalize all file inputs to bytes before constructing messages"],"exampleFix":"# before\ncontent = some_lib.compress(data)  # returns bytearray\nmsg = [{\"type\": \"file\", \"file\": {\"file_content\": content}}]  # ValueError: Unsupported file content type: <class 'bytearray'>\n\n# after\ncontent = bytes(some_lib.compress(data))  # or memoryview_obj.tobytes()\nmsg = [{\"type\": \"file\", \"file\": {\"file_content\": (\"data.bin\", content)}}]","handlingStrategy":"type-guard","validationCode":"def ensure_bytes(v) -> bytes:\n    if isinstance(v, bytearray):\n        return bytes(v)\n    if isinstance(v, memoryview):\n        return v.tobytes()\n    if isinstance(v, str):\n        return v.encode(\"utf-8\")\n    return v  # bytes / file-likes pass through\n\nmsg = [{\"type\": \"file\", \"file\": {\"file_content\": ensure_bytes(content)}}]","typeGuard":"def is_supported_file_content(v: object) -> bool:\n    return isinstance(v, bytes) or hasattr(v, \"read\")","tryCatchPattern":null,"preventionTips":["Normalize all buffers to bytes at the edge of your pipeline","Remember bytearray/memoryview are not bytes - convert explicitly","Log type(file_content) in dev builds to catch unsupported producers early"],"tags":["litellm","file-upload","type-validation"],"backgroundTag":"file-upload-type-validation","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}