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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Treat {{ and }} in a merged prompt as literal braces (#10742)
Vineeth Sai Varikuntlafc84dd1a9c7a
diff --git a/tests/python/test_to_sharegpt_optional_none.py b/tests/python/test_to_sharegpt_optional_none.pyindex 35e61142130..cfc7c00444f 100644--- a/tests/python/test_to_sharegpt_optional_none.py+++ b/tests/python/test_to_sharegpt_optional_none.py@@ -2,15 +2,26 @@ import astimport refrom pathlib import Path+import pytest+def _load_formatter_builders():# Extract _parse_combined_prompt and _create_formatter without importing unsloth (importing unsloth needs# unsloth_zoo / a GPU).source = Path(__file__).parents[2] / "unsloth" / "chat_templates.py"tree = ast.parse(source.read_text(encoding = "utf-8"))- wanted = {"_parse_combined_prompt", "_create_formatter"}+ wanted = {+ "_ESCAPED_BRACES_RE",+ "_COLUMN_RE",+ "_column_names_in",+ "_parse_combined_prompt",+ "_create_formatter",+ }funcs = [- node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name in wanted+ node+ for node in tree.body+ if getattr(node, "name", None) in wanted+ or (isinstance(node, ast.Assign) and getattr(node.targets[0], "id", None) in wanted)]namespace = {"re": re}module = ast.Module(body = funcs, type_ignores = [])@@ -97,9 +108,19 @@ def _load_to_sharegpt():# Same trick as above: pull to_sharegpt and the two helpers it calls out of the source without importing unsloth.source = Path(__file__).parents[2] / "unsloth" / "chat_templates.py"tree = ast.parse(source.read_text(encoding = "utf-8"))- wanted = {"_parse_combined_prompt", "_create_formatter", "to_sharegpt"}+ wanted = {+ "_ESCAPED_BRACES_RE",+ "_COLUMN_RE",+ "_column_names_in",+ "_parse_combined_prompt",+ "_create_formatter",+ "to_sharegpt",+ }funcs = [- node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name in wanted+ node+ for node in tree.body+ if getattr(node, "name", None) in wanted+ or (isinstance(node, ast.Assign) and getattr(node.targets[0], "id", None) in wanted)]namespace = {"re": re}module = ast.Module(body = funcs, type_ignores = [])@@ -195,3 +216,31 @@ def test_null_cells_match_the_merged_prompt_path():plain = to_sharegpt(Dataset.from_dict(rows))assert [r["conversations"] for r in merged] == [r["conversations"] for r in plain]+++_ROWS = {"instruction": ["Sum 1 and 2"], "input": ["1 2"]}+_COLS = ["instruction", "input"]+++def test_escaped_braces_are_not_column_names():+ out = _render('Task: {instruction}\nReply as {{"answer": <number>}}', _COLS, _ROWS)+ assert out == ['Task: Sum 1 and 2\nReply as {"answer": <number>}']+++def test_escaped_braces_beside_an_optional_block():+ out = _render('{instruction}[[ / {input}]]\nFormat: {{"a": 1}}', _COLS, _ROWS)+ assert out == ['Sum 1 and 2 / 1 2\nFormat: {"a": 1}']+++def test_escaped_braces_wrapping_a_real_column():+ assert _render("{instruction} -> {{{input}}}", _COLS, _ROWS) == ["Sum 1 and 2 -> {1 2}"]+++def test_empty_escaped_pair():+ out = _render("Set is {{}} and task is {instruction}", _COLS, _ROWS)+ assert out == ["Set is {} and task is Sum 1 and 2"]+++def test_misspelled_column_still_rejected():+ with pytest.raises(KeyError, match = "instrution"):+ _render("Task: {instrution}", _COLS, _ROWS)diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.pyindex 33d794184d6..988bbc9a324 100644--- a/unsloth/chat_templates.py+++ b/unsloth/chat_templates.py@@ -2183,8 +2183,17 @@ def remove_special_tokens(tokenizer, prompt):return prompt+# The prompt is rendered with str.format, so `{{` / `}}` are literal braces, not columns.+_ESCAPED_BRACES_RE = re.compile(r"\{\{|\}\}")+_COLUMN_RE = re.compile(r"\{(.+?)\}")+++def _column_names_in(text):+ return _COLUMN_RE.findall(_ESCAPED_BRACES_RE.sub("", text))++def _parse_combined_prompt(combined_prompt, dataset):- possible_columns = re.findall(r"\{(.+?)\}", combined_prompt)+ possible_columns = _column_names_in(combined_prompt)dataset_columns = set(dataset.column_names)for column in possible_columns:if column not in dataset_columns:@@ -2227,14 +2236,14 @@ def _create_formatter(possible_columns, final_optional_prompts, user_column_namefor j, optional_prompt in enumerate(final_optional_prompts):if type(optional_prompt) is str:- needed_columns = re.findall(r"\{(.+?)\}", optional_prompt)+ needed_columns = _column_names_in(optional_prompt)formatter_templates.append(("required", optional_prompt, needed_columns))merged_prompt_parts.append(optional_prompt)continue_, prompt = optional_promptprompt = prompt[2:-2]- needed_columns = re.findall(r"\{(.+?)\}", prompt)+ needed_columns = _column_names_in(prompt)if len(needed_columns) == 0:raise IndexError("Unsloth: Optional [[...]] blocks must contain at least 1 {column}.")optional_name = f"__optional_{j}__"