BerriAI/litellm · error · ValueError

Unsupported file content type

Error message

Unsupported file content type: {type(file_content)}

What it means

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.

Solutions

  1. Coerce buffers: bytes(buf) for bytearray, buf.tobytes() for memoryview
  2. Serialize structured data: json.dumps(obj).encode("utf-8") for dicts
  3. Log type(file_content) at the call site to identify which producer yields the unsupported type
  4. Normalize all file inputs to bytes before constructing messages

Example fix

# before
content = some_lib.compress(data)  # returns bytearray
msg = [{"type": "file", "file": {"file_content": content}}]  # ValueError: Unsupported file content type: <class 'bytearray'>

# after
content = bytes(some_lib.compress(data))  # or memoryview_obj.tobytes()
msg = [{"type": "file", "file": {"file_content": ("data.bin", content)}}]
Defensive patterns

Strategy: type-guard

Validate before calling

def ensure_bytes(v) -> bytes:
    if isinstance(v, bytearray):
        return bytes(v)
    if isinstance(v, memoryview):
        return v.tobytes()
    if isinstance(v, str):
        return v.encode("utf-8")
    return v  # bytes / file-likes pass through

msg = [{"type": "file", "file": {"file_content": ensure_bytes(content)}}]

Type guard

def is_supported_file_content(v: object) -> bool:
    return isinstance(v, bytes) or hasattr(v, "read")

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/141a6e758fb32089. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/prompt_templates/common_utils.py:818

            filename = Path(file_content).name
        with open(file_content, "rb") as f:
            content = f.read()
    elif isinstance(file_content, io.IOBase):
        # If it's a file-like object
        # Try to get filename from file handle if not already set
        if not filename and hasattr(file_content, "name"):
            filename = Path(file_content.name).name

        content = file_content.read()

        if isinstance(content, str):
            content = content.encode("utf-8")
        # Reset file pointer to beginning
        file_content.seek(0)
    elif isinstance(file_content, bytes):
        content = file_content
    else:
        raise ValueError(f"Unsupported file content type: {type(file_content)}")

    # Use provided content type or guess based on filename
    if not content_type:
        if filename:
            guessed_type: Final = mimetypes.guess_type(filename)[0]
            content_type = guessed_type if guessed_type else "application/octet-stream"
        else:
            content_type = "application/octet-stream"

    return ExtractedFileData(
        filename=filename,
        content=content,
        content_type=content_type,
        headers=file_headers,
    )


# ---------------------------------------------------------------------------

View on GitHub (pinned to 77b7c6c40c)