Skip to content

Commit dae7c24

Browse files
fix(provider): recognize thinking tags in compatible completions (#10116)
Co-authored-by: konodiodaaaaa1 <[email protected]>
1 parent 2782454 commit dae7c24

2 files changed

Lines changed: 76 additions & 5 deletions

File tree

‎astrbot/core/provider/sources/openai_source.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,17 +854,19 @@ async def _parse_openai_completion(
854854
# parse the text completion
855855
if choice.message.content is not None:
856856
completion_text = self._normalize_content(choice.message.content)
857-
# specially, some providers may set <think> tags around reasoning content in the completion text,
857+
# Some compatible providers wrap reasoning in <think> or <thinking> tags.
858858
# we use regex to remove them, and store then in reasoning_content field
859-
reasoning_pattern = re.compile(r"<think>(.*?)</think>", re.DOTALL)
859+
reasoning_pattern = re.compile(r"<(think|thinking)>(.*?)</\1>", re.DOTALL)
860860
matches = reasoning_pattern.findall(completion_text)
861861
if matches:
862862
llm_response.reasoning_content = "\n".join(
863-
[match.strip() for match in matches],
863+
[match[1].strip() for match in matches],
864864
)
865865
completion_text = reasoning_pattern.sub("", completion_text).strip()
866-
# Also clean up orphan </think> tags that may leak from some models
867-
completion_text = re.sub(r"</think>\s*$", "", completion_text).strip()
866+
# Also clean up trailing reasoning closing tags from some models.
867+
completion_text = re.sub(
868+
r"</(?:think|thinking)>\s*$", "", completion_text
869+
).strip()
868870
llm_response.result_chain = MessageChain().message(completion_text)
869871
elif refusal := getattr(choice.message, "refusal", None):
870872
refusal_text = self._normalize_content(refusal)

‎tests/test_openai_thinking_tags.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import pytest
2+
from openai.types.chat.chat_completion import ChatCompletion
3+
4+
from astrbot.core.provider.sources.openai_source import ProviderOpenAIOfficial
5+
6+
7+
@pytest.mark.asyncio
8+
@pytest.mark.parametrize(
9+
("content", "reasoning_field", "answer", "reasoning"),
10+
[
11+
("<think>First\nsecond</think>Answer", None, "Answer", "First\nsecond"),
12+
("<thinking>First\nsecond</thinking>Answer", None, "Answer", "First\nsecond"),
13+
("<thinking></thinking>Answer", None, "Answer", ""),
14+
("<thinking>Only reasoning</thinking>", None, "", "Only reasoning"),
15+
(
16+
"<think>One</think><thinking>Two</thinking>Answer",
17+
None,
18+
"Answer",
19+
"One\nTwo",
20+
),
21+
("Answer</think>", None, "Answer", None),
22+
("Answer</thinking> ", None, "Answer", None),
23+
(
24+
"Ordinary thinking and reasoning",
25+
None,
26+
"Ordinary thinking and reasoning",
27+
None,
28+
),
29+
("<thinker>Example</thinker>", None, "<thinker>Example</thinker>", None),
30+
("<thinking>Inline</thinking>Answer", "Structured", "Answer", "Structured"),
31+
("<thinking>Inline</thinking>Answer", "", "Answer", ""),
32+
("<thinking>Unfinished", None, "<thinking>Unfinished", None),
33+
(
34+
"<think>Example</thinking>Answer",
35+
None,
36+
"<think>Example</thinking>Answer",
37+
None,
38+
),
39+
],
40+
)
41+
async def test_openai_reasoning_tag_compatibility(
42+
content, reasoning_field, answer, reasoning
43+
):
44+
provider = ProviderOpenAIOfficial.__new__(ProviderOpenAIOfficial)
45+
provider.reasoning_key = "reasoning_content"
46+
message = {"role": "assistant", "content": content}
47+
if reasoning_field is not None:
48+
message["reasoning_content"] = reasoning_field
49+
completion = ChatCompletion.model_validate(
50+
{
51+
"id": "reasoning-tags",
52+
"object": "chat.completion",
53+
"created": 0,
54+
"model": "test-model",
55+
"choices": [
56+
{
57+
"index": 0,
58+
"finish_reason": "stop",
59+
"message": message,
60+
}
61+
],
62+
}
63+
)
64+
response = await provider._parse_openai_completion(completion, None)
65+
assert response.completion_text == answer
66+
if reasoning is not None:
67+
assert response.reasoning_content == reasoning
68+
else:
69+
assert not response.reasoning_content

0 commit comments

Comments
 (0)