From d34dd9192da06d203eb7e80d20229117bb4e981c Mon Sep 17 00:00:00 2001 From: Vinay Umrethe <175500353+Vinay-Umrethe@users.noreply.github.com> Date: Mon, 26 Oct 2026 23:43:56 +0530 Subject: [PATCH] fix: Improve response prefix logic, handle default behaviors, and clarify documentation (#523) * fix: Handle the default thinking behavior Some chat-templates insert full CoT block when thinking is *disabled* by Default like Qwen3.5-2B. And some models by Default only insert the CoT initializer (somtimes possibly with whitespaces) like Qwen3.5-4B when thinking is *enabled*. Co-authored-by: benthecarman * chore: Update CI test hashes Removing $ from pattern_initializer broke the test model mistral-3, which was expected * fix: Several fixes, docs update, cleanup * chore: Update Windows test hash very much expected, and looks good to me * chore: Update CI test hashes * chore: Update CI test hash (alternate) good to see that it caught the third hash as well. Anyway, the GitHub CI remains terrible for such tests * fix: minor change * fix: Override the system prompt for the dummy_prompt case This is correct for models which insert instructions about 'How to think' in the system prompt, which can cause false-positives when searching the dummy_prompt * chore: Update CI test hashes * chore: Update CI test hashes * fix: minor change * fix: Add alternate CI test hash I'm not liking having a fourth hash btw --------- Co-authored-by: benthecarman --- config.default.toml | 12 ++-- src/heretic/config.py | 12 ++-- src/heretic/main.py | 102 ++++++++++++++++++++------- tests/deepseek-v4-ara/SHA256SUMS.ci | 2 +- tests/deepseek-v4-ara/SHA256SUMS.ci2 | 2 +- tests/deepseek-v4-ara/SHA256SUMS.ci3 | 2 +- tests/deepseek-v4-ara/SHA256SUMS.ci4 | 1 + tests/deepseek-v4/SHA256SUMS.ci | 2 +- tests/deepseek-v4/SHA256SUMS.ci2 | 2 +- tests/deepseek-v4/SHA256SUMS.ci3 | 2 +- tests/deepseek-v4/SHA256SUMS.ci4 | 2 +- tests/mistral-3/SHA256SUMS.ci | 2 +- tests/mistral-3/SHA256SUMS.ci3 | 2 +- tests/mistral-3/SHA256SUMS.windows | 2 +- tests/qwen3.5-moe/SHA256SUMS.ci | 2 +- tests/qwen3.5-moe/SHA256SUMS.ci2 | 7 ++ tests/qwen3.5-moe/SHA256SUMS.windows | 2 +- 17 files changed, 107 insertions(+), 51 deletions(-) create mode 100644 tests/deepseek-v4-ara/SHA256SUMS.ci4 create mode 100644 tests/qwen3.5-moe/SHA256SUMS.ci2 diff --git a/config.default.toml b/config.default.toml index 0c21bf2..f7c77a1 100644 --- a/config.default.toml +++ b/config.default.toml @@ -42,29 +42,29 @@ max_batch_size = 128 # Maximum number of tokens to generate for each response. max_response_length = 100 -# List of pairs of the form [cot_initializer, closed_cot_block] used to skip +# List of pairs of the form [cot_initializer, cot_finalizer] used to skip # the Chain-of-Thought block in responses, so that evaluation happens # at the start of the actual response. -chain_of_thought_skips = [ +chain_of_thought_blocks = [ # Most thinking models. [ "", - "", + "", ], # gpt-oss. [ "<|channel|>analysis<|message|>", - "<|channel|>analysis<|message|><|end|><|start|>assistant<|channel|>final<|message|>", + "<|end|><|start|>assistant<|channel|>final<|message|>", ], # Unknown, suggested by user. [ "", - "", + "", ], # Unknown, suggested by user. [ "[THINK]", - "[THINK][/THINK]", + "[/THINK]", ], ] diff --git a/src/heretic/config.py b/src/heretic/config.py index cee7df1..ee56cc7 100644 --- a/src/heretic/config.py +++ b/src/heretic/config.py @@ -332,31 +332,31 @@ class Settings(BaseSettings): exclude=True, ) - chain_of_thought_skips: list[tuple[str, str]] = Field( + chain_of_thought_blocks: list[tuple[str, str]] = Field( default=[ # Most thinking models. ( "", - "", + "", ), # gpt-oss. ( "<|channel|>analysis<|message|>", - "<|channel|>analysis<|message|><|end|><|start|>assistant<|channel|>final<|message|>", + "<|end|><|start|>assistant<|channel|>final<|message|>", ), # Unknown, suggested by user. ( "", - "", + "", ), # Unknown, suggested by user. ( "[THINK]", - "[THINK][/THINK]", + "[/THINK]", ), ], description=( - "List of pairs of the form (cot_initializer, closed_cot_block) used to skip " + "List of pairs of the form (cot_initializer, cot_finalizer) used to skip " "the Chain-of-Thought block in responses, so that evaluation happens " "at the start of the actual response." ), diff --git a/src/heretic/main.py b/src/heretic/main.py index 4d70c87..a11679a 100644 --- a/src/heretic/main.py +++ b/src/heretic/main.py @@ -490,19 +490,36 @@ def run(): print(f"* [bold]{len(response_prefix_test_prompts)}[/] prompts loaded") print() - print("Checking chat template for a Chain-of-Thought initializer...") + print( + "Checking chat template for a closed Chain-of-Thought block or an initializer..." + ) - # Detect if the model's chat template inserts a reasoning tag on its own - # at the end of user's prompt (e.g. ) by using a dummy prompt. - # If found, then we use the full closed CoT as the response prefix. - # LiquidAI's LFM models do this (Lfm2ForCausalLM). + # Detect if the model's chat template inserts a closed Chain-of-Thought block + # (e.g. ) or an initializer (e.g. ) on its own at the end + # of the user's prompt by using a dummy prompt. + # If a full closed block is found, no response prefix is required. If only an + # initializer is found, we use the finalizer as the response prefix (e.g. ) + # so that when it's concatenated, it completes the closed CoT block. + # LiquidAI's LFM2.5 models and Qwen's Qwen3.5 models do this by + # inserting the CoT initializer to enable thinking by default. + # + # If the model supports system messages, we provide a custom one to + # override the default system instructions for this case. Because they + # may contain CoT examples like '[THINK]...[/THINK]', which would trigger + # false-positive matches when searching in the dummy prompt (e.g. Mistral-3). + dummy_messages = [] + if model.supports_system_role: + dummy_messages.append( + {"role": "system", "content": "You are a helpful assistant."} + ) + dummy_messages.append({"role": "user", "content": "This is a dummy prompt."}) # This cast is valid because str is the return type # for a single chat operation with tokenize=False. dummy_prompt = cast( str, model.tokenizer.apply_chat_template( - [{"role": "user", "content": "This is a dummy prompt."}], + dummy_messages, add_generation_prompt=True, tokenize=False, ), @@ -510,22 +527,44 @@ def run(): cot_skip_applied = False - for cot_initializer, closed_cot_block in settings.chain_of_thought_skips: - # Match the tag and ignore any whitespace characters following it at the end - # (if any), including spaces, tabs, and linebreaks. This is required for models - # having whitespaces after the tags. - pattern = rf"{re.escape(cot_initializer)}\s*$" - match = re.search(pattern, dummy_prompt) - - if match: - # We use only the closed CoT block here. Any whitespaces - # will be handled by the 'Rechecking with prefix' logic below. - settings.response_prefix = closed_cot_block + for cot_initializer, cot_finalizer in settings.chain_of_thought_blocks: + # Check if the chat template already inserts the full closed CoT block + # (e.g. When thinking is disabled by the chat template). + # Smaller variants like Qwen/Qwen3.5-2B do this by inserting the full + # CoT block to disable thinking by default, while larger variants only + # insert the initializer to enable thinking by default (>=4B). + # We allow arbitrary characters and whitespaces, including newlines via + # re.DOTALL between the initializer and the finalizer. + # This makes the logic more generic for patterns which pre-fill + # some text (e.g. Some instruction). + pattern_closed = ( + rf"{re.escape(cot_initializer)}.*{re.escape(cot_finalizer)}" + ) + if re.search(pattern_closed, dummy_prompt, flags=re.DOTALL): + settings.response_prefix = "" print( - f"* Chat template inserts [bold]{escape(repr(cot_initializer))}[/]" + f"* Chat template inserts full closed CoT block [bold]{escape(repr(cot_initializer + cot_finalizer))}[/]" + ) + print("* Response prefix is not required") + cot_skip_applied = True + break + + if cot_initializer in dummy_prompt: + # The chat template already inserted the initializer, so we just + # supply the finalizer as the response prefix. So that when + # concatenated to the prompts during generation, it forms a full + # closed CoT block (e.g. ). + # Passing a full closed CoT block for models which only insert the + # initializer is wrong, resulting in: '\n\n\n' + # during the generation for Qwen3.5 models (>=4B) as an example. + # Any trailing whitespaces will be handled by the + # 'Rechecking with prefix' logic below. + settings.response_prefix = cot_finalizer + print( + f"* Chat template inserts CoT initializer: [bold]{escape(repr(cot_initializer))}[/]" ) print( - f"* Closed Chain-of-Thought block: [bold]{escape(repr(settings.response_prefix))}[/]" + f"* Remaining CoT finalizer: [bold]{escape(repr(cot_finalizer))}[/]" ) cot_skip_applied = True break @@ -534,10 +573,13 @@ def run(): print("* None found") # Fallback to inference for models like mistral-3 which are specifically - # instructed to generate thinking tags using the system prompt in their - # chat template, instead of inserting a prefix tag (e.g. ) at - # the end of user prompt like the case above. We expect the model to - # generate those tags. + # instructed to generate the CoT block using the system prompt in their + # chat template, instead of inserting an initializer at the end of + # the user prompt like the case above. The test prompts do not + # override the system prompt, so here we expect the model to generate + # the initializer, which we then close using the finalizer. + # Some models may inherently generate the CoT block simply due + # to their training, regardless of instructions. if settings.response_prefix is None: print() print("Checking for common response prefix...") @@ -558,10 +600,10 @@ def run(): for ( cot_initializer, - closed_cot_block, - ) in settings.chain_of_thought_skips: + cot_finalizer, + ) in settings.chain_of_thought_blocks: if settings.response_prefix.startswith(cot_initializer): - settings.response_prefix = closed_cot_block + settings.response_prefix = cot_initializer + cot_finalizer print( f"* Closed Chain-of-Thought block: [bold]{escape(repr(settings.response_prefix))}[/]" ) @@ -570,7 +612,13 @@ def run(): else: print("* None found") - if cot_skip_applied: + # Only recheck if we actually supply a prefix to find any missed whitespaces + # like '' (supplied finalizer) or '[THINK][/THINK]' (supplied closed CoT + # block for models instructed to think, e.g. mistral-3). + # When the chat template already inserts a full closed block, response_prefix is "" + # and rechecking is not required. Otherwise it would just run inference + # and set common words as the response_prefix (e.g. 'Sure,', 'I cannot'). + if cot_skip_applied and settings.response_prefix: # When using a Chain-of-Thought skip, we need to check that the prefix # is actually complete (e.g. not missing a trailing newline). print("* Rechecking with prefix...") diff --git a/tests/deepseek-v4-ara/SHA256SUMS.ci b/tests/deepseek-v4-ara/SHA256SUMS.ci index 9f6187a..f44e97a 100644 --- a/tests/deepseek-v4-ara/SHA256SUMS.ci +++ b/tests/deepseek-v4-ara/SHA256SUMS.ci @@ -1 +1 @@ -03ca3c7b8ad59ddae470c86849f6623e08ccf7ef911ab239fe339e3fbedbb8b9 *adapter_model.safetensors +1c202cb35786c40bbd59fb5065a8497883ecddaed0ca899baa4f74f1ee4cf6f2 *adapter_model.safetensors diff --git a/tests/deepseek-v4-ara/SHA256SUMS.ci2 b/tests/deepseek-v4-ara/SHA256SUMS.ci2 index 46dabeb..f03a469 100644 --- a/tests/deepseek-v4-ara/SHA256SUMS.ci2 +++ b/tests/deepseek-v4-ara/SHA256SUMS.ci2 @@ -1 +1 @@ -f01ac38a0efdfd876f604f3ecba4f7239c1e8b4c199d0711291dfba52eb107a4 *adapter_model.safetensors +d568ee21e84b934aac09dcd0d2fdccd0f708d2fbf52d16ee5d957d5d07fa9ec5 *adapter_model.safetensors diff --git a/tests/deepseek-v4-ara/SHA256SUMS.ci3 b/tests/deepseek-v4-ara/SHA256SUMS.ci3 index b5b6aaa..f635330 100644 --- a/tests/deepseek-v4-ara/SHA256SUMS.ci3 +++ b/tests/deepseek-v4-ara/SHA256SUMS.ci3 @@ -1 +1 @@ -a4e17011af1957d0e66505b302dc4b6f098e85a823a95ef1b9d93e5c3623b9c7 *adapter_model.safetensors +f7ef967e0bf0532014c9ba8828ac63eb4fe216f625ef360dc3d4ea8f4f7cfe8a *adapter_model.safetensors diff --git a/tests/deepseek-v4-ara/SHA256SUMS.ci4 b/tests/deepseek-v4-ara/SHA256SUMS.ci4 new file mode 100644 index 0000000..e4cad41 --- /dev/null +++ b/tests/deepseek-v4-ara/SHA256SUMS.ci4 @@ -0,0 +1 @@ +4841d8c994690004df8cf28d7606ba1078577c87e79f3caa704e6e2a66a9e0fa *adapter_model.safetensors diff --git a/tests/deepseek-v4/SHA256SUMS.ci b/tests/deepseek-v4/SHA256SUMS.ci index 66c73e1..dc4ea10 100644 --- a/tests/deepseek-v4/SHA256SUMS.ci +++ b/tests/deepseek-v4/SHA256SUMS.ci @@ -1 +1 @@ -221e865de7fbd712de6d0f19012e3da915b326eb797a2b430afa146c6d1d6cf2 *adapter_model.safetensors +fab6b4c5e8e86d0117451ade5ceb8fa83599b67af9bc543f6f4fd2db5ed523ba *adapter_model.safetensors diff --git a/tests/deepseek-v4/SHA256SUMS.ci2 b/tests/deepseek-v4/SHA256SUMS.ci2 index 375f387..d4e8d3a 100644 --- a/tests/deepseek-v4/SHA256SUMS.ci2 +++ b/tests/deepseek-v4/SHA256SUMS.ci2 @@ -1 +1 @@ -e73ed117efc71ba636195891caa393abee29eb136273dcf2a48ebec0c3611717 *adapter_model.safetensors +4b81cbbe8ed75c4d82ed60ff9c883319ca436d3d834e613cd0a46cc51f6ab05d *adapter_model.safetensors diff --git a/tests/deepseek-v4/SHA256SUMS.ci3 b/tests/deepseek-v4/SHA256SUMS.ci3 index 8a3c9a1..7cf72cc 100644 --- a/tests/deepseek-v4/SHA256SUMS.ci3 +++ b/tests/deepseek-v4/SHA256SUMS.ci3 @@ -1 +1 @@ -e62c18d8cd28576234d45978ea273ce446be0eebc74b170080a8f9238ec2a233 *adapter_model.safetensors +cb742e4929c6883dbe586978c48560c7c28da53bce0752bdb3b1d707328c7158 *adapter_model.safetensors diff --git a/tests/deepseek-v4/SHA256SUMS.ci4 b/tests/deepseek-v4/SHA256SUMS.ci4 index 63592ff..e76fef2 100644 --- a/tests/deepseek-v4/SHA256SUMS.ci4 +++ b/tests/deepseek-v4/SHA256SUMS.ci4 @@ -1 +1 @@ -6b923a6e0d39ed7664d36a913c7e885c684d8996a1958fc4d470afdcc586d70f *adapter_model.safetensors +c2c1262cba3f64f81591bf82b178cf20e1e7091473d2229042157c7c49caa7ff *adapter_model.safetensors diff --git a/tests/mistral-3/SHA256SUMS.ci b/tests/mistral-3/SHA256SUMS.ci index d9aa980..34d77a2 100644 --- a/tests/mistral-3/SHA256SUMS.ci +++ b/tests/mistral-3/SHA256SUMS.ci @@ -1,7 +1,7 @@ 39f03c383413f531fd302c06c7e982ad98c83f0657a8339ae25478ccb81fdcda *chat_template.jinja b2cfd8a9da09efc97040f3839fe629f3925db856a44b58314301dc5e11345ec8 *config.json 338d303c3c3df6884030cf5be24560f6e973fc183220d14acfb9364cd0e070e4 *generation_config.json -05497f43a38427c5813105fc007ce0dd41be65470e9cb8f343443f31bab7f0fe *model.safetensors +4cf4d463a94f477ed446e47130cb7da6b5994f0b788e75b82b3c65a531585621 *model.safetensors a05f93a41b7e42ccc18461b540fb54490c3ffa13aef79c800bbe7942e02e360f *processor_config.json c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json 7b29c843c0043622d28fd4638451cbb0a609d99a0762ffbff3b92b4b2fee4d94 *tokenizer_config.json diff --git a/tests/mistral-3/SHA256SUMS.ci3 b/tests/mistral-3/SHA256SUMS.ci3 index 34d77a2..d9aa980 100644 --- a/tests/mistral-3/SHA256SUMS.ci3 +++ b/tests/mistral-3/SHA256SUMS.ci3 @@ -1,7 +1,7 @@ 39f03c383413f531fd302c06c7e982ad98c83f0657a8339ae25478ccb81fdcda *chat_template.jinja b2cfd8a9da09efc97040f3839fe629f3925db856a44b58314301dc5e11345ec8 *config.json 338d303c3c3df6884030cf5be24560f6e973fc183220d14acfb9364cd0e070e4 *generation_config.json -4cf4d463a94f477ed446e47130cb7da6b5994f0b788e75b82b3c65a531585621 *model.safetensors +05497f43a38427c5813105fc007ce0dd41be65470e9cb8f343443f31bab7f0fe *model.safetensors a05f93a41b7e42ccc18461b540fb54490c3ffa13aef79c800bbe7942e02e360f *processor_config.json c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json 7b29c843c0043622d28fd4638451cbb0a609d99a0762ffbff3b92b4b2fee4d94 *tokenizer_config.json diff --git a/tests/mistral-3/SHA256SUMS.windows b/tests/mistral-3/SHA256SUMS.windows index f88145d..1b24771 100644 --- a/tests/mistral-3/SHA256SUMS.windows +++ b/tests/mistral-3/SHA256SUMS.windows @@ -1,7 +1,7 @@ 72f84af4ea36b82409c35e31b584361534305ef7c0d90fce20d0dc38a7efead8 *chat_template.jinja c2b697282298f9500f1170b9237dc234dbe1cb7ca741fe56b034d64d9e38e1d2 *config.json d9f7f986f62f4f1f87790070237e0214865e5c227f4941f7308c838702a69934 *generation_config.json -8244162ecb6ce7bad7b10fe4f015889d76a09c7749dc7ac8c4d957f0864b2e82 *model.safetensors +1bc5d08088f30b35f829609f38a6cde565d26d33050c8d581e39e2a86a442cab *model.safetensors 5abd409b04043c99c3fcd52b06cc412b46fb4bd14c8ef9f79695a3be1490987b *processor_config.json c3a8d92e371b92a2cd6e678e31ebc27d0235e929a51fbf290f74742b341fa96f *tokenizer.json 60a8042e29b4b20e884e48375aa1b9ac0025547371d50e60f6d55e6a9675e868 *tokenizer_config.json diff --git a/tests/qwen3.5-moe/SHA256SUMS.ci b/tests/qwen3.5-moe/SHA256SUMS.ci index 5bdfe28..8722d36 100644 --- a/tests/qwen3.5-moe/SHA256SUMS.ci +++ b/tests/qwen3.5-moe/SHA256SUMS.ci @@ -1,7 +1,7 @@ a4aee8afcf2e0711942cf848899be66016f8d14a889ff9ede07bca099c28f715 *chat_template.jinja 4b501bef0727793de90d446fcfdc7757a9f8d19efc42ceaa3d26e90db4f876d5 *config.json 532190619b0b243e0558a846257cc62883c26df812d456f8133e7b1043c69ad8 *generation_config.json -cfa4d5428e9b23c245fd2413ad702688cbcacd25808ecb17889e97c0976532ac *model.safetensors +aee5ba8012fe581a2905a72e278d80b6e65cc1ac08dfb328772c18825724deb3 *model.safetensors 3814737f2d4bfdd153a82364b3850f1be5dbc6f074a55c8d5fbf4d40127c3e87 *processor_config.json a5cd9732badce41de57e6efce8302930ded1c1188c5f81feb2bd6c24c4a1941f *tokenizer.json 5e55dc6b9d9d28d49b6b2b80d2a2f2558a0b6e0e7b194bbca8664fc269ab3ebc *tokenizer_config.json diff --git a/tests/qwen3.5-moe/SHA256SUMS.ci2 b/tests/qwen3.5-moe/SHA256SUMS.ci2 new file mode 100644 index 0000000..421bc5b --- /dev/null +++ b/tests/qwen3.5-moe/SHA256SUMS.ci2 @@ -0,0 +1,7 @@ +a4aee8afcf2e0711942cf848899be66016f8d14a889ff9ede07bca099c28f715 *chat_template.jinja +4b501bef0727793de90d446fcfdc7757a9f8d19efc42ceaa3d26e90db4f876d5 *config.json +532190619b0b243e0558a846257cc62883c26df812d456f8133e7b1043c69ad8 *generation_config.json +845fda6e6bfa82f7e28930606816f7f0962d053996a912e8d18d5b95ce5d154e *model.safetensors +3814737f2d4bfdd153a82364b3850f1be5dbc6f074a55c8d5fbf4d40127c3e87 *processor_config.json +a5cd9732badce41de57e6efce8302930ded1c1188c5f81feb2bd6c24c4a1941f *tokenizer.json +5e55dc6b9d9d28d49b6b2b80d2a2f2558a0b6e0e7b194bbca8664fc269ab3ebc *tokenizer_config.json diff --git a/tests/qwen3.5-moe/SHA256SUMS.windows b/tests/qwen3.5-moe/SHA256SUMS.windows index e4acd62..87338bd 100644 --- a/tests/qwen3.5-moe/SHA256SUMS.windows +++ b/tests/qwen3.5-moe/SHA256SUMS.windows @@ -1,7 +1,7 @@ a92e1dd97cb1cb175c9b70c0828e146bea4371c2643319b661b777e89811972e *chat_template.jinja 356d8dbfb902b90da033c2d52057a806bfb6515769dd0a838a2f9f63f11ff50f *config.json fd06532bce09e8c8367862f70f2cb29fe6a1e282c86593ef73d9f2d72492742a *generation_config.json -bf3d8ecacf8c2612ad23e0902a2ba9a0b2c8f8aa96ffbc31f424a847e8721f46 *model.safetensors +9ac21259a4afa23b4bd60750749cf0aa397f207f7d764850837af92da46967c9 *model.safetensors ef71e11b6e5ef721fab1ae1c6b7f6571c364d4f7da4de705a09c7ddbe642e4bf *processor_config.json a5cd9732badce41de57e6efce8302930ded1c1188c5f81feb2bd6c24c4a1941f *tokenizer.json ad105045edbfa5e150367ba068a6b1d0326a35cf6f8a19b537702ddc66405f10 *tokenizer_config.json