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

updated heuristic tool parser for orphaned toolcall tags

Alishahryar185eed8a1bc8b
diff --git a/providers/nvidia_nim/utils/heuristic_tool_parser.py b/providers/nvidia_nim/utils/heuristic_tool_parser.py
index 01c91b0b..0c9b39cc 100644
--- a/providers/nvidia_nim/utils/heuristic_tool_parser.py
+++ b/providers/nvidia_nim/utils/heuristic_tool_parser.py
@@ -6,6 +6,13 @@ from typing import List, Dict, Any, Tuple
logger = logging.getLogger(__name__)
+# Some OpenAI-compatible backends/models occasionally leak internal sentinel tokens
+# into `delta.content` (e.g. "<|tool_call_end|>"). These should never be shown to
+# end users, and they can disrupt downstream parsing if left in place.
+_CONTROL_TOKEN_RE = re.compile(r"<\|[^|>]{1,80}\|>")
+_CONTROL_TOKEN_START = "<|"
+_CONTROL_TOKEN_END = "|>"
+
class ParserState(Enum):
TEXT = 1
@@ -35,6 +42,29 @@ class HeuristicToolParser:
r"<parameter=([^>]+)>(.*?)(?:</parameter>|$)", re.DOTALL
)
+ def _strip_control_tokens(self, text: str) -> str:
+ # Remove complete sentinel tokens. If a token is split across chunks it
+ # will be removed once the buffer contains the full token.
+ return _CONTROL_TOKEN_RE.sub("", text)
+
+ def _split_incomplete_control_token_tail(self) -> str:
+ """
+ If the buffer ends with an incomplete "<|...|>" sentinel token, keep that
+ fragment in the buffer and return the safe-to-emit prefix.
+
+ This prevents leaking raw sentinel fragments to the user when streaming.
+ """
+ start = self.buffer.rfind(_CONTROL_TOKEN_START)
+ if start == -1:
+ return ""
+ end = self.buffer.find(_CONTROL_TOKEN_END, start)
+ if end != -1:
+ return ""
+
+ prefix = self.buffer[:start]
+ self.buffer = self.buffer[start:]
+ return prefix
+
def feed(self, text: str) -> Tuple[str, List[Dict[str, Any]]]:
"""
Feed text into the parser.
@@ -44,6 +74,7 @@ class HeuristicToolParser:
detected_tool_calls: List of Anthropic-format tool_use blocks.
"""
self.buffer += text
+ self.buffer = self._strip_control_tokens(self.buffer)
detected_tools = []
filtered_output = ""
@@ -56,6 +87,13 @@ class HeuristicToolParser:
self.buffer = self.buffer[idx:]
self.state = ParserState.MATCHING_FUNCTION
else:
+ # Avoid emitting an incomplete "<|...|>" sentinel fragment if the
+ # token got split across streaming chunks.
+ safe_prefix = self._split_incomplete_control_token_tail()
+ if safe_prefix:
+ filtered_output += safe_prefix
+ break
+
filtered_output += self.buffer
self.buffer = ""
break
@@ -168,6 +206,7 @@ class HeuristicToolParser:
"""
Flush any remaining tool calls in the buffer.
"""
+ self.buffer = self._strip_control_tokens(self.buffer)
detected_tools = []
if self.state == ParserState.PARSING_PARAMETERS:
# Try to extract any partial parameters remaining in buffer
diff --git a/tests/test_parsers.py b/tests/test_parsers.py
index 72be5df8..7473d2c7 100644
--- a/tests/test_parsers.py
+++ b/tests/test_parsers.py
@@ -74,6 +74,44 @@ def test_heuristic_tool_parser_flush():
assert tools[0]["input"] == {"command": "ls -la"}
+def test_heuristic_tool_parser_strips_control_tokens():
+ p = HeuristicToolParser()
+ filtered, tools = p.feed("Hello <|tool_call_end|> world")
+ tools.extend(p.flush())
+
+ assert "<|tool_call_end|>" not in filtered
+ assert filtered == "Hello world"
+ assert tools == []
+
+
+def test_heuristic_tool_parser_strips_control_tokens_split_across_chunks():
+ p = HeuristicToolParser()
+ f1, t1 = p.feed("Hello <|tool_call_")
+ f2, t2 = p.feed("end|> world")
+ tools = t1 + t2 + p.flush()
+
+ assert "<|tool_call_end|>" not in (f1 + f2)
+ assert (f1 + f2) == "Hello world"
+ assert tools == []
+
+
+def test_heuristic_tool_parser_strips_control_tokens_inside_tool_text():
+ p = HeuristicToolParser()
+ text = (
+ "Before <|tool_calls_section_end|> ● <function=Grep>"
+ "<parameter=pattern>hi</parameter> After"
+ )
+ filtered, tools = p.feed(text)
+ tools.extend(p.flush())
+
+ assert "<|tool_calls_section_end|>" not in filtered
+ assert "Before" in filtered
+ assert "After" in filtered
+ assert len(tools) == 1
+ assert tools[0]["name"] == "Grep"
+ assert tools[0]["input"] == {"pattern": "hi"}
+
+
def test_interleaved_thinking_and_tools():
parser_think = ThinkTagParser()
parser_tool = HeuristicToolParser()