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: keep each row's own question when training on a vision dataset (#12909)

Nilay1ff07c4b9065
diff --git a/studio/backend/tests/test_vlm_question_column.py b/studio/backend/tests/test_vlm_question_column.py
new file mode 100644
index 00000000000..fadf6e004f2
--- /dev/null
+++ b/studio/backend/tests/test_vlm_question_column.py
@@ -0,0 +1,116 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import pytest
+from datasets import Dataset
+from PIL import Image
+
+from utils.datasets import format_and_template_dataset, llm_assist
+
+HELPER_SENTENCE = "Solve the math problem shown in the image."
+GENERIC_SENTENCE = "Describe this image in detail."
+
+
[email protected](autouse = True)
+def _helper_writes_one_sentence(monkeypatch):
+ monkeypatch.setattr(
+ llm_assist,
+ "llm_generate_vlm_instruction",
+ lambda **kwargs: {"instruction": HELPER_SENTENCE, "confidence": 0.85},
+ )
+
+
+def _format(rows, mapping = None):
+ images = [Image.new("RGB", (8, 8), (i * 40, 0, 0)) for i in range(2)]
+ ds = Dataset.from_dict({"image": images, **rows})
+ info = format_and_template_dataset(
+ ds,
+ model_name = "unsloth/Qwen2.5-VL-3B-Instruct",
+ tokenizer = None,
+ is_vlm = True,
+ dataset_name = "org/ds",
+ custom_format_mapping = mapping,
+ )
+ assert info["success"], info["errors"]
+ return info["dataset"]
+
+
+def _turns(sample):
+ user, assistant = sample["messages"]
+ user_text = next(part["text"] for part in user["content"] if part["type"] == "text")
+ return user_text, assistant["content"][0]["text"]
+
+
+ "question_col, answer_col, mapping",
+ [
+ ("problem", "solution", True),
+ ("problem", "answer", False),
+ ("Question", "Answer", False),
+ ("inputs", "outputs", True),
+ ("input", "output", False),
+ ],
+)
+def test_each_row_keeps_its_own_question(question_col, answer_col, mapping):
+ questions = ["Find x in the triangle.", "How many red cubes are left?"]
+ answers = ["x = 42", "3"]
+ out = _format(
+ {question_col: questions, answer_col: answers},
+ {"image": "image", answer_col: "text"} if mapping else None,
+ )
+
+ assert [_turns(sample) for sample in out] == list(zip(questions, answers))
+
+
+def test_answer_column_is_not_reused_as_the_question():
+ prompts = ["a red square", "a dark red square"]
+ out = _format({"prompt": prompts}, {"image": "image", "prompt": "text"})
+
+ assert [_turns(sample) for sample in out] == [(HELPER_SENTENCE, p) for p in prompts]
+
+
+def test_exact_question_column_wins_over_a_case_variant():
+ questions = ["Find x in the triangle.", "How many red cubes are left?"]
+ answers = ["x = 42", "3"]
+ out = _format(
+ {"Question": ["upper one", "upper two"], "question": questions, "answer": answers}
+ )
+
+ assert [_turns(sample) for sample in out] == list(zip(questions, answers))
+
+
+def test_populated_case_variant_wins_over_an_empty_exact_column():
+ questions = ["Find x in the triangle.", "How many red cubes are left?"]
+ answers = ["x = 42", "3"]
+ out = _format({"Question": questions, "question": [None, None], "answer": answers})
+
+ assert [_turns(sample) for sample in out] == list(zip(questions, answers))
+
+
+def test_structured_input_column_is_not_used_as_the_question():
+ answers = ["a red square", "a dark red square"]
+ out = _format({"inputs": [[101, 102], [103]], "caption": answers})
+
+ assert [_turns(sample) for sample in out] == [(HELPER_SENTENCE, a) for a in answers]
+
+
+def test_missing_question_value_uses_the_generic_instruction():
+ questions = ["Find x in the triangle.", None]
+ answers = ["x = 42", "3"]
+ out = _format({"problem": questions, "answer": answers})
+
+ assert [_turns(sample) for sample in out] == [
+ (questions[0], answers[0]),
+ (GENERIC_SENTENCE, answers[1]),
+ ]
+
+
+def test_later_question_value_is_kept_when_the_first_is_missing():
+ questions = [None, "How many red cubes are left?"]
+ answers = ["x = 42", "3"]
+ out = _format({"problem": questions, "answer": answers})
+
+ assert [_turns(sample) for sample in out] == [
+ (GENERIC_SENTENCE, answers[0]),
+ (questions[1], answers[1]),
+ ]
diff --git a/studio/backend/utils/datasets/format_conversion.py b/studio/backend/utils/datasets/format_conversion.py
index bc0dd5868db..51eb137d7ef 100644
--- a/studio/backend/utils/datasets/format_conversion.py
+++ b/studio/backend/utils/datasets/format_conversion.py
@@ -457,6 +457,8 @@ def convert_to_vlm_format(
if uses_dynamic and instruction_column:
current_instruction = sample[instruction_column]
+ if not isinstance(current_instruction, str) or not current_instruction.strip():
+ current_instruction = "Describe this image in detail."
else:
current_instruction = instruction
diff --git a/studio/backend/utils/datasets/vlm_processing.py b/studio/backend/utils/datasets/vlm_processing.py
index 0494ae5556b..1b3744a7f39 100644
--- a/studio/backend/utils/datasets/vlm_processing.py
+++ b/studio/backend/utils/datasets/vlm_processing.py
@@ -18,34 +18,31 @@ def generate_smart_vlm_instruction(
image_column = "image",
dataset_name = None,
):
- """
- Generate a smart, context-aware instruction for VLM datasets via heuristics.
-
- Strategy:
- 1. Explicit question/instruction column → use that
- 2. Infer from text column name + sample content
- 3. Analyze dataset name for task hints
- 4. Generic fallback
-
- Returns:
- dict: {
- "instruction": str or None, # None means use column content
- "instruction_type": "explicit" | "inferred" | "generic",
- "uses_dynamic_instruction": bool, # True if it varies per sample
- "confidence": float, # 0.0 to 1.0
- }
- """
- column_names = set(next(iter(dataset)).keys())
+ """selects a VLM instruction from explicit columns, heuristics, an LLM, or the default."""
sample = next(iter(dataset))
-
- # Columns that hold per-sample instructions
- question_columns = ["question", "query", "prompt", "instruction", "user_prompt"]
-
- for col in question_columns:
- if col in column_names:
- # Use it only if it has non-empty content
- sample_content = sample[col]
- if sample_content and str(sample_content).strip():
+ column_names = set(sample.keys())
+
+ question_columns = [
+ "question",
+ "query",
+ "prompt",
+ "instruction",
+ "user_prompt",
+ "problem",
+ "input",
+ "inputs",
+ ]
+ columns = [col for col in sample if col not in (text_column, image_column)]
+
+ for name in question_columns:
+ matches = ([name] if name in columns else []) + [
+ col for col in columns if col != name and col.lower() == name
+ ]
+ for col in matches:
+ has_text = any(
+ isinstance(row.get(col), str) and row[col].strip() for row in islice(dataset, 100)
+ )
+ if has_text:
return {
"instruction": None,
"instruction_column": col,