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: reuse the VLM URL probe's downloads in the conversion (#15870)

Daniel Han6055083f61e0
diff --git a/studio/backend/tests/test_vlm_probe_reuses_downloads.py b/studio/backend/tests/test_vlm_probe_reuses_downloads.py
new file mode 100644
index 00000000000..90f6691cf04
--- /dev/null
+++ b/studio/backend/tests/test_vlm_probe_reuses_downloads.py
@@ -0,0 +1,78 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The VLM URL probe's downloads are reused by the conversion; a failed probe download is still retried."""
+
+import sys
+import threading
+from collections import Counter
+from pathlib import Path
+
+import pytest
+from datasets import Dataset
+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
+
+
[email protected](autouse = True)
+def no_llm_warning(monkeypatch):
+ from utils.datasets import llm_assist
+ def refuse(*args, **kwargs):
+ raise RuntimeError("no LLM in tests")
+
+ monkeypatch.setattr(llm_assist, "llm_generate_dataset_warning", refuse)
+
+
+def fetches(monkeypatch):
+ calls = Counter()
+ lock = threading.Lock()
+
+ def fake_fetch(url):
+ with lock:
+ calls[url] += 1
+ attempt = calls[url]
+ index = int(url.rsplit("/", 1)[1].split(".")[0])
+ if index % 9 == 0:
+ raise OSError(f"HTTP 404 for {index}")
+ if index < 200 and index % 13 == 0 and attempt == 1:
+ raise OSError(f"transient failure for {index}")
+ return Image.new("RGB", (4, 4), (index % 256, index // 256, 7))
+
+ monkeypatch.setattr(format_conversion, "_fetch_row_image", fake_fetch)
+ return calls
+
+
+def _dataset(total):
+ return Dataset.from_list(
+ [{"image": f"https://example.com/{i}.png", "caption": f"caption {i}"} for i in range(total)]
+ )
+
+
[email protected]("total", [450, 1200])
+def test_probe_downloads_are_not_fetched_again(fetches, total):
+ converted = convert_to_vlm_format(
+ _dataset(total), instruction = "Describe.", text_column = "caption"
+ )
+
+ # Every URL is fetched once, except one whose probe download failed, which the conversion retries.
+ probe_failures = {i for i in range(200) if i % 9 == 0 or i % 13 == 0}
+ expected = Counter(
+ {f"https://example.com/{i}.png": 2 if i in probe_failures else 1 for i in range(total)}
+ )
+ assert fetches == expected
+
+ # Rows that only failed transiently in the probe still train; permanent 404s are skipped.
+ kept = [i for i in range(total) if i % 9 != 0]
+ assert [row["messages"][1]["content"][0]["text"] for row in converted] == [
+ f"caption {i}" for i in kept
+ ]
+ for row, i in zip(converted, kept):
+ image = row["messages"][0]["content"][1]["image"]
+ assert image.getpixel((0, 0)) == (i % 256, i // 256, 7)
diff --git a/studio/backend/utils/datasets/format_conversion.py b/studio/backend/utils/datasets/format_conversion.py
index 7e2f2511332..74e6b68b858 100644
--- a/studio/backend/utils/datasets/format_conversion.py
+++ b/studio/backend/utils/datasets/format_conversion.py
@@ -497,13 +497,13 @@ def convert_to_vlm_format(
instruction_column = None
uses_dynamic = False
- def _convert_single_sample(sample):
- """Image may be a PIL Image, local path, URL, or bare filename."""
+ def _convert_single_sample(sample, fetched = None):
+ """Image may be a PIL Image, local path, URL, or bare filename; ``fetched`` = the URL's already-downloaded image."""
image_data = sample[image_column]
if isinstance(image_data, str):
if image_data.startswith(("http://", "https://")):
- image_data = _fetch_row_image(image_data)
+ image_data = fetched if fetched is not None else _fetch_row_image(image_data)
elif _image_lookup is not None and image_data in _image_lookup:
# Bare filename → resolve via HF repo lookup
from huggingface_hub import hf_hub_download
@@ -609,6 +609,9 @@ def convert_to_vlm_format(
PROBE_SIZE = 200
MAX_FAIL_RATE = 0.3
+ # Successful probe downloads by row, reused by the conversion.
+ probe_images = {}
+
if has_urls and total > PROBE_SIZE:
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
@@ -623,8 +626,15 @@ def convert_to_vlm_format(
probe_fail = 0
probe_start = time.time()
+ def _probe_sample(index, sample):
+ url = sample[image_column]
+ if isinstance(url, str) and url.startswith(("http://", "https://")):
+ # A failed download is not kept, so the conversion retries it.
+ probe_images[index] = _fetch_row_image(url)
+ return _convert_single_sample(sample, probe_images.get(index))
+
with ThreadPoolExecutor(max_workers = num_workers) as executor:
- futures = {executor.submit(_convert_single_sample, s): s for s in probe_samples}
+ futures = {executor.submit(_probe_sample, i, s): s for i, s in enumerate(probe_samples)}
for future in as_completed(futures):
try:
future.result()
@@ -701,7 +711,9 @@ def convert_to_vlm_format(
with ThreadPoolExecutor(max_workers = num_workers) as executor:
futures = {
- executor.submit(_convert_single_sample, s): i
+ executor.submit(
+ _convert_single_sample, s, probe_images.pop(batch_start + i, None)
+ ): i
for i, s in enumerate(batch_samples)
}
batch_results = [None] * len(batch_samples)