Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: find VLM rows without an image or caption from Arrow validity (#15787)

Daniel Han5ad31fef129f
diff --git a/studio/backend/tests/test_vlm_null_row_selection.py b/studio/backend/tests/test_vlm_null_row_selection.py
new file mode 100644
index 00000000000..00db3003275
--- /dev/null
+++ b/studio/backend/tests/test_vlm_null_row_selection.py
@@ -0,0 +1,123 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""VLM conversion finds rows without an image or text from Arrow validity, with the same rows as walking the column."""
+
+import io
+import sys
+from pathlib import Path
+
+import pytest
+from datasets import Dataset, concatenate_datasets
+from datasets import Image as ImageFeature
+from PIL import Image
+
+_BACKEND_ROOT = Path(__file__).resolve().parents[1]
+if str(_BACKEND_ROOT) not in sys.path:
+ sys.path.insert(0, str(_BACKEND_ROOT))
+
+from utils.datasets import format_conversion # noqa: E402
+from utils.datasets.format_conversion import convert_to_vlm_format # noqa: E402
+
+
+def _png(width):
+ out = io.BytesIO()
+ Image.new("RGB", (width, 1)).save(out, format = "PNG")
+ return out.getvalue()
+
+
+def _reference(dataset, column):
+ return [i for i, value in enumerate(dataset[column]) if value is not None]
+
+
+def _cases():
+ text = ["a", None, "", "b", None, " ", "c"]
+ base = Dataset.from_dict({"text": text, "n": list(range(len(text)))})
+ yield "strings", base, "text"
+ yield "no nulls", Dataset.from_dict({"text": ["x", "", "y"]}), "text"
+ yield "all null", Dataset.from_dict({"text": [None, None]}), "text"
+ yield "empty", base.select([]), "text"
+ yield "lists", Dataset.from_dict({"text": [["a"], None, [], [None], ["b", "c"]]}), "text"
+ yield "structs", Dataset.from_dict({"text": [{"a": None}, None, {"a": "x"}]}), "text"
+ yield "selected", base.select([6, 4, 1, 0, 2]), "text"
+ yield "shuffled", base.shuffle(seed = 3), "text"
+ yield "filtered", base.filter(lambda r: r["n"] % 2 == 0), "text"
+ yield "chunks", concatenate_datasets([base, base.select([1, 0]), base]), "text"
+ images = Dataset.from_dict(
+ {"image": [{"bytes": _png(1), "path": None}, None, {"bytes": _png(3), "path": None}]}
+ )
+ yield "image feature", images.cast_column("image", ImageFeature()), "image"
+
+
[email protected]("name,dataset,column", list(_cases()), ids = [c[0] for c in _cases()])
+def test_non_null_rows_match_the_column_walk(name, dataset, column):
+ assert list(format_conversion._non_null_rows(dataset, column)) == _reference(dataset, column)
+
+
+def test_image_cells_are_not_decoded(monkeypatch):
+ ds = Dataset.from_dict({"image": [{"bytes": _png(2), "path": None}, None]}).cast_column(
+ "image", ImageFeature()
+ )
+ decoded = []
+ real = ImageFeature.decode_example
+ monkeypatch.setattr(
+ ImageFeature,
+ "decode_example",
+ lambda self, *a, **k: decoded.append(1) or real(self, *a, **k),
+ )
+
+ assert list(format_conversion._non_null_rows(ds, "image")) == [0]
+ assert decoded == []
+
+
+def test_conversion_does_not_walk_the_columns(monkeypatch):
+ ds = Dataset.from_dict(
+ {
+ "image": [f"https://example.com/{i}.png" if i != 2 else None for i in range(6)],
+ "caption": [f"c{i}" if i != 4 else None for i in range(6)],
+ }
+ )
+ walked = []
+ real_getitem = Dataset.__getitem__
+
+ def getitem(self, key):
+ if isinstance(key, str) and self.format["type"] != "arrow":
+ walked.append(key)
+ return real_getitem(self, key)
+
+ monkeypatch.setattr(Dataset, "__getitem__", getitem)
+ monkeypatch.setattr(
+ format_conversion, "_fetch_row_image", lambda url: Image.new("RGB", (int(url[-5]) + 1, 1))
+ )
+ logs = []
+ monkeypatch.setattr(
+ format_conversion.logger, "info", lambda msg, *a, **k: logs.append(str(msg))
+ )
+
+ out = convert_to_vlm_format(ds, instruction = "Describe.", text_column = "caption")
+
+ assert walked == []
+ assert [m["messages"][1]["content"][0]["text"] for m in out] == ["c0", "c1", "c3", "c5"]
+ assert "Skipping 1/6 rows without an image" in logs
+ assert "Skipping 1/5 rows without text" in logs
+
+
+def test_objects_without_an_arrow_view_walk_the_column():
+ class Rows:
+ def __init__(self, rows):
+ self.rows = rows
+
+ def __getitem__(self, key):
+ return [r[key] for r in self.rows]
+
+ assert format_conversion._non_null_rows(Rows([{"t": "a"}, {"t": None}, {"t": ""}]), "t") == [
+ 0,
+ 2,
+ ]
+
+
[email protected]("fmt", ["numpy", "pandas"])
+def test_formatted_datasets_keep_the_column_walk(fmt):
+ dataset = Dataset.from_dict({"text": [1.0, None, float("nan"), 2.0]}).with_format(fmt)
+
+ assert list(format_conversion._non_null_rows(dataset, "text")) == _reference(dataset, "text")
diff --git a/studio/backend/utils/datasets/format_conversion.py b/studio/backend/utils/datasets/format_conversion.py
index 350cb0d8e8b..a447747be52 100644
--- a/studio/backend/utils/datasets/format_conversion.py
+++ b/studio/backend/utils/datasets/format_conversion.py
@@ -460,6 +460,27 @@ def _fetch_row_image(url):
return Image.open(BytesIO(fetch(url))).convert("RGB")
+def _non_null_rows(dataset, column):
+ """Row positions whose ``column`` is not None, read from Arrow validity so cells are never built or decoded."""
+ try:
+ # numpy / pandas / torch / custom formats turn nulls into values (NaN, pd.NA) the walk keeps.
+ if dataset.format["type"] not in (None, "python"):
+ raise TypeError("formatted dataset")
+ values = dataset.with_format("arrow")[column]
+ if values.null_count == 0:
+ return range(len(values))
+ import numpy as np
+ import pyarrow as pa
+ import pyarrow.compute as pc
+
+ valid = pc.is_valid(values)
+ if isinstance(valid, pa.ChunkedArray):
+ valid = valid.combine_chunks()
+ return np.flatnonzero(valid.to_numpy(zero_copy_only = False)).tolist()
+ except Exception:
+ return [i for i, value in enumerate(dataset[column]) if value is not None]
+
+
def convert_to_vlm_format(
dataset,
instruction = None,
@@ -558,14 +579,14 @@ def convert_to_vlm_format(
has_urls = isinstance(first_image, str) and first_image.startswith(("http://", "https://"))
if has_urls:
- with_image = [i for i, url in enumerate(dataset[image_column]) if url is not None]
+ with_image = _non_null_rows(dataset, image_column)
if len(with_image) < total:
logger.info(f"Skipping {total - len(with_image)}/{total} rows without an image")
dataset = dataset.select(with_image)
total = len(with_image)
# A null caption would render as the word "None" in the assistant turn, so leave those rows out like rows without an image.
- with_text = [i for i, text in enumerate(dataset[text_column]) if text is not None]
+ with_text = _non_null_rows(dataset, text_column)
if len(with_text) < total:
logger.info(f"Skipping {total - len(with_text)}/{total} rows without text")
dataset = dataset.select(with_text)