fix(reply): a tool call written in Qwen's XML keeps its argument types

Every <parameter> value was passed as text, so a delete confirmed with
confirm=true reached chatChannels as "true" and asked again. Values now take
the type the tool's input schema declares (boolean, integer, number, array,
object); a value that does not fit stays text. The roadmap's eval summary now
names the reply-length failure apart from the later passing runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 13:21:45 +09:00
parent 6d435caef0
commit 07c701be54
3 changed files with 50 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@@ -301,7 +301,34 @@ def _is_malformed_model_output(content: str) -> bool:
return False
def _extract_text_tool_call(content_field: str, known_names: set):
def _typed_xml_args(name: str, raw: dict, mcp_tools: Optional[dict] = None) -> dict:
"""Qwen's XML tool call carries every argument as text; give each the type its tool's input
schema declares (``confirm`` true is the boolean True, ``limit`` 5 the integer 5). A value that
does not read as its declared type, or has no declared type, stays text, for the tool to judge."""
schema = _get_tool_input_schema(name, mcp_tools) or {}
props = schema.get("properties") if isinstance(schema.get("properties"), dict) else {}
out: dict = {}
for key, text in raw.items():
kind = (props.get(key) or {}).get("type") if isinstance(props.get(key), dict) else None
value = text
try:
if kind == "boolean" and text.strip().lower() in ("true", "false"):
value = text.strip().lower() == "true"
elif kind == "integer":
value = int(text.strip())
elif kind == "number":
value = float(text.strip())
elif kind in ("array", "object"):
parsed = json.loads(text)
if isinstance(parsed, list if kind == "array" else dict):
value = parsed
except (ValueError, TypeError):
value = text
out[key] = value
return out
def _extract_text_tool_call(content_field: str, known_names: set, mcp_tools: Optional[dict] = None):
"""Parse a tool call out of a content-mode LLM response.
Small models emit several shapes when instructed to use text-based tool
@@ -332,8 +359,9 @@ def _extract_text_tool_call(content_field: str, known_names: set):
body = tc_block.group(1)
fn = re.match(r"<function=([^>\s]+)>(.*?)(?:</function>|$)", body, re.DOTALL)
if fn:
args = {k: v.strip() for k, v in re.findall(r"<parameter=([^>\s]+)>(.*?)</parameter>", fn.group(2), re.DOTALL)}
return fn.group(1).strip(), args, f"call_{uuid.uuid4().hex[:8]}"
name = fn.group(1).strip()
raw = {k: v.strip() for k, v in re.findall(r"<parameter=([^>\s]+)>(.*?)</parameter>", fn.group(2), re.DOTALL)}
return name, _typed_xml_args(name, raw, mcp_tools), f"call_{uuid.uuid4().hex[:8]}"
try:
data = json.loads(body)
name = str(data.get("name", "")).strip() if isinstance(data, dict) else ""
@@ -2046,7 +2074,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
# logic is unit-testable and shared across future callers.
content_field = msg.get("content", "") or ""
known_names = known_tool_names
name, args, tool_call_id = _extract_text_tool_call(content_field, known_names)
name, args, tool_call_id = _extract_text_tool_call(content_field, known_names, mcp_tools)
if name:
return name, args, tool_call_id

View File

@@ -313,5 +313,22 @@ class TestToolCallBlockAsText:
name, args, _ = _extract('<tool_call>\n{"name": "webSearch", "arguments": {"search_query": "x"}}\n</tool_call>')
assert name == "webSearch" and args == {"search_query": "x"}
def test_qwen_xml_arguments_get_the_types_the_tool_declares(self):
"""A delete confirmed in XML must reach the tool as the boolean True, or it asks again."""
import jarvis.reply.engine as engine_mod
content = ("<tool_call>\n<function=chatChannels>\n<parameter=action>\ndelete\n</parameter>\n"
"<parameter=confirm>\ntrue\n</parameter>\n<parameter=limit>\n5\n</parameter>\n"
"</function>\n</tool_call>")
name, args, _ = engine_mod._extract_text_tool_call(content, {"chatChannels"})
assert name == "chatChannels"
assert args == {"action": "delete", "confirm": True, "limit": 5}
def test_qwen_xml_values_that_do_not_fit_their_type_stay_text(self):
import jarvis.reply.engine as engine_mod
content = ("<tool_call><function=chatChannels><parameter=confirm>maybe</parameter>"
"<parameter=limit>a few</parameter></function></tool_call>")
_, args, _ = engine_mod._extract_text_tool_call(content, {"chatChannels"})
assert args == {"confirm": "maybe", "limit": "a few"}
def test_prose_mentioning_tags_is_not_a_call(self):
assert _extract("I would call <tool_call> if I could.")[0] is None