{"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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/prompt_templates/common_utils.py#L800-L836","documentation":"extract_file_data accepts only a closed set of content types — file-like objects with .read(), bytes, and the earlier branches (PathLike, tuples, UploadFile). Anything else (int, dict, list, None after earlier handling, arbitrary objects) reaches the else branch and raises ValueError naming the offending type. This guards the file upload path against values that cannot be turned into bytes.","triggerScenarios":"Passing file_data as a dict (e.g. raw JSON payload), an int, or a generator; a file-like object without .read()/.seek(); None slipping through when earlier branches only partially matched; langchain/other-framework objects passed unconverted.","commonSituations":"Wrapping frameworks that hand over their own content abstractions; frontend JSON where file content arrives as a nested dict instead of bytes; partial refactors leaving placeholder values like 0 or {} in file blocks.","solutions":["Convert the value to bytes before passing: extract the actual content and pass bytes or a BytesIO handle.","For framework objects, call their content accessor first (e.g. obj.content, obj.file.read()).","If passing a file handle, ensure it is a real binary file object supporting read() and seek().","Log type(file_data) at your call site to find where the wrong type enters."],"exampleFix":"// before\nfile_data={'name':'a.pdf','body':'raw'}  # dict\n\n# after\nfile_data=(('a.pdf'), b'%PDF-1.4 ...')  # (filename, bytes) tuple","handlingStrategy":"type-guard","validationCode":"def is_supported_file_content(v) -> bool:\n    return isinstance(v, (bytes, bytearray)) or (hasattr(v, 'read') and hasattr(v, 'seek'))","typeGuard":"from typing import Any, TypeGuard\nfrom collections.abc import ByteString\n\ndef is_file_content(v: Any) -> TypeGuard[ByteString]:\n    return isinstance(v, (bytes, bytearray, memoryview))","tryCatchPattern":"try:\n    data = extract_file_data(file_data=content)\nexcept ValueError as e:\n    if 'Unsupported file content type' in str(e):\n        content = content.read() if hasattr(content, 'read') else bytes(content)\n        data = extract_file_data(file_data=content)\n    else:\n        raise","preventionTips":["Normalize file inputs to bytes or file handles at your API boundary.","Add schema validation (e.g. pydantic) on inbound file blocks so bad types fail early with clear errors."],"tags":["files","type-error","input-validation"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}