Skip to content

Commit fe2b514

Browse files
MiaAI-Labashhart
authored andcommitted
fix: the CUDA server types Qwen tool parameters by the tool's schema
The Mac server has decoded Qwen `<parameter=k>` values by the tool's JSON schema since 0.3.5 (#28); the CUDA server's parser still returned every value as a string, so an array parameter reached the client as "[\"a\", \"b\"]" instead of ["a", "b"] and strict clients rejected the call. cuda/reply_text.parse_tool_calls now runs each value through the same tool_parameters.decode_parameter the Mac server uses: array, object, boolean, integer, number and null parameters decode when the text is valid JSON of that type, anything else keeps its exact text.
1 parent 756a125 commit fe2b514

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎src/tensorfold/cuda/reply_text.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from tensorfold.server.stopping import StopPolicy
1111
from tensorfold.server.tools import parse_glm_tool_call_block
12+
from tensorfold.tool_parameters import decode_parameter, parameter_schemas
1213

1314
_CALL_OPEN, _CALL_CLOSE = "<tool_call>", "</tool_call>"
1415
_TOOL_CALL_BLOCK_RE = re.compile(r"<tool_call>\s*(.*?)\s*</tool_call>", re.IGNORECASE | re.DOTALL)
@@ -102,6 +103,7 @@ def parse_tool_calls(text: str, tools: list[dict[str, Any]], *, max_calls: int |
102103
if not tools:
103104
return text, None
104105
known = {_tool_name(t).lower(): _tool_name(t) for t in tools}
106+
schemas = parameter_schemas(tools)
105107
calls: list[dict[str, Any]] = []
106108
residue: list[str] = []
107109
cursor = 0
@@ -126,7 +128,11 @@ def parse_tool_calls(text: str, tools: list[dict[str, Any]], *, max_calls: int |
126128
if max_calls is not None and _TOOL_PARAMETER_BLOCK_RE.sub("", m.group(2)).strip():
127129
continue
128130
name = m.group(1).strip()
129-
args = {p.group(1).strip(): p.group(2) for p in _TOOL_PARAMETER_BLOCK_RE.finditer(m.group(2))}
131+
# typed parameters (array, object, number...) decode per the tool's schema, as the Mac server does
132+
props = schemas.get(name.lower(), {})
133+
args = {key: decode_parameter(value, props.get(key, {}))
134+
for key, value in ((p.group(1).strip(), p.group(2))
135+
for p in _TOOL_PARAMETER_BLOCK_RE.finditer(m.group(2)))}
130136
else:
131137
glm = parse_glm_tool_call_block(block, tools, complete=max_calls is not None)
132138
if glm is not None:

‎tests/test_glm_tool_calls.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ def test_a_tool_the_client_did_not_offer_stays_in_the_reply(parser):
7979

8080
@PARSERS
8181
def test_qwen_function_blocks_are_unchanged(parser):
82-
# since 0.3.5 the Mac server types Qwen parameters by the tool's schema; the CUDA server keeps them as text
83-
days = "3" if parser is parse_tool_calls else 3
82+
# both servers type Qwen parameters by the tool's schema
8483
text = ("<tool_call>\n<function=get_weather>\n<parameter=city>\nParis\n</parameter>\n"
8584
"<parameter=days>\n3\n</parameter>\n</function>\n</tool_call>")
86-
assert _parse(parser, text) == ("", [("get_weather", {"city": "Paris", "days": days})])
85+
assert _parse(parser, text) == ("", [("get_weather", {"city": "Paris", "days": 3})])
8786

8887

8988
@PARSERS

‎tests/test_tool_parameter_types.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
from tensorfold.cuda.reply_text import parse_tool_calls as parse_cuda_tool_calls
56
from tensorfold.engine.tool_draft import ToolCallStreamer
67
from tensorfold.server.http import parse_tool_calls_from_content
78

@@ -37,6 +38,9 @@ def test_parameter_type_matches_schema_in_both_paths(kind, value, expected, step
3738
text = f"<tool_call>\n<function=question>\n<parameter=questions>\n{value}\n</parameter>\n</function>\n</tool_call>"
3839
_, calls = parse_tool_calls_from_content(text, tools)
3940
assert json.loads(calls[0]["function"]["arguments"]) == {"questions": expected}
41+
for max_calls in (None, 1): # the CUDA server's parser, the same types
42+
_, calls = parse_cuda_tool_calls(text, tools, max_calls=max_calls)
43+
assert json.loads(calls[0]["function"]["arguments"]) == {"questions": expected}
4044
streamer = ToolCallStreamer(tools)
4145
deltas = []
4246
for n in range(1, len(text) + 1, step):

0 commit comments

Comments
 (0)