Skip to content

The agent that grows with you

refactor(agent): name the tool-call XML namespace prefix once

kshitijk4poorcf1344858063
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index 18d65346fd0..02f298bcde3 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -35,11 +35,13 @@ logger = logging.getLogger(__name__)
# Cap same-entry OAuth refreshes on a persistent auth failure, else a single-entry pool re-mints forever.
_MAX_AUTH_REFRESH_ATTEMPTS = 2
_TOOL_CALL_TAG_NAMES = ("tool_call", "tool_calls", "tool_result", "function_call", "function_calls")
+# Optional XML namespace prefix: some models serialize native tool calls as <ns:function_calls>.
+_NS_PREFIX = r"(?:[\w.-]+:)?"
_REASONING_BLOCK_PATTERNS = tuple(
re.compile(rf"<{name}>.*?</{name}>", re.DOTALL | re.IGNORECASE) for name in THINK_TAG_NAMES
)
_TOOL_CALL_BLOCK_PATTERNS = tuple(
- re.compile(rf"<(?:[\w.-]+:)?{name}\b[^>]*>.*?</(?:[\w.-]+:)?{name}>", re.DOTALL | re.IGNORECASE)
+ re.compile(rf"<{_NS_PREFIX}{name}\b[^>]*>.*?</{_NS_PREFIX}{name}>", re.DOTALL | re.IGNORECASE)
for name in _TOOL_CALL_TAG_NAMES
)
@@ -56,7 +58,7 @@ _ORPHAN_REASONING_TAG_PATTERN = re.compile(
rf'</?(?:{"|".join(THINK_TAG_NAMES)})>\s*', re.IGNORECASE
)
_STRAY_TOOL_CALL_CLOSER_PATTERN = re.compile(
- rf'</(?:(?:[\w.-]+:)?(?:{"|".join(_TOOL_CALL_TAG_NAMES)}|function))>\s*', re.IGNORECASE
+ rf'</(?:{_NS_PREFIX}(?:{"|".join(_TOOL_CALL_TAG_NAMES)}|function))>\s*', re.IGNORECASE
)
# A tool-call opener with no closer, or GLM-style argument markup
@@ -65,7 +67,7 @@ _STRAY_TOOL_CALL_CLOSER_PATTERN = re.compile(
# can't be recovered; strip from the block-boundary opener (or the line
# holding the first stray argument tag) to the end of the text.
_UNTERMINATED_TOOL_CALL_PATTERN = re.compile(
- rf'(?:^|\n)[ \t]*<(?:[\w.-]+:)?(?:{"|".join(_TOOL_CALL_TAG_NAMES)})\b[^>]*>.*$'
+ rf'(?:^|\n)[ \t]*<{_NS_PREFIX}(?:{"|".join(_TOOL_CALL_TAG_NAMES)})\b[^>]*>.*$'
r'|(?:^|\n)[^\n<]*</?arg_(?:key|value)\b.*$',
re.DOTALL | re.IGNORECASE,
)
diff --git a/cli.py b/cli.py
index 7c1fab09be5..4bf44d4ea72 100644
--- a/cli.py
+++ b/cli.py
@@ -185,7 +185,7 @@ _TOOL_CALL_TAGS = ("tool_call", "tool_calls", "tool_result", "function_call", "f
def _strip_reasoning_tags(text: str) -> str:
"""Strip reasoning blocks (closed, unterminated, orphan-close) and leaked tool-call XML from display text.
- Keep in sync with ``run_agent._strip_think_blocks`` and the stream consumer's think-tag sets.
+ Keep in sync with ``agent.agent_runtime_helpers.strip_think_blocks`` and the stream consumer's think-tag sets.
Also strips tool-call XML blocks some open models leak into visible content (``<tool_call>``,
``<function_calls>``, Gemma-style ``<function name="…">…</function>``). Ported from
diff --git a/tests/agent/test_strip_reasoning_tags_cli.py b/tests/agent/test_strip_reasoning_tags_cli.py
index df1c645cfcd..cf8557544fb 100644
--- a/tests/agent/test_strip_reasoning_tags_cli.py
+++ b/tests/agent/test_strip_reasoning_tags_cli.py
@@ -54,11 +54,6 @@ class TestToolCallStripping:
assert out.strip() == "Waiting."
-
-
-
-
-
def test_empty_string(self):
assert _strip_reasoning_tags("") == ""