From 0e2e3dbf39a0c43de368b16b065f3cf58daa615f Mon Sep 17 00:00:00 2001 From: aplicity <1634594707@qq.com> Date: Mon, 9 Nov 2026 22:45:27 +0800 Subject: [PATCH] fix(llm): stop dropping the answer when a reply mentions Two inputs lost text that should have been shown: - a reply that names the tag in prose ("Use to start a reasoning block.") matched the unterminated-block rule, which deletes from the tag to the end of the text, so only the words before it survived; - a block opened with one spelling and closed with the other ("...") matched neither pattern, fell through to the same rule, and took the answer after the closing tag with it. The unterminated rule now applies only to a tag that starts a line, which is what a truncated scratchpad actually looks like, and either spelling closes a block. A closed block still disappears; a mid-sentence mention now survives. --- deeptutor/services/llm/utils.py | 10 ++++++++-- tests/services/llm/test_utils.py | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/deeptutor/services/llm/utils.py b/deeptutor/services/llm/utils.py index 99fbf3c08..2a2e48b6d 100644 --- a/deeptutor/services/llm/utils.py +++ b/deeptutor/services/llm/utils.py @@ -187,15 +187,21 @@ def clean_thinking_tags( if not content: return "" + # Either spelling closes the block. A provider that opens with and + # closes with (or the reverse) used to match neither pattern, so + # the block fell through to the unclosed rule and took the answer with it. closed_pattern = re.compile( - r"`?<\s*(?Pthink(?:ing)?)\b[^>]*>`?.*?`?<\s*/\s*(?P=tag)\s*>`?", + r"`?<\s*(?:think|thinking)\b[^>]*>`?.*?`?<\s*/\s*(?:think|thinking)\s*>`?", re.DOTALL | re.IGNORECASE, ) cleaned = re.sub(closed_pattern, "", content) # Streaming providers can surface a final partial block if the request is # interrupted after reasoning has started. Never expose that scratchpad. + # Only a block that starts a line counts as that scratchpad: a reply that + # mentions "" mid-sentence is prose, and deleting from there to the + # end of the text dropped the rest of the answer. unclosed_pattern = re.compile( - r"`?<\s*think(?:ing)?\b[^>]*>`?.*$", + r"(?:\A|\n)[ \t]*`?<\s*think(?:ing)?\b[^>]*>`?.*\Z", re.DOTALL | re.IGNORECASE, ) cleaned = re.sub(unclosed_pattern, "", cleaned) diff --git a/tests/services/llm/test_utils.py b/tests/services/llm/test_utils.py index 57d581481..ac3400f2d 100644 --- a/tests/services/llm/test_utils.py +++ b/tests/services/llm/test_utils.py @@ -76,6 +76,32 @@ def test_clean_thinking_tags_handles_backtick_wrapped_tags() -> None: assert clean_thinking_tags("``ignore``Hello") == "Hello" +def test_clean_thinking_tags_keeps_prose_that_names_the_tag() -> None: + """A reply explaining the tag is prose, not an unterminated scratchpad.""" + assert ( + clean_thinking_tags("Use to start a reasoning block.") + == "Use to start a reasoning block." + ) + assert ( + clean_thinking_tags("The `` tag marks the scratchpad.") + == "The `` tag marks the scratchpad." + ) + + +def test_clean_thinking_tags_closes_on_either_spelling() -> None: + """... is a closed block, not an unterminated one.""" + assert clean_thinking_tags("reasoninganswer") == "answer" + assert clean_thinking_tags("reasoninganswer") == "answer" + + +def test_clean_thinking_tags_keeps_text_after_a_closed_block() -> None: + """Only the block is removed; a later literal mention survives intact.""" + assert ( + clean_thinking_tags("rThe tag is a marker.") + == "The tag is a marker." + ) + + def test_extract_response_content() -> None: """Response content extraction should handle mapping payloads.""" payload = {"content": [{"text": "Hello"}, {"text": "World"}]}