fix: strip reasoning model thinking tags from document exports (#16687)

Co-authored-by: noob <yixiao121314@outlook.com>
This commit is contained in:
Wang Qi
2026-07-07 12:17:53 +08:00
committed by GitHub
parent a0bda639e0
commit 3660b98ae9
2 changed files with 22 additions and 1 deletions

View File

@@ -189,7 +189,7 @@ class DocGenerator(Message, ABC):
flags=re.DOTALL,
)
return content
return self._strip_thinking(content)
def _get_output_directory(self) -> str:
os.makedirs(self._default_output_directory, exist_ok=True)

View File

@@ -365,10 +365,31 @@ class Message(ComponentBase):
return None
@staticmethod
def _strip_thinking(content: str) -> str:
"""Remove <think>...</think> reasoning blocks before document export.
Reasoning models (e.g. DeepSeek-R1, OpenAI o1) embed chain-of-thought
inside ``<think>`` tags. These blocks must not leak into exported
Word/PDF/Excel documents.
"""
if not isinstance(content, str) or not content:
return content
# Remove complete think blocks (DOTALL so newlines are matched)
cleaned = re.sub(r"<think>.*?</think>", "", content, flags=re.DOTALL)
# Remove any dangling unclosed <think> opening tag + trailing content
cleaned = re.sub(r"<think>.*$", "", cleaned, flags=re.DOTALL)
# Remove leftover standalone tags
cleaned = re.sub(r"</?think>", "", cleaned)
# Collapse 3+ consecutive newlines left behind by removed blocks
cleaned = re.sub(r"\n{3,}", "\n\n", cleaned)
return cleaned.strip()
def _convert_content(self, content):
if not self._param.output_format:
return
content = self._strip_thinking(content)
import pypandoc
doc_id = get_uuid()