fix: strip partial think tags in streaming output

This commit is contained in:
bravel
2026-05-01 20:09:59 +08:00
committed by Xubin Ren
parent aea5948b11
commit 2c397ad442
3 changed files with 39 additions and 0 deletions
+10
View File
@@ -32,6 +32,8 @@ def strip_think(text: str) -> str:
explanatory prose that mentions these tokens.
5. Orphan closing tags `</think>` / `</thought>` **at the very start
or end of the text** only, for the same reason.
6. Trailing partial control tags split across stream chunks, such as
`<thi`, `<thin`, or `<tho`.
Since this is also applied before persisting to history (memory.py),
the edge-only stripping of (4) and (5) is deliberate: stripping those
@@ -58,6 +60,14 @@ def strip_think(text: str) -> str:
text = re.sub(r"\s*</thought>\s*$", "", text)
# Edge-only channel markers (harmony / Gemma 4 variant leaks).
text = re.sub(r"^\s*<\|?channel\|?>\s*", "", text)
# Stream chunks may end in the middle of a control tag. Strip only known
# control-token prefixes at the very end.
partial_control_tag = (
r"</?(?:t|th|thi|thin|think|tho|thou|thoug|though|thought)"
r"|<\|?(?:c|ch|cha|chan|chann|channe|channel|channel\|?)"
)
text = re.sub(rf"(?:{partial_control_tag})$", "", text)
text = re.sub(r"^\s*<\|?$", "", text)
return text.strip()