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 Alpaca formatting: build the missing-input fallback once per batch (#16341)

Daniel Hanaaf3d70839d7
diff --git a/studio/backend/tests/test_alpaca_blank_cells.py b/studio/backend/tests/test_alpaca_blank_cells.py
index c5310c6aaca..f5596d7cbc8 100644
--- a/studio/backend/tests/test_alpaca_blank_cells.py
+++ b/studio/backend/tests/test_alpaca_blank_cells.py
@@ -148,3 +148,53 @@ def test_cell_text_maps_every_empty_spelling_to_empty_text():
assert cell_text(False) == "False"
assert cell_text("0") == "0"
assert cell_text(" ") == " "
+
+
+def test_alpaca_without_input_column_formats_empty_input_on_every_row():
+ dataset = Dataset.from_dict(
+ {
+ "instruction": ["a", "b", "c", "d", "e"],
+ "output": ["1", None, "3", "4", "5"],
+ }
+ )
+
+ texts = _format(dataset)
+
+ assert len(texts) == 5
+ for instruction, output, text in zip("abcde", ["1", "", "3", "4", "5"], texts):
+ assert text.endswith(
+ f"### Instruction:\n{instruction}\n\n### Input:\n\n\n### Response:\n{output}</s>"
+ )
+
+
+def test_alpaca_input_column_is_read_per_row_across_batches():
+ dataset = Dataset.from_dict(
+ {
+ "instruction": [f"i{n}" for n in range(5)],
+ "input": [f"x{n}" if n % 2 else None for n in range(5)],
+ "output": [f"o{n}" for n in range(5)],
+ }
+ )
+
+ texts = _format(dataset)
+
+ for n, text in enumerate(texts):
+ expected_input = f"x{n}" if n % 2 else ""
+ assert text.endswith(
+ f"### Instruction:\ni{n}\n\n### Input:\n{expected_input}\n\n### Response:\no{n}</s>"
+ )
+
+
+def test_alpaca_system_column_formats_from_a_numpy_formatted_dataset():
+ dataset = Dataset.from_dict(
+ {
+ "instruction": ["a", "b"],
+ "output": ["1", "2"],
+ "system": ["Be brief.", ""],
+ }
+ ).with_format("numpy")
+
+ texts = _format(dataset)
+
+ assert texts[0].startswith("Be brief.\n\n### Instruction:\na\n")
+ assert texts[1].endswith("### Instruction:\nb\n\n### Input:\n\n\n### Response:\n2</s>")
diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.py
index 4d20a317f66..548e7dd1100 100644
--- a/studio/backend/utils/datasets/chat_templates.py
+++ b/studio/backend/utils/datasets/chat_templates.py
@@ -675,22 +675,20 @@ def apply_chat_template_to_dataset(
def _format_alpaca(examples):
texts = []
+ instructions = examples["instruction"]
+ n = len(instructions)
+ inputs = examples["input"] if "input" in examples else [""] * n
systems = examples.get("system")
if systems is None:
- systems = [None] * len(examples["instruction"])
- for i in range(len(examples["instruction"])):
- fields = {
- "instruction": examples["instruction"][i],
- "input": examples.get("input", [""] * len(examples["instruction"]))[i],
- "output": examples["output"][i]
- }
- fields = {key: cell_text(value) for key, value in fields.items()}
- system = systems[i]
+ systems = [None] * n
+ for instruction, input_, output, system in zip(
+ instructions, inputs, examples["output"], systems
+ ):
if not (isinstance(system, str) and system.strip()):
system = preamble
text = system + "\n\n" + alpaca_body.format(
- fields["instruction"], fields["input"], fields["output"]
+ cell_text(instruction), cell_text(input_), cell_text(output)
)
if not text.endswith(eos_token):
text += eos_token