From 771df1d923f058aaa893e2ef9dfe91573cef366e Mon Sep 17 00:00:00 2001 From: JSap0914 Date: Tue, 16 Jun 2026 17:20:21 +0900 Subject: [PATCH] Fix malformed <|im_start> token in Phi4Reasoning prompt style The first ChatML turn in Phi4Reasoning.apply emitted '<|im_start>system' (missing the closing pipe) instead of the well-formed '<|im_start|>' special token used by Phi4 and every other ChatML-based template. With the malformed marker the Phi-4-reasoning tokenizer does not recognize the system turn as a single special token, corrupting the chat template. Add a regression test asserting the template uses the well-formed token. --- litgpt/prompts.py | 2 +- tests/test_prompts.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/litgpt/prompts.py b/litgpt/prompts.py index 6298d0a962..1cfaf114e8 100644 --- a/litgpt/prompts.py +++ b/litgpt/prompts.py @@ -334,7 +334,7 @@ def apply(self, prompt: str, *, sys_prompt: str | None = None, **kwargs: str) -> sys_prompt or "You are Phi, a language model trained by Microsoft to help users. Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: {Thought section} {Solution section}. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion. Now, try to solve the following question through the above guidelines:" ) - return f"<|im_start>system<|im_sep|>{sys_prompt}<|im_end|><|im_start|>user<|im_sep|>{prompt}<|im_end|><|im_start|>assistant<|im_sep|>" + return f"<|im_start|>system<|im_sep|>{sys_prompt}<|im_end|><|im_start|>user<|im_sep|>{prompt}<|im_end|><|im_start|>assistant<|im_sep|>" class Phi4Mini(PromptStyle): diff --git a/tests/test_prompts.py b/tests/test_prompts.py index 880c5b8c93..d1d0d99542 100644 --- a/tests/test_prompts.py +++ b/tests/test_prompts.py @@ -11,6 +11,7 @@ Default, Llama3, Phi3, + Phi4Reasoning, PromptStyle, has_prompt_style, load_prompt_style, @@ -202,3 +203,12 @@ def test_multiturn_prompt(): output = style.apply(msgs) simple_output = style.apply(content) assert output == simple_output + + +def test_phi4_reasoning_uses_well_formed_im_start_token(): + # Regression test: the first turn previously emitted a malformed "<|im_start>" + # token (missing the closing pipe), unlike every other ChatML/Phi-4 template. + output = Phi4Reasoning().apply("What is 2 + 2?") + assert "<|im_start>" not in output + assert output.startswith("<|im_start|>system<|im_sep|>") + assert output.count("<|im_start|>") == 3