From 5b347afd9925dfe9017feae5725a93230b123b30 Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Sat, 8 Aug 2026 05:05:45 +0800 Subject: [PATCH] fix: avoid AttributeError in JinaEmbeddings image input for str/Path (#3670) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What `JinaEmbeddings._generate_image_input_dict()` crashes with `AttributeError: 'function' object has no attribute 'urlparse'` on any image given as a URL string, local path string, or `pathlib.Path` — i.e. every documented `jina-clip-v1` image-embedding use case except raw `bytes`. ## Why ```python from urllib.parse import urlparse ... parsed = urlparse.urlparse(image) ``` `urlparse` is imported as a function, then called as if it were the `urllib.parse` module (`urlparse.urlparse(...)`). The module-level `is_valid_url()` a few lines above does it correctly (`urlparse(text)`), which is why this reads as a typo rather than intentional. Fixed to `urlparse(str(image))` — `str()` is needed because `urlparse()` only accepts `str`/`bytes` and raises a different `AttributeError` on a raw `Path`. ## Testing Added `test_jina_generate_image_input_dict_local_path`, which fails with the original `AttributeError` before the fix and passes after, covering both a `str` path and a `pathlib.Path`. Verified locally (built the Rust extension, ran red→green, then the full `test_embeddings.py` file: 15 passed / 8 skipped, no regressions) and with `ruff check`/`ruff format`. --- Disclosure: this PR was drafted with AI assistance (Claude); I reviewed, tested, and take responsibility for the change. --------- Co-authored-by: Claude Opus 4.8 --- python/python/lancedb/embeddings/jinaai.py | 7 ++++--- python/python/tests/test_embeddings.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/python/python/lancedb/embeddings/jinaai.py b/python/python/lancedb/embeddings/jinaai.py index 9656f041f..f6ab601b3 100644 --- a/python/python/lancedb/embeddings/jinaai.py +++ b/python/python/lancedb/embeddings/jinaai.py @@ -87,12 +87,13 @@ class JinaEmbeddings(EmbeddingFunction): if isinstance(image, bytes): image_dict = {"image": base64.b64encode(image).decode("utf-8")} elif isinstance(image, (str, Path)): - parsed = urlparse.urlparse(image) - # TODO handle drive letter on windows. + parsed = urlparse(str(image)) PIL_Image = attempt_import_or_raise("PIL.Image", "pillow") if parsed.scheme == "file": pil_image = PIL_Image.open(parsed.path) - elif parsed.scheme == "": + elif parsed.scheme == "" or (os.name == "nt" and len(parsed.scheme) == 1): + # A Windows drive letter parses as a one-character scheme + # ("C:\\img.png" -> scheme="c"), so treat it as a local path. pil_image = PIL_Image.open(image if os.name == "nt" else parsed.path) elif parsed.scheme.startswith("http"): pil_image = PIL_Image.open(io.BytesIO(url_retrieve(image))) diff --git a/python/python/tests/test_embeddings.py b/python/python/tests/test_embeddings.py index 678270f19..9850669eb 100644 --- a/python/python/tests/test_embeddings.py +++ b/python/python/tests/test_embeddings.py @@ -631,3 +631,23 @@ def test_url_retrieve_downloads_image(): image_bytes = url_retrieve(image_url) img = Image.open(io.BytesIO(image_bytes)) assert img.size[0] > 0 and img.size[1] > 0 + + +def test_jina_generate_image_input_dict_local_path(tmp_path): + """ + JinaEmbeddings._generate_image_input_dict must accept a local image path + (str or Path), not just bytes. Previously it crashed with + `AttributeError: 'function' object has no attribute 'urlparse'` on any + str/Path input because it called `urlparse.urlparse(image)` instead of + `urlparse(image)` (urlparse was imported as a function, not a module). + """ + Image = pytest.importorskip("PIL.Image") + from lancedb.embeddings.jinaai import JinaEmbeddings + + image_path = tmp_path / "test.png" + Image.new("RGB", (4, 4), color="red").save(image_path, format="PNG") + + for image in (str(image_path), image_path): + image_dict = JinaEmbeddings._generate_image_input_dict(image) + assert "image" in image_dict + assert isinstance(image_dict["image"], str) and len(image_dict["image"]) > 0