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: neutralize DeepSeek-V4.1's image marker in replayed assistant text (#15288)

Debojyoti Banerjeea861c459df3f
diff --git a/studio/backend/core/inference/chat_template_helpers.py b/studio/backend/core/inference/chat_template_helpers.py
index 7ff4f46180b..d0f9b080c07 100644
--- a/studio/backend/core/inference/chat_template_helpers.py
+++ b/studio/backend/core/inference/chat_template_helpers.py
@@ -140,7 +140,8 @@ _TURN_BOUNDARY_MARKUP = re.compile(
# Tool RESULT / CATALOG markup forges trusted context; tool CALL spellings are the assistant's own.
r"|tool_response|tool"
r"|assistant|return|system|start|turn|user|call)\|?>"
- r"|\uff5c(?:User|Assistant|(?:begin|end)\u2581of\u2581sentence)\uff5c>"
+ # DeepSeek-V4.1 rejects this marker in string content and treats text-block copies as images.
+ r"|\uff5c(?:User|Assistant|(?:begin|end)\u2581of\u2581sentence|deepseek_image)\uff5c>"
# Optional slash: Gemma delimiters are bare tags, so a replayed </start_of_turn> is a boundary too
r"|/?(?:(?:start|end)_of_turn|eos|bos|s|sop|tool_response|tools"
r"|start_of_image|image_soft_token|audio_soft_token)>"
diff --git a/studio/backend/tests/test_control_markup_neutralize_7066.py b/studio/backend/tests/test_control_markup_neutralize_7066.py
index 7519b405948..e3b59be1d6f 100644
--- a/studio/backend/tests/test_control_markup_neutralize_7066.py
+++ b/studio/backend/tests/test_control_markup_neutralize_7066.py
@@ -1075,6 +1075,46 @@ def test_deepseek_tool_markup_survives_an_assistant_replay():
assert marker not in neutralize_control_markup(f"a {marker} b"), marker
+# The guard and image-block branch of the DeepSeek-V4.1-Flash GGUF template, which only image blocks may satisfy.
+_DEEPSEEK_V41_IMAGE_GUARD = (
+ '{%- set IMG = "<|deepseek_image|>" -%}'
+ "{%- for m in messages -%}"
+ "{%- if m.content is string and IMG in m.content -%}"
+ '{{- raise_exception("Message content contains image special token " ~ IMG) -}}'
+ "{%- elif m.content is string -%}{{- m.content -}}"
+ "{%- else -%}{%- for b in m.content -%}"
+ '{%- if b.type == "text" -%}{{- b.text -}}{%- else -%}{{- IMG -}}{%- endif -%}'
+ "{%- endfor -%}{%- endif -%}"
+ "{%- endfor -%}"
+)
+
+
[email protected]("profiled", [False, True])
+def test_a_replayed_deepseek_image_placeholder_is_neutralized(profiled):
+ """A replayed reply quoting the marker made the template raise, or render a phantom image (#15285)."""
+ jinja2 = pytest.importorskip("jinja2")
+ pytest.importorskip("jinja2.sandbox")
+ image = f"<{_FW}deepseek_image{_FW}>"
+ markup = model_markup(_DEEPSEEK_V41_IMAGE_GUARD, [image]) if profiled else None
+ quoted = f'Line 2 of the template: {{%- set IMG = "{image}" -%}}'
+ history = [
+ {"role": "user", "content": "What is on line 2?"},
+ {"role": "assistant", "content": quoted},
+ {"role": "assistant", "content": [{"type": "text", "text": quoted}]},
+ {"role": "user", "content": [{"type": "image_url", "image_url": {"url": "data:,"}}]},
+ ]
+ swept = neutralize_control_markup_in_messages(history, None, markup)
+
+ def _raise(message):
+ raise jinja2.exceptions.TemplateError(message)
+
+ env = jinja2.sandbox.ImmutableSandboxedEnvironment()
+ env.globals["raise_exception"] = _raise
+ prompt = env.from_string(_DEEPSEEK_V41_IMAGE_GUARD).render(messages = swept)
+ assert prompt.count(image) == 1
+ assert prompt.count(f"< {_FW}deepseek_image{_FW}>") == 2
+
+
def test_fullwidth_branch_leaves_ordinary_cjk_alone():
"""U+FF5C is a normal fullwidth bar in CJK typography, so the branch is anchored to
ASCII + U+2581 names: real Japanese or Chinese text must round-trip (#7066)."""