Merge pull request #617 from benjamin7007/fix/strip-thinking-tokens

fix: strip <think> tokens from reasoning model output
This commit is contained in:
Yufan Dang 2026-05-25 11:12:24 +08:00 committed by GitHub
commit 3615218e31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
import base64
import hashlib
import re
import binascii
import os
@ -383,18 +384,31 @@ class OpenAIProvider(ModelProvider):
type="function"
))
content = self._get_attr(msg, "content") or ""
content = self._strip_thinking_tokens(content)
return Message(
role=MessageRole.ASSISTANT,
content=self._get_attr(msg, "content") or "",
content=content,
tool_calls=tool_calls
)
_THINK_PATTERN = re.compile(r"<think>.*?</think>\s*", re.DOTALL)
@classmethod
def _strip_thinking_tokens(cls, text: str) -> str:
"""Strip <think>...</think> blocks from model output (e.g. DeepSeek-R1, MiniMax-M2.7)."""
if "<think>" not in text:
return text
return cls._THINK_PATTERN.sub("", text).strip()
def _append_chat_response_output(self, timeline: List[Any], response: Any) -> None:
"""Add chat response to timeline, preserving tool_calls (Chat API compatible)."""
msg = response.choices[0].message
content = self._strip_thinking_tokens(msg.content or "")
assistant_msg = {
"role": "assistant",
"content": msg.content or ""
"content": content
}
if getattr(msg, "tool_calls", None):