Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Fixed orphan </think> tags

Alishahryar158f556f8bd42
diff --git a/providers/utils/think_parser.py b/providers/utils/think_parser.py
index c356cd34..d78c0061 100644
--- a/providers/utils/think_parser.py
+++ b/providers/utils/think_parser.py
@@ -67,18 +67,33 @@ class ThinkTagParser:
def _parse_outside_think(self) -> Optional[ContentChunk]:
"""Parse content outside think tags."""
think_start = self._buffer.find(self.OPEN_TAG)
+ orphan_close = self._buffer.find(self.CLOSE_TAG)
+
+ # Handle orphan </think> - strip it (Step Fun AI sends reasoning via
+ # reasoning_content but may leak closing tags in content)
+ if orphan_close != -1 and (think_start == -1 or orphan_close < think_start):
+ pre_orphan = self._buffer[:orphan_close]
+ self._buffer = self._buffer[orphan_close + self.CLOSE_TAG_LEN :]
+ if pre_orphan:
+ return ContentChunk(ContentType.TEXT, pre_orphan)
+ # Continue parsing after stripping orphan tag
+ return self._parse_outside_think()
if think_start == -1:
# No tag found - check for partial tag at end
- # We buffer any trailing '<' and subsequent characters that could be part of <think>
+ # We buffer any trailing '<' and subsequent characters that could be part of <think> or </think>
last_bracket = self._buffer.rfind("<")
- if (
- last_bracket != -1
- and len(self._buffer) - last_bracket < self.OPEN_TAG_LEN
- ):
- # Check if the partial string could be the start of <think>
+ if last_bracket != -1:
potential_tag = self._buffer[last_bracket:]
- if self.OPEN_TAG.startswith(potential_tag):
+ tag_len = len(potential_tag)
+ # Check if could be partial <think> or </think>
+ if (
+ tag_len < self.OPEN_TAG_LEN
+ and self.OPEN_TAG.startswith(potential_tag)
+ ) or (
+ tag_len < self.CLOSE_TAG_LEN
+ and self.CLOSE_TAG.startswith(potential_tag)
+ ):
emit = self._buffer[:last_bracket]
self._buffer = self._buffer[last_bracket:]
if emit:
diff --git a/tests/test_parsers.py b/tests/test_parsers.py
index 6aa30c82..19904d2a 100644
--- a/tests/test_parsers.py
+++ b/tests/test_parsers.py
@@ -223,3 +223,82 @@ def test_text_between_params_lost():
assert "text1" in filtered
assert "text2" in filtered
assert tools[0]["input"] == {"a": "1", "b": "2"}
+
+
+# --- Orphan </think> Tag Tests (Step Fun AI compatibility) ---
+
+
+def test_orphan_close_tag_stripped():
+ """Orphan </think> without opening tag should be stripped."""
+ parser = ThinkTagParser()
+ chunks = list(parser.feed("Hello </think> world"))
+
+ # Should get one text chunk with orphan tag stripped
+ assert len(chunks) == 2
+ assert chunks[0].type == ContentType.TEXT
+ assert chunks[0].content == "Hello "
+ assert chunks[1].type == ContentType.TEXT
+ assert chunks[1].content == " world"
+
+
+def test_orphan_close_tag_at_start():
+ """Orphan </think> at start should be stripped."""
+ parser = ThinkTagParser()
+ chunks = list(parser.feed("</think>Hello world"))
+
+ assert len(chunks) == 1
+ assert chunks[0].type == ContentType.TEXT
+ assert chunks[0].content == "Hello world"
+
+
+def test_orphan_close_tag_at_end():
+ """Orphan </think> at end should be stripped."""
+ parser = ThinkTagParser()
+ chunks = list(parser.feed("Hello world</think>"))
+
+ assert len(chunks) == 1
+ assert chunks[0].type == ContentType.TEXT
+ assert chunks[0].content == "Hello world"
+
+
+def test_multiple_orphan_close_tags():
+ """Multiple orphan </think> tags should all be stripped."""
+ parser = ThinkTagParser()
+ chunks = list(parser.feed("a</think>b</think>c"))
+
+ text = "".join(c.content for c in chunks if c.type == ContentType.TEXT)
+ assert text == "abc"
+ assert "</think>" not in text
+
+
+def test_orphan_close_tag_streaming():
+ """Orphan </think> split across chunks should be stripped."""
+ parser = ThinkTagParser()
+
+ # Feed partial orphan tag
+ chunks1 = list(parser.feed("Hello </thi"))
+ assert len(chunks1) == 1
+ assert chunks1[0].content == "Hello "
+
+ # Complete the orphan tag
+ chunks2 = list(parser.feed("nk> world"))
+ assert len(chunks2) == 1
+ assert chunks2[0].type == ContentType.TEXT
+ assert chunks2[0].content == " world"
+
+
+def test_orphan_close_with_valid_think_pair():
+ """Orphan </think> followed by valid <think>...</think> pair."""
+ parser = ThinkTagParser()
+ chunks = list(parser.feed("a</think>b<think>thinking</think>c"))
+
+ types = [c.type for c in chunks]
+ contents = [c.content for c in chunks]
+
+ assert ContentType.TEXT in types
+ assert ContentType.THINKING in types
+ # Text should be "ab" and "c", thinking should be "thinking"
+ text_content = "".join(c.content for c in chunks if c.type == ContentType.TEXT)
+ think_content = "".join(c.content for c in chunks if c.type == ContentType.THINKING)
+ assert text_content == "abc"
+ assert think_content == "thinking"