Run 35B MoE models in ~2.5 GB of RAM. Weights stream from SSD; a trained router predicts loads a step ahead. Mac, iPhone, Android, Windows supported
Python
feat(server): OpenAI-compatible tool/function calling
Demetrios Agourakisef8dc2c56679
diff --git a/python/src/edge0/engine/ling.py b/python/src/edge0/engine/ling.pyindex 936803c..810ed5c 100644--- a/python/src/edge0/engine/ling.py+++ b/python/src/edge0/engine/ling.py@@ -343,13 +343,15 @@ class Ling8BEngine(Edge0Engine):).from_string(src)return self._chat_tpl- def encode_chat(self, messages, think=None) -> list:+ def encode_chat(self, messages, think=None, tools=None) -> list:"""Tokenize chat messages with the deployment chat template.``think`` mirrors THINK_MODE: True renders "detailed thinking on"(the model answers with a reasoning preamble), False renders"detailed thinking off" (direct answer). Defaults to the- engine's ``think`` flag.+ engine's ``think`` flag. ``tools`` is the OpenAI-style function+ list from the request, forwarded to the template's own ``# Tools``+ system-prompt section and ``<tool_call>`` format instructions."""if think is None:think = self.think@@ -366,8 +368,15 @@ class Ling8BEngine(Edge0Engine):messages=msgs,add_generation_prompt=True,enable_thinking=bool(think),- tools=None,+ tools=tools,)# transformers encode would prepend/append special tokens by# default; the template text is already complete.return self._tok.encode(text, add_special_tokens=False)++ def parse_tool_calls(self, text: str):+ """Split generated ``text`` into (content, OpenAI tool_calls) per+ this checkpoint's chat template ``<tool_call>name<arg_key>...``+ dialect (see edge0.server.tool_calls)."""+ from edge0.server.tool_calls import parse_ling_tool_calls+ return parse_ling_tool_calls(text)diff --git a/python/src/edge0/engine/qwen.py b/python/src/edge0/engine/qwen.pyindex b22a107..6f8df55 100644--- a/python/src/edge0/engine/qwen.py+++ b/python/src/edge0/engine/qwen.py@@ -113,6 +113,13 @@ class Qwen35Engine(Edge0Engine):name = "edge0-35b"+ def parse_tool_calls(self, text: str):+ """Split generated ``text`` into (content, OpenAI tool_calls) per+ this checkpoint's chat template ``<tool_call><function=...>``+ dialect (see edge0.server.tool_calls)."""+ from edge0.server.tool_calls import parse_qwen_tool_calls+ return parse_qwen_tool_calls(text)+def __init__(self, model_dir: str, cfg, tokenizer=None):# NAN_BANG_COLLAPSE_FIX parity (engine/ling.py): hidden clip default# 1000 unless the deployer overrides QWEN_HIDDEN_CLIP explicitly.diff --git a/python/src/edge0/server/app.py b/python/src/edge0/server/app.pyindex 9d94b1b..02ddff9 100644--- a/python/src/edge0/server/app.py+++ b/python/src/edge0/server/app.py@@ -47,6 +47,21 @@ def _split_think(text: str, think: bool):return "", text+def _extract_tool_calls(server: QueueServer, req, content: str):+ """(content, tool_calls) after stripping any ``<tool_call>`` block the+ engine's chat template asked the model to emit. ``tool_calls`` is None+ when the engine has no parser (unsupported family) or the request+ didn't ask for tools, or when no call was found in ``content``."""+ if req.tool_choice == "none" or (not req.tools+ and req.tool_choice != "required"):+ return content, None+ parse = getattr(server.engine, "parse_tool_calls", None)+ if not callable(parse):+ return content, None+ content, calls = parse(content)+ return content, (calls or None)++def _chat_once(server: QueueServer, payload: dict):req = parse_chat_request(payload)tokens, meta = server.chat(req)@@ -54,6 +69,14 @@ def _chat_once(server: QueueServer, payload: dict):think = bool(req.enable_thinking if req.enable_thinking is not Noneelse getattr(server.engine, "think", False))reasoning, content = _split_think(text, think)+ content, tool_calls = _extract_tool_calls(server, req, content)+ message = {+ "role": "assistant",+ "content": content,+ "reasoning_content": reasoning,+ }+ if tool_calls:+ message["tool_calls"] = tool_callsreturn {"id": f"chatcmpl-{int(time.time() * 1000)}","object": "chat.completion",@@ -61,12 +84,8 @@ def _chat_once(server: QueueServer, payload: dict):"model": server.model_name,"choices": [{"index": 0,- "message": {- "role": "assistant",- "content": content,- "reasoning_content": reasoning,- },- "finish_reason": "stop",+ "message": message,+ "finish_reason": "tool_calls" if tool_calls else "stop",}],"usage": meta["usage"],}@@ -78,8 +97,20 @@ def _chat_stream(server: QueueServer, payload: dict):finished = object()request_id = f"chatcmpl-{int(time.time() * 1000)}"created = int(time.time())+ # Tool-call XML (<tool_call>...</tool_call>) must not leak into content+ # deltas verbatim -- the whole point of #115 is that a client asking+ # for tools gets a parsed message.tool_calls, not raw template markup.+ # Detecting a block incrementally, token by token, needs a lookback+ # buffer for a tag that can straddle token boundaries; simpler and+ # just as correct: buffer the full response and parse once, same as+ # the non-streaming path. Requests with no tools (the common case)+ # keep the existing immediate per-token streaming, unchanged.+ buffering = bool(req.tools) and req.tool_choice != "none" and callable(+ getattr(server.engine, "parse_tool_calls", None))def on_token(tid: int):+ if buffering:+ returntext = decode_tokens(server.engine, [tid])events.put(sse_format({"id": request_id, "object": "chat.completion.chunk",@@ -91,12 +122,27 @@ def _chat_stream(server: QueueServer, payload: dict):def produce():try:- _, meta = server.chat(req, on_token=on_token)+ tokens, meta = server.chat(req, on_token=on_token)+ finish_reason = "stop"+ delta = {}+ if buffering:+ text = decode_tokens(server.engine, tokens)+ think = bool(+ req.enable_thinking if req.enable_thinking is not None+ else getattr(server.engine, "think", False))+ _, content = _split_think(text, think)+ content, tool_calls = _extract_tool_calls(+ server, req, content)+ if tool_calls:+ delta = {"content": content, "tool_calls": tool_calls}+ finish_reason = "tool_calls"+ else:+ delta = {"content": content}events.put(sse_format({"id": request_id, "object": "chat.completion.chunk","created": created, "model": server.model_name,- "choices": [{"index": 0, "delta": {},- "finish_reason": "stop"}],+ "choices": [{"index": 0, "delta": delta,+ "finish_reason": finish_reason}],"usage": meta["usage"],}).encode("utf-8"))events.put(b"data: [DONE]\n\n")diff --git a/python/src/edge0/server/chat.py b/python/src/edge0/server/chat.pyindex a1502bd..b39080e 100644--- a/python/src/edge0/server/chat.py+++ b/python/src/edge0/server/chat.py@@ -21,6 +21,9 @@ from edge0.engine.base import Edge0Engineclass ChatMessage:role: strcontent: str+ tool_calls: list[dict] | None = None+ tool_call_id: str | None = None+ name: str | None = None@dataclass@@ -34,6 +37,8 @@ class ChatRequest:seed: int | None = Nonestream: bool = Falseenable_thinking: bool | None = None+ tools: list[dict] | None = None+ tool_choice: Any = Noneraw: dict[str, Any] = field(default_factory=dict)@@ -41,11 +46,17 @@ def parse_chat_request(payload: dict) -> ChatRequest:msgs = []for m in payload.get("messages", []):role = str(m.get("role", "user"))- content = m.get("content", "")+ content = m.get("content") or "" # tool-call-only assistant turns+ # send content: null, not an omitted keyif isinstance(content, list): # multi-part content: join text partscontent = "".join(p.get("text", "") for p in content if isinstance(p, dict))- msgs.append(ChatMessage(role=role, content=str(content)))+ msgs.append(ChatMessage(+ role=role, content=str(content),+ tool_calls=m.get("tool_calls"),+ tool_call_id=m.get("tool_call_id"),+ name=m.get("name"),+ ))return ChatRequest(model=str(payload.get("model", "")),messages=msgs,@@ -56,6 +67,8 @@ def parse_chat_request(payload: dict) -> ChatRequest:seed=payload.get("seed"),stream=bool(payload.get("stream", False)),enable_thinking=payload.get("enable_thinking"),+ tools=payload.get("tools"),+ tool_choice=payload.get("tool_choice"),raw=payload,)@@ -68,6 +81,16 @@ class ChatSession:self.req = reqself._tok = engine._tok+ def _tools(self) -> list[dict] | None:+ """Tool definitions to forward to the chat template, or None if+ the request has none or explicitly disabled calling+ (``tool_choice: "none"``). Forcing a specific function or+ ``tool_choice: "required"`` would need constrained decoding,+ which isn't implemented; both currently behave like "auto"."""+ if not self.req.tools or self.req.tool_choice == "none":+ return None+ return self.req.tools+def prompt_ids(self) -> list[int]:tok = self._tok# Families with a vendored chat template@@ -82,7 +105,8 @@ class ChatSession:if think is None:think = getattr(self.engine, "think", False)return encode(- [m.__dict__ for m in self.req.messages], think=bool(think))+ [m.__dict__ for m in self.req.messages], think=bool(think),+ tools=self._tools())if hasattr(tok, "apply_chat_template"):# The qwen35 template supports ``enable_thinking``: False# renders the canonical no-think prompt — an EMPTY think@@ -101,7 +125,7 @@ class ChatSession:try:text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True,- enable_thinking=bool(think))+ enable_thinking=bool(think), tools=self._tools())except TypeError:# tokenizer template without the kwarg: plain rendertry:diff --git a/python/src/edge0/server/tool_calls.py b/python/src/edge0/server/tool_calls.pynew file mode 100644index 0000000..4c89f10--- /dev/null+++ b/python/src/edge0/server/tool_calls.py@@ -0,0 +1,116 @@+"""Parse the XML tool-call blocks each model family's chat template asks+it to emit, and convert them to the OpenAI ``message.tool_calls`` schema.++The two supported checkpoints use two different XML dialects (confirmed+against the real ``chat_template.jinja`` shipped with each checkpoint,+not assumed):++* **edge0-8b (Ling)** — ``<tool_call>{name}<arg_key>k</arg_key>+ <arg_value>v</arg_value>...</arg_call>``, one ``arg_key``/``arg_value``+ pair per argument.+* **edge0-35b (Qwen3.5)** — ``<tool_call>\\n<function={name}>\\n+ <parameter={key}>\\nvalue\\n</parameter>\\n...</function>\\n</tool_call>``.++Both formats can appear more than once per turn (multiple tool calls in+one response); each ``<tool_call>...</tool_call>`` block becomes one+OpenAI tool-call entry with a synthesized ``id``.+"""++from __future__ import annotations++import json+import re+import uuid++_TOOL_CALL_BLOCK = re.compile(r"<tool_call>(.*?)</tool_call>", re.DOTALL)++_LING_ARG = re.compile(+ r"<arg_key>(.*?)</arg_key>\s*<arg_value>(.*?)</arg_value>", re.DOTALL)++_QWEN_FUNCTION = re.compile(+ r"\s*<function=([^>]+)>\s*(.*?)\s*</function>\s*", re.DOTALL)+_QWEN_PARAM = re.compile(+ r"<parameter=([^>]+)>\n(.*?)\n</parameter>", re.DOTALL)+++def _coerce(raw: str):+ """A bare arg value is a string unless it parses as JSON (numbers,+ booleans, null, objects, arrays) -- mirrors the template's own+ ``v if v is string else v|tojson`` split, in reverse."""+ try:+ return json.loads(raw)+ except (json.JSONDecodeError, ValueError):+ return raw+++def _tool_call_id() -> str:+ return f"call_{uuid.uuid4().hex[:24]}"+++def _to_openai(name: str, args: dict) -> dict:+ return {+ "id": _tool_call_id(),+ "type": "function",+ "function": {"name": name.strip(), "arguments": json.dumps(args)},+ }+++def _split(text: str, parse_block) -> tuple[str | None, list[dict]]:+ """Shared block extraction: find every ``<tool_call>...</tool_call>``,+ remove it from the content, and parse its body with ``parse_block``.+ Blocks ``parse_block`` cannot make sense of are left in ``content``+ rather than silently dropped."""+ calls = []+ leftover_spans = []+ pos = 0+ for m in _TOOL_CALL_BLOCK.finditer(text):+ parsed = parse_block(m.group(1))+ if parsed is None:+ continue+ calls.append(_to_openai(*parsed))+ leftover_spans.append((pos, m.start()))+ pos = m.end()+ leftover_spans.append((pos, len(text)))+ content = "".join(text[a:b] for a, b in leftover_spans).strip()+ if calls and not content:+ return None, calls+ return content, calls+++def _parse_ling_block(body: str):+ name_match = re.match(r"^([^<]+)", body)+ if not name_match:+ return None+ name = name_match.group(1).strip()+ if not name:+ return None+ args = {k.strip(): _coerce(v) for k, v in _LING_ARG.findall(body)}+ return name, args+++def parse_ling_tool_calls(text: str) -> tuple[str | None, list[dict]]:+ """Split ``text`` into (remaining content, OpenAI tool_calls) for the+ edge0-8b (Ling) XML dialect. Returns ``(text.strip(), [])`` when no+ ``<tool_call>`` block is present -- same stripping convention as+ ``app._split_think``."""+ return _split(text, _parse_ling_block)+++def _parse_qwen_block(body: str):+ fn_match = _QWEN_FUNCTION.match(body)+ if not fn_match:+ return None+ name = fn_match.group(1).strip()+ if not name:+ return None+ params = fn_match.group(2)+ args = {k.strip(): _coerce(v) for k, v in _QWEN_PARAM.findall(params)}+ return name, args+++def parse_qwen_tool_calls(text: str) -> tuple[str | None, list[dict]]:+ """Split ``text`` into (remaining content, OpenAI tool_calls) for the+ edge0-35b (Qwen3.5) XML dialect. Returns ``(text.strip(), [])`` when+ no ``<tool_call>`` block is present -- same stripping convention as+ ``app._split_think``."""+ return _split(text, _parse_qwen_block)diff --git a/python/tests/test_server.py b/python/tests/test_server.pyindex bad235b..e1cd6e3 100644--- a/python/tests/test_server.py+++ b/python/tests/test_server.py@@ -51,8 +51,10 @@ class FakeTok:def apply_chat_template(self, messages, tokenize=False,add_generation_prompt=True,- enable_thinking=False):+ enable_thinking=False, tools=None):text = "<tpl>" + json.dumps(messages)+ if tools:+ text += "<tools>" + json.dumps(tools)if add_generation_prompt:text += "<|im_start|>assistant\n"return text@@ -113,6 +115,42 @@ class FakeEngine:self.pos = 0+class ToolCallTok(FakeTok):+ """Decodes generated tokens to a Ling-style ``<tool_call>`` block, the+ same shape the real edge0-8b template asks the model to emit."""++ def decode(self, tokens):+ return ("<tool_call>calculator\n"+ "<arg_key>expression</arg_key>\n"+ "<arg_value>2 + 2</arg_value>\n"+ "</tool_call>")+++class ToolCallEngine(FakeEngine):+ """FakeEngine + the real Ling tool-call parser, so tests exercise the+ actual edge0.server.tool_calls integration, not a re-implementation+ of it."""++ def __init__(self, tok=None):+ super().__init__(tok=tok or ToolCallTok())++ def parse_tool_calls(self, text: str):+ from edge0.server.tool_calls import parse_ling_tool_calls+ return parse_ling_tool_calls(text)+++_TOOLS = [{+ "type": "function",+ "function": {+ "name": "calculator",+ "description": "Calculate an arithmetic expression.",+ "parameters": {"type": "object",+ "properties": {"expression": {"type": "string"}},+ "required": ["expression"]},+ },+}]++def _req(**kw) -> ChatRequest:payload = {"model": "fake",@@ -155,6 +193,26 @@ def test_parse_stream_flag_and_sampling():assert req.stream is True+def test_parse_preserves_tools_and_tool_choice():+ req = _req(tools=_TOOLS, tool_choice="auto")+ assert req.tools == _TOOLS+ assert req.tool_choice == "auto"+++def test_parse_message_tool_calls_and_tool_role_roundtrip():+ req = parse_chat_request({"messages": [+ {"role": "user", "content": "compute 2+2"},+ {"role": "assistant", "content": None, "tool_calls": [+ {"id": "call_1", "type": "function",+ "function": {"name": "calculator",+ "arguments": '{"expression": "2 + 2"}'}}]},+ {"role": "tool", "tool_call_id": "call_1", "content": "4"},+ ]})+ assert req.messages[1].tool_calls[0]["function"]["name"] == "calculator"+ assert req.messages[1].content == "" # null, not the string "None"+ assert req.messages[2].tool_call_id == "call_1"++# ---- sse / decode ---------------------------------------------------------@@ -293,6 +351,105 @@ def test_chat_once_response_shape():assert out["usage"]["completion_tokens"] == 3+# ---- tool calls (#115) -----------------------------------------------------+++def test_chat_once_ignores_tool_call_text_without_tools_field():+ """No ``tools`` in the request -> the raw <tool_call> XML the fake+ generation returns is left as plain content, exactly today's+ (buggy) behavior -- this only changes when the client opts in."""+ srv = QueueServer(ToolCallEngine())+ out = _chat_once(srv, {"messages": [{"role": "user", "content": "hi"}]})+ ch = out["choices"][0]+ assert "tool_calls" not in ch["message"]+ assert "<tool_call>" in ch["message"]["content"]+ assert ch["finish_reason"] == "stop"+++def test_chat_once_parses_tool_calls_when_requested():+ srv = QueueServer(ToolCallEngine())+ out = _chat_once(srv, {+ "messages": [{"role": "user", "content": "compute 2 + 2"}],+ "tools": _TOOLS, "tool_choice": "auto",+ })+ ch = out["choices"][0]+ assert ch["message"]["content"] is None+ assert ch["finish_reason"] == "tool_calls"+ calls = ch["message"]["tool_calls"]+ assert len(calls) == 1+ fn = calls[0]["function"]+ assert fn["name"] == "calculator"+ assert json.loads(fn["arguments"]) == {"expression": "2 + 2"}+ assert calls[0]["type"] == "function"+ assert calls[0]["id"].startswith("call_")+++def test_chat_once_tool_choice_none_suppresses_parsing():+ srv = QueueServer(ToolCallEngine())+ out = _chat_once(srv, {+ "messages": [{"role": "user", "content": "compute 2 + 2"}],+ "tools": _TOOLS, "tool_choice": "none",+ })+ ch = out["choices"][0]+ assert "tool_calls" not in ch["message"]+ assert "<tool_call>" in ch["message"]["content"]+++def test_chat_once_tools_requested_but_engine_cannot_parse():+ """An engine with no parse_tool_calls (unsupported family) must not+ crash a tools-enabled request; it just can't extract structured+ calls, same as today."""+ srv = QueueServer(FakeEngine())+ out = _chat_once(srv, {+ "messages": [{"role": "user", "content": "hi"}], "tools": _TOOLS,+ })+ ch = out["choices"][0]+ assert "tool_calls" not in ch["message"]+ assert ch["finish_reason"] == "stop"+++def test_chat_stream_tools_requested_buffers_and_emits_final_tool_calls():+ srv = QueueServer(ToolCallEngine())+ events = _chat_stream(srv, {+ "messages": [{"role": "user", "content": "compute 2 + 2"}],+ "stream": True, "tools": _TOOLS, "tool_choice": "auto",+ })+ body = b"".join(events)+ frames = [e for e in body.decode("utf-8").split("\n\n") if e]+ assert frames[-1] == "data: [DONE]"+ chunks = [json.loads(e[6:]) for e in frames[:-1]]+ # No raw <tool_call> XML in any per-token delta -- buffered, not+ # streamed token-by-token, unlike the no-tools path.+ for c in chunks[:-1]:+ assert "<tool_call>" not in json.dumps(c)+ final = chunks[-1]["choices"][0]+ assert final["finish_reason"] == "tool_calls"+ assert final["delta"]["content"] is None+ calls = final["delta"]["tool_calls"]+ assert calls[0]["function"]["name"] == "calculator"+ assert json.loads(calls[0]["function"]["arguments"]) == {+ "expression": "2 + 2"}+++def test_chat_stream_without_tools_keeps_immediate_per_token_deltas():+ """Regression guard: requests with no tools must keep the existing+ immediate per-token streaming -- buffering is scoped to tool-enabled+ requests only."""+ srv = QueueServer(ToolCallEngine())+ events = _chat_stream(srv, {+ "messages": [{"role": "user", "content": "hi"}], "stream": True,+ })+ body = b"".join(events)+ frames = [e for e in body.decode("utf-8").split("\n\n") if e]+ chunks = [json.loads(e[6:]) for e in frames[:-1]]+ deltas = [c["choices"][0]["delta"].get("content")+ for c in chunks if c["choices"][0]["delta"]]+ # unchanged from the no-tools path: whatever decode_tokens([tid])+ # returns per call, not the whole buffered response+ assert len(deltas) == 3+ assert chunks[-1]["choices"][0]["finish_reason"] == "stop"++def test_chat_stream_events_sequence():srv = QueueServer(FakeEngine())events = _chat_stream(srv, {diff --git a/python/tests/test_tool_calls.py b/python/tests/test_tool_calls.pynew file mode 100644index 0000000..e4cd469--- /dev/null+++ b/python/tests/test_tool_calls.py@@ -0,0 +1,123 @@+"""edge0.server.tool_calls: XML tool-call parsing for both checkpoint+families.++The fixture strings below are not invented: they were captured by+rendering the *real* ``chat_template.jinja`` shipped with each checkpoint+(edge0-8b / Ling, edge0-35b / Qwen3.5) with jinja2 directly, for an+assistant message carrying one ``tool_calls`` entry, and copying the+generated span verbatim -- including the Ling template's own leading-+whitespace quirk before the first ``<arg_key>`` (a real jinja2 whitespace-+control artifact in that template, not a typo here). This guards against+inventing a plausible-looking XML dialect that the real templates don't+actually produce.+"""++from __future__ import annotations++import json++from edge0.server.tool_calls import parse_ling_tool_calls, parse_qwen_tool_calls++# Captured from edge0-8b/chat_template.jinja rendered with+# tools=[calculator] and one assistant tool_calls entry.+LING_REAL_SPAN = (+ "<tool_call>calculator\n"+ " <arg_key>expression</arg_key>\n"+ "<arg_value>2 + 2</arg_value>\n"+ "</tool_call>"+)++# Captured from edge0-35b/chat_template.jinja's own in-prompt format+# example (example_function_name / example_parameter_1/2, the second+# spanning multiple lines -- the format the "IMPORTANT" reminder block+# documents to the model).+QWEN_REAL_SPAN = (+ "<tool_call>\n"+ "<function=example_function_name>\n"+ "<parameter=example_parameter_1>\n"+ "value_1\n"+ "</parameter>\n"+ "<parameter=example_parameter_2>\n"+ "This is the value for the second parameter\n"+ "that can span\n"+ "multiple lines\n"+ "</parameter>\n"+ "</function>\n"+ "</tool_call>"+)+++def test_ling_parses_real_template_span():+ content, calls = parse_ling_tool_calls(LING_REAL_SPAN)+ assert content is None+ assert len(calls) == 1+ fn = calls[0]["function"]+ assert fn["name"] == "calculator"+ assert json.loads(fn["arguments"]) == {"expression": "2 + 2"}+ assert calls[0]["type"] == "function"+ assert calls[0]["id"].startswith("call_")+++def test_qwen_parses_real_template_span_with_multiline_value():+ content, calls = parse_qwen_tool_calls(QWEN_REAL_SPAN)+ assert content is None+ assert len(calls) == 1+ fn = calls[0]["function"]+ assert fn["name"] == "example_function_name"+ args = json.loads(fn["arguments"])+ assert args["example_parameter_1"] == "value_1"+ assert args["example_parameter_2"] == (+ "This is the value for the second parameter\n"+ "that can span\nmultiple lines")+++def test_no_tool_call_leaves_content_unchanged_stripped():+ for parser in (parse_ling_tool_calls, parse_qwen_tool_calls):+ content, calls = parser(" just a plain answer ")+ assert content == "just a plain answer"+ assert calls == []+++def test_prose_before_tool_call_is_preserved_as_content():+ text = "Sure, let me compute that.\n" + LING_REAL_SPAN+ content, calls = parse_ling_tool_calls(text)+ assert content == "Sure, let me compute that."+ assert len(calls) == 1+++def test_multiple_tool_calls_in_one_response():+ text = LING_REAL_SPAN + "\n" + (+ "<tool_call>calculator\n<arg_key>expression</arg_key>\n"+ "<arg_value>3 * 3</arg_value>\n</tool_call>")+ content, calls = parse_ling_tool_calls(text)+ assert content is None+ assert len(calls) == 2+ assert json.loads(calls[0]["function"]["arguments"]) == {+ "expression": "2 + 2"}+ assert json.loads(calls[1]["function"]["arguments"]) == {+ "expression": "3 * 3"}+ # each call gets its own id+ assert calls[0]["id"] != calls[1]["id"]+++def test_qwen_malformed_block_kept_as_content():+ # missing '<function=...>' -- not what the template ever emits, but a+ # truncated generation could still produce it; must not crash or+ # silently eat the block.+ text = "<tool_call>\nnot a function block\n</tool_call>"+ content, calls = parse_qwen_tool_calls(text)+ assert calls == []+ assert "<tool_call>" in content+++def test_ling_argument_coercion_numbers_and_json():+ text = (+ "<tool_call>set_temperature"+ "<arg_key>value</arg_key>\n<arg_value>21.5</arg_value>"+ "<arg_key>enabled</arg_key>\n<arg_value>true</arg_value>"+ "<arg_key>label</arg_key>\n<arg_value>living room</arg_value>"+ "\n</tool_call>"+ )+ _, calls = parse_ling_tool_calls(text)+ args = json.loads(calls[0]["function"]["arguments"])+ assert args == {"value": 21.5, "enabled": True, "label": "living room"}