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
fix(server): complete tool-call message round trips
Demetrios Agourakis185bd9ddcae3
diff --git a/python/src/edge0/server/app.py b/python/src/edge0/server/app.pyindex 02ddff9..fcbda96 100644--- a/python/src/edge0/server/app.py+++ b/python/src/edge0/server/app.py@@ -28,9 +28,7 @@ except ImportError: # pragma: no coverdef _error(status: int, msg: str) -> tuple:- if _HAS_FLASK:- return jsonify({"error": {"message": msg, "type": "invalid_request"}}), status- return (json.dumps({"error": {"message": msg}}), status)+ return {"error": {"message": msg, "type": "invalid_request"}}, statusdef _split_think(text: str, think: bool):@@ -134,7 +132,9 @@ def _chat_stream(server: QueueServer, payload: dict):content, tool_calls = _extract_tool_calls(server, req, content)if tool_calls:- delta = {"content": content, "tool_calls": tool_calls}+ delta = {"content": content, "tool_calls": [+ {"index": i, **call} for i, call in enumerate(tool_calls)+ ]}finish_reason = "tool_calls"else:delta = {"content": content}@@ -165,6 +165,11 @@ def build_app_handlers(server: QueueServer):"""Return a handler dispatch dict shared by both transports."""def handle_chat(payload: dict):+ # Validate before sending streaming response headers.+ try:+ parse_chat_request(payload)+ except ValueError as exc:+ return _error(400, str(exc))if payload.get("stream"):if _HAS_FLASK:return Response(@@ -217,8 +222,9 @@ def create_app(server: QueueServer):out = handlers["POST /v1/chat/completions"](payload)if isinstance(out, Response):return out- if isinstance(out, tuple) and out and isinstance(out[0], Response):- return out+ if isinstance(out, tuple):+ body, status = out+ return jsonify(body), statusreturn jsonify(out)@app.post("/v1/completions")@@ -226,7 +232,8 @@ def create_app(server: QueueServer):payload = request.get_json(force=True, silent=True) or {}out = handlers["POST /v1/completions"](payload)if isinstance(out, tuple):- return out+ body, status = out+ return jsonify(body), statusreturn jsonify(out)return app@@ -283,18 +290,20 @@ class _StdlibHandler(BaseHTTPRequestHandler):returnlength = int(self.headers.get("Content-Length", 0))payload = json.loads(self.rfile.read(length) or b"{}")+ if path == "/v1/chat/completions":+ try:+ parse_chat_request(payload)+ except ValueError as exc:+ self._json(400, {"error": {"message": str(exc),+ "type": "invalid_request"}})+ returnif path == "/v1/chat/completions" and payload.get("stream"):self._sse(_chat_stream(self.server_q, payload))returnout = handlers[("POST " + path)](payload)if isinstance(out, tuple):- body, status, headers = out- self.send_response(status)- for k, v in headers.items():- self.send_header(k, v)- self.send_header("Content-Length", str(len(body)))- self.end_headers()- self.wfile.write(body)+ body, status = out+ self._json(status, body)else:self._json(200, out)diff --git a/python/src/edge0/server/chat.py b/python/src/edge0/server/chat.pyindex b39080e..1cdb93a 100644--- a/python/src/edge0/server/chat.py+++ b/python/src/edge0/server/chat.py@@ -42,6 +42,30 @@ class ChatRequest:raw: dict[str, Any] = field(default_factory=dict)+def _template_tool_calls(calls) -> list[dict] | None:+ if calls is None:+ return None+ if not isinstance(calls, list):+ raise ValueError("tool_calls must be a list of function calls")+ normalized = []+ for call in calls:+ if not isinstance(call, dict) or not isinstance(call.get("function"), dict):+ raise ValueError("tool_calls entries must contain a function object")+ function = call["function"]+ arguments = function.get("arguments", {})+ # OpenAI messages serialize arguments; checkpoint templates iterate them.+ if isinstance(arguments, str):+ try:+ arguments = json.loads(arguments)+ except ValueError as exc:+ raise ValueError(+ "tool_calls function arguments must be a JSON object") from exc+ if not isinstance(arguments, dict):+ raise ValueError("tool_calls function arguments must be a JSON object")+ normalized.append({**call, "function": {**function, "arguments": arguments}})+ return normalized++def parse_chat_request(payload: dict) -> ChatRequest:msgs = []for m in payload.get("messages", []):@@ -53,7 +77,7 @@ def parse_chat_request(payload: dict) -> ChatRequest:p.get("text", "") for p in content if isinstance(p, dict))msgs.append(ChatMessage(role=role, content=str(content),- tool_calls=m.get("tool_calls"),+ tool_calls=_template_tool_calls(m.get("tool_calls")),tool_call_id=m.get("tool_call_id"),name=m.get("name"),))diff --git a/python/src/edge0/server/tool_calls.py b/python/src/edge0/server/tool_calls.pyindex 4c89f10..0a0538f 100644--- a/python/src/edge0/server/tool_calls.py+++ b/python/src/edge0/server/tool_calls.py@@ -6,7 +6,7 @@ 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``+ <arg_value>v</arg_value>...</tool_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>``.@@ -77,6 +77,22 @@ def _split(text: str, parse_block) -> tuple[str | None, list[dict]]:return content, calls+def _parse_arguments(text: str, pattern) -> dict | None:+ args = {}+ pos = 0+ for match in pattern.finditer(text):+ if text[pos:match.start()].strip():+ return None+ key = match.group(1).strip()+ if not key:+ return None+ args[key] = _coerce(match.group(2))+ pos = match.end()+ if text[pos:].strip():+ return None+ return args++def _parse_ling_block(body: str):name_match = re.match(r"^([^<]+)", body)if not name_match:@@ -84,7 +100,9 @@ def _parse_ling_block(body: str):name = name_match.group(1).strip()if not name:return None- args = {k.strip(): _coerce(v) for k, v in _LING_ARG.findall(body)}+ args = _parse_arguments(body[name_match.end():], _LING_ARG)+ if args is None:+ return Nonereturn name, args@@ -97,14 +115,16 @@ def parse_ling_tool_calls(text: str) -> tuple[str | None, list[dict]]:def _parse_qwen_block(body: str):- fn_match = _QWEN_FUNCTION.match(body)+ fn_match = _QWEN_FUNCTION.fullmatch(body)if not fn_match:return Nonename = fn_match.group(1).strip()if not name:return Noneparams = fn_match.group(2)- args = {k.strip(): _coerce(v) for k, v in _QWEN_PARAM.findall(params)}+ args = _parse_arguments(params, _QWEN_PARAM)+ if args is None:+ return Nonereturn name, argsdiff --git a/python/tests/fixtures/tool_templates/README.md b/python/tests/fixtures/tool_templates/README.mdnew file mode 100644index 0000000..ef4346a--- /dev/null+++ b/python/tests/fixtures/tool_templates/README.md@@ -0,0 +1,8 @@+These Apache-2.0 templates are copied from the official Edge0 checkpoints+for offline multi-turn tool-call regression tests. Model weights are not included.++- Ling: https://huggingface.co/Edge0/Edge0-8B-A1B-preview/blob/269b9a2c4a69d897c50e9f4e125328481d7c0fcf/chat_template.jinja+- Qwen: https://huggingface.co/Edge0/Edge0-35B-A3B-preview/blob/3fe15cbf2bd5bbcdfd611035e1ac88971ff1cdab/chat_template.jinja++The original model cards identify the license as Apache-2.0. Retain the+templates' original comments and consult the repository LICENSE.diff --git a/python/tests/fixtures/tool_templates/ling/chat_template.jinja b/python/tests/fixtures/tool_templates/ling/chat_template.jinjanew file mode 100644index 0000000..4be3ebd--- /dev/null+++ b/python/tests/fixtures/tool_templates/ling/chat_template.jinja@@ -0,0 +1,130 @@+{#- Bailing V3 chat template -#}+{#- Supports: thinking option, tool calling -#}++{#- ==================== thinking option normalization ==================== -#}+{%- if enable_thinking is defined %}+{%- if enable_thinking %}+{%- set thinking_option = 'on' %}+{%- else %}+{%- set thinking_option = 'off' %}+{%- endif %}+{%- elif thinking_option is not defined %}+{%- set thinking_option = 'on' %}+{%- endif %}++{#- ==================== preserved thinking ==================== -#}+{% set preserved_thinking = true %}++{#- ==================== system message ==================== -#}+{{- '<role>SYSTEM</role>' }}+{%- if tools %}+ {%- if messages[0].role == 'system' %}+ {{- messages[0].content + '\n' }}+ {%- endif %}+ {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}+ {%- for tool in tools %}+ {{- "\n" }}+ {{- tool | tojson }}+ {%- endfor %}+ {{- "\n</tools>\n\nIf none of the functions can be used, point it out. If the given question lacks the parameters required by the function, also point it out.\nIf you need to use a function, for each function call, output the function name and arguments within the following XML format:\n<tool_call>{function-name}\n<arg_key>{arg-key-1}</arg_key>\n<arg_value>{arg-value-1}</arg_value>\n<arg_key>{arg-key-2}</arg_key>\n<arg_value>{arg-value-2}</arg_value>\n...\n</tool_call>\n" }}+ {%- if messages[0].role == 'system' and messages[0].content is string and ('detailed thinking on' in messages[0].content or 'detailed thinking off' in messages[0].content) %}+ {{- '<|role_end|>' }}+ {%- else %}+ {{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}+ {%- endif %}+{%- else %}+ {%- if messages[0].role == 'system' %}+ {%- if 'detailed thinking on' in messages[0].content or 'detailed thinking off' in messages[0].content %}+ {{- messages[0].content + '<|role_end|>' }}+ {%- else %}+ {{- messages[0].content + '\n' }}+ {{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}+ {%- endif %}+ {% else %}+ {{- 'detailed thinking ' + thinking_option + '<|role_end|>' }}+ {%- endif %}+{%- endif %}+{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}+{%- for message in messages[::-1] %}+ {%- set index = (messages|length - 1) - loop.index0 %}+ {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}+ {%- set ns.multi_step_tool = false %}+ {%- set ns.last_query_index = index %}+ {%- endif %}+{%- endfor %}+{%- for message in messages %}+ {%- if message.content is string %}+ {%- set content = message.content %}+ {%- else %}+ {%- set content = '' %}+ {%- endif %}+ {%- if message.role == "user" %}+ {{- '<role>HUMAN</role>' + message.content + '<|role_end|>' }}+ {%- elif message.role == "system" and not loop.first %}+ {{- '<role>SYSTEM</role>' + message.content + '<|role_end|>' }}+ {%- elif message.role == "assistant" %}+ {%- set reasoning_content = '' %}+ {%- if message.reasoning_content is string and message.reasoning_content != '' %}+ {%- set reasoning_content = message.reasoning_content %}+ {%- else %}+ {%- if '</think>' in content %}+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}+ {%- endif %}+ {%- endif %}+ {%- if preserved_thinking or loop.index0 > ns.last_query_index %}+ {%- if reasoning_content != '' %}+ {{- '<role>ASSISTANT</role>' + '\n<think>' + reasoning_content.strip('\n') + '</think>' + content.lstrip('\n') }}+ {%- else %}+ {{- '<role>ASSISTANT</role>\n<think></think>' + content }}+ {%- endif %}+ {%- else %}+ {{- '<role>ASSISTANT</role>\n<think></think>' + content }}+ {%- endif %}+ {%- if message.tool_calls %}+ {%- for tool_call in message.tool_calls %}+ {%- if (loop.first and content) or (not loop.first) %}+ {{- '\n' }}+ {%- endif %}+ {%- set tc = tool_call %}+ {%- if tool_call.function %}+ {%- set tc = tool_call.function %}+ {%- endif %}+ {{- '<tool_call>' + tc.name }}+ {% set _args = tc.arguments %}+ {%- for k, v in _args.items() %}+ {{- '<arg_key>' + k + '</arg_key>' }}+ {{- '\n<arg_value>' }}+ {%- if v is string %}+ {{- v }}+ {%- else %}+ {{- v | tojson(ensure_ascii=False) }}+ {%- endif %}+ {{- '</arg_value>' }}+ {%- endfor %}+ {{- '\n</tool_call>' }}+ {%- endfor %}+ {%- endif %}+ {{- '<|role_end|>' }}+ {%- elif message.role == "tool" %}+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}+ {{- '<role>OBSERVATION</role>' }}+ {%- endif %}+ {{- '\n<tool_response>\n' }}+ {{- content }}+ {{- '\n</tool_response>' }}+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}+ {{- '<|role_end|>' }}+ {%- endif %}+ {%- endif %}+{%- endfor %}++{#- ==================== generation prompt ==================== -#}+{%- if add_generation_prompt %}+ {{- '<role>ASSISTANT</role>' }}+ {%- if thinking_option == 'on' %}+ {{- '\n<think>' }}+ {%- elif thinking_option == 'off' %}+ {{- '\n<think></think>' }}+ {%- endif %}+{%- endif %}diff --git a/python/tests/fixtures/tool_templates/qwen/chat_template.jinja b/python/tests/fixtures/tool_templates/qwen/chat_template.jinjanew file mode 100644index 0000000..4540e44--- /dev/null+++ b/python/tests/fixtures/tool_templates/qwen/chat_template.jinja@@ -0,0 +1,154 @@+{%- set image_count = namespace(value=0) %}+{%- set video_count = namespace(value=0) %}+{%- macro render_content(content, do_vision_count, is_system_content=false) %}+ {%- if content is string %}+ {{- content }}+ {%- elif content is iterable and content is not mapping %}+ {%- for item in content %}+ {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}+ {%- if is_system_content %}+ {{- raise_exception('System message cannot contain images.') }}+ {%- endif %}+ {%- if do_vision_count %}+ {%- set image_count.value = image_count.value + 1 %}+ {%- endif %}+ {%- if add_vision_id %}+ {{- 'Picture ' ~ image_count.value ~ ': ' }}+ {%- endif %}+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}+ {%- elif 'video' in item or item.type == 'video' %}+ {%- if is_system_content %}+ {{- raise_exception('System message cannot contain videos.') }}+ {%- endif %}+ {%- if do_vision_count %}+ {%- set video_count.value = video_count.value + 1 %}+ {%- endif %}+ {%- if add_vision_id %}+ {{- 'Video ' ~ video_count.value ~ ': ' }}+ {%- endif %}+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}+ {%- elif 'text' in item %}+ {{- item.text }}+ {%- else %}+ {{- raise_exception('Unexpected item type in content.') }}+ {%- endif %}+ {%- endfor %}+ {%- elif content is none or content is undefined %}+ {{- '' }}+ {%- else %}+ {{- raise_exception('Unexpected content type.') }}+ {%- endif %}+{%- endmacro %}+{%- if not messages %}+ {{- raise_exception('No messages provided.') }}+{%- endif %}+{%- if tools and tools is iterable and tools is not mapping %}+ {{- '<|im_start|>system\n' }}+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}+ {%- for tool in tools %}+ {{- "\n" }}+ {{- tool | tojson }}+ {%- endfor %}+ {{- "\n</tools>" }}+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}+ {%- if messages[0].role == 'system' %}+ {%- set content = render_content(messages[0].content, false, true)|trim %}+ {%- if content %}+ {{- '\n\n' + content }}+ {%- endif %}+ {%- endif %}+ {{- '<|im_end|>\n' }}+{%- else %}+ {%- if messages[0].role == 'system' %}+ {%- set content = render_content(messages[0].content, false, true)|trim %}+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}+ {%- endif %}+{%- endif %}+{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}+{%- for message in messages[::-1] %}+ {%- set index = (messages|length - 1) - loop.index0 %}+ {%- if ns.multi_step_tool and message.role == "user" %}+ {%- set content = render_content(message.content, false)|trim %}+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}+ {%- set ns.multi_step_tool = false %}+ {%- set ns.last_query_index = index %}+ {%- endif %}+ {%- endif %}+{%- endfor %}+{%- if ns.multi_step_tool %}+ {{- raise_exception('No user query found in messages.') }}+{%- endif %}+{%- for message in messages %}+ {%- set content = render_content(message.content, true)|trim %}+ {%- if message.role == "system" %}+ {%- if not loop.first %}+ {{- raise_exception('System message must be at the beginning.') }}+ {%- endif %}+ {%- elif message.role == "user" %}+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}+ {%- elif message.role == "assistant" %}+ {%- set reasoning_content = '' %}+ {%- if message.reasoning_content is string %}+ {%- set reasoning_content = message.reasoning_content %}+ {%- else %}+ {%- if '</think>' in content %}+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}+ {%- endif %}+ {%- endif %}+ {%- set reasoning_content = reasoning_content|trim %}+ {%- if (preserve_thinking is defined and preserve_thinking is true) or (loop.index0 > ns.last_query_index) %}+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}+ {%- else %}+ {{- '<|im_start|>' + message.role + '\n' + content }}+ {%- endif %}+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}+ {%- for tool_call in message.tool_calls %}+ {%- if tool_call.function is defined %}+ {%- set tool_call = tool_call.function %}+ {%- endif %}+ {%- if loop.first %}+ {%- if content|trim %}+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}+ {%- else %}+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}+ {%- endif %}+ {%- else %}+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}+ {%- endif %}+ {%- if tool_call.arguments is defined %}+ {%- for args_name, args_value in tool_call.arguments|items %}+ {{- '<parameter=' + args_name + '>\n' }}+ {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %}+ {{- args_value }}+ {{- '\n</parameter>\n' }}+ {%- endfor %}+ {%- endif %}+ {{- '</function>\n</tool_call>' }}+ {%- endfor %}+ {%- endif %}+ {{- '<|im_end|>\n' }}+ {%- elif message.role == "tool" %}+ {%- if loop.previtem and loop.previtem.role != "tool" %}+ {{- '<|im_start|>user' }}+ {%- endif %}+ {{- '\n<tool_response>\n' }}+ {{- content }}+ {{- '\n</tool_response>' }}+ {%- if not loop.last and loop.nextitem.role != "tool" %}+ {{- '<|im_end|>\n' }}+ {%- elif loop.last %}+ {{- '<|im_end|>\n' }}+ {%- endif %}+ {%- else %}+ {{- raise_exception('Unexpected message role.') }}+ {%- endif %}+{%- endfor %}+{%- if add_generation_prompt %}+ {{- '<|im_start|>assistant\n' }}+ {%- if enable_thinking is defined and enable_thinking is false %}+ {{- '<think>\n\n</think>\n\n' }}+ {%- else %}+ {{- '<think>\n' }}+ {%- endif %}+{%- endif %}diff --git a/python/tests/test_server.py b/python/tests/test_server.pyindex e1cd6e3..c153da2 100644--- a/python/tests/test_server.py+++ b/python/tests/test_server.py@@ -12,7 +12,9 @@ import jsonimport threadingimport timefrom http.server import ThreadingHTTPServer-from types import SimpleNamespace+from pathlib import Path+from types import MethodType, SimpleNamespace+from urllib.error import HTTPErrorfrom urllib import request as urlrequestimport pytest@@ -213,6 +215,50 @@ def test_parse_message_tool_calls_and_tool_role_roundtrip():assert req.messages[2].tool_call_id == "call_1"[email protected]("arguments", ['{"expression": "2 + 2"}',+ {"expression": "2 + 2"}])+def test_parse_normalizes_tool_arguments_without_mutating_payload(arguments):+ payload = {"messages": [{"role": "assistant", "content": None,+ "tool_calls": [{+ "id": "call_1", "type": "function",+ "function": {"name": "calculator",+ "arguments": arguments},+ }]}]}+ before = json.dumps(payload)+ req = parse_chat_request(payload)+ call = req.messages[0].tool_calls[0]+ assert call["function"]["arguments"] == {"expression": "2 + 2"}+ assert call["id"] == "call_1"+ assert json.dumps(payload) == before++[email protected]("arguments", ["{broken", "[]", "null", '"text"',+ "42", [], None])+def test_parse_rejects_invalid_tool_arguments(arguments):+ with pytest.raises(ValueError, match="arguments.*JSON object"):+ parse_chat_request({"messages": [{+ "role": "assistant", "content": None,+ "tool_calls": [{"type": "function", "function": {+ "name": "calculator", "arguments": arguments}}],+ }]})++[email protected]("calls", ["bad", {}, [None], [{"function": "bad"}]])+def test_parse_rejects_invalid_tool_call_shapes(calls):+ with pytest.raises(ValueError, match="tool_calls"):+ parse_chat_request({"messages": [{+ "role": "assistant", "content": None, "tool_calls": calls,+ }]})+++def test_parse_tool_call_without_arguments_uses_empty_object():+ req = parse_chat_request({"messages": [{+ "role": "assistant", "content": None,+ "tool_calls": [{"function": {"name": "get_time"}}],+ }]})+ assert req.messages[0].tool_calls[0]["function"]["arguments"] == {}++# ---- sse / decode ---------------------------------------------------------@@ -354,6 +400,88 @@ def test_chat_once_response_shape():# ---- tool calls (#115) -----------------------------------------------------[email protected](params=["ling", "qwen"])+def real_template_engine(request):+ from tokenizers import Tokenizer+ from tokenizers.models import WordLevel+ from transformers import PreTrainedTokenizerFast++ family = request.param+ template_dir = Path(__file__).parent / "fixtures" / "tool_templates" / family+ template = (template_dir / "chat_template.jinja").read_text(encoding="utf-8")+ tokenizer = PreTrainedTokenizerFast(+ tokenizer_object=Tokenizer(WordLevel({"[UNK]": 0}, unk_token="[UNK]")),+ unk_token="[UNK]", chat_template=template,+ )++ class TemplateTok(FakeTok):+ output = (+ "<tool_call>calculator<arg_key>expression</arg_key>"+ "<arg_value>2 + 2</arg_value></tool_call>"+ if family == "ling" else+ "<tool_call>\n<function=calculator>\n<parameter=expression>\n"+ "2 + 2\n</parameter>\n</function>\n</tool_call>"+ )++ def apply_chat_template(self, *args, **kwargs):+ return tokenizer.apply_chat_template(*args, **kwargs)++ def encode(self, text, **kwargs):+ return super().encode(text)++ def decode(self, tokens):+ return self.output++ engine = ToolCallEngine(tok=TemplateTok())+ if family == "ling":+ from edge0.engine.ling import Ling8BEngine+ engine.dir = str(template_dir)+ engine.think = False+ engine._chat_tpl = None+ engine._chat_template = MethodType(Ling8BEngine._chat_template, engine)+ engine.encode_chat = MethodType(Ling8BEngine.encode_chat, engine)+ else:+ from edge0.engine.qwen import Qwen35Engine+ engine.parse_tool_calls = MethodType(Qwen35Engine.parse_tool_calls, engine)+ return engine++[email protected]("stream", [False, True])+def test_tool_call_roundtrip_renders_real_template(real_template_engine, stream):+ engine = real_template_engine+ server = QueueServer(engine)+ user = {"role": "user", "content": "Use the calculator for 2 + 2."}+ payload = {"messages": [user], "tools": _TOOLS}+ if stream:+ frames = b"".join(_chat_stream(server, payload)).decode().split("\n\n")+ chunk = json.loads(frames[-3][6:])["choices"][0]+ assert chunk["finish_reason"] == "tool_calls"+ assistant = {"role": "assistant", **chunk["delta"]}+ # A client accumulates indexed SSE deltas into a message.+ assistant["tool_calls"] = [+ {k: v for k, v in call.items() if k != "index"}+ for call in assistant["tool_calls"]+ ]+ else:+ choice = _chat_once(server, payload)["choices"][0]+ assert choice["finish_reason"] == "tool_calls"+ assistant = choice["message"]+ call = assistant["tool_calls"][0]+ assert isinstance(call["function"]["arguments"], str)+ engine._tok.output = "The result is 4."+ result = _chat_once(server, {+ "messages": [user, assistant, {+ "role": "tool", "tool_call_id": call["id"],+ "name": "calculator", "content": "4",+ }], "tools": _TOOLS,+ })+ prompt = engine._tok.encoded_text+ assert "<tool_response>\n4\n</tool_response>" in prompt+ assert "<tool_call>" in prompt+ assert "2 + 2" in prompt+ assert result["choices"][0]["message"]["content"] == "The result is 4."++def test_chat_once_ignores_tool_call_text_without_tools_field():"""No ``tools`` in the request -> the raw <tool_call> XML the fakegeneration returns is left as plain content, exactly today's@@ -426,11 +554,26 @@ def test_chat_stream_tools_requested_buffers_and_emits_final_tool_calls():assert final["finish_reason"] == "tool_calls"assert final["delta"]["content"] is Nonecalls = final["delta"]["tool_calls"]+ assert calls[0]["index"] == 0assert calls[0]["function"]["name"] == "calculator"assert json.loads(calls[0]["function"]["arguments"]) == {"expression": "2 + 2"}+def test_chat_stream_indexes_multiple_tool_calls():+ class MultiCallTok(ToolCallTok):+ def decode(self, tokens):+ return super().decode(tokens) * 2++ server = QueueServer(ToolCallEngine(tok=MultiCallTok()))+ frames = b"".join(_chat_stream(server, {+ "messages": [{"role": "user", "content": "hi"}], "tools": _TOOLS,+ })).decode().split("\n\n")+ calls = json.loads(frames[-3][6:])["choices"][0]["delta"]["tool_calls"]+ assert [call["index"] for call in calls] == [0, 1]+ assert calls[0]["id"] != calls[1]["id"]++def test_chat_stream_without_tools_keeps_immediate_per_token_deltas():"""Regression guard: requests with no tools must keep the existingimmediate per-token streaming -- buffering is scoped to tool-enabled@@ -510,6 +653,47 @@ def test_chat_stream_yields_before_generation_finishes():# ---- stdlib HTTP transport ------------------------------------------------[email protected]("stream", [False, True])[email protected]("transport", ["stdlib", "flask"])+def test_http_rejects_invalid_tool_arguments_before_generation(stream, transport):+ engine = FakeEngine()+ server = QueueServer(engine)+ payload = {"stream": stream, "messages": [{+ "role": "assistant", "content": None,+ "tool_calls": [{"type": "function", "function": {+ "name": "calculator", "arguments": "{broken"}}],+ }]}+ if transport == "flask":+ if not _HAS_FLASK:+ pytest.skip("flask is not installed")+ response = create_app(server).test_client().post(+ "/v1/chat/completions", json=payload)+ assert response.status_code == 400+ error = response.get_json()+ else:+ handler = type("Edge0Handler", (_StdlibHandler,), {"server_q": server})+ httpd = ThreadingHTTPServer(("127.0.0.1", 0), handler)+ thread = threading.Thread(target=httpd.serve_forever, daemon=True)+ thread.start()+ try:+ req = urlrequest.Request(+ f"http://127.0.0.1:{httpd.server_address[1]}/v1/chat/completions",+ data=json.dumps(payload).encode(),+ headers={"Content-Type": "application/json"},+ )+ with pytest.raises(HTTPError) as exc:+ urlrequest.urlopen(req, timeout=10)+ with exc.value as response:+ assert response.code == 400+ error = json.loads(response.read())+ finally:+ httpd.shutdown()+ httpd.server_close()+ thread.join(timeout=5)+ assert "arguments must be a JSON object" in error["error"]["message"]+ assert engine.generated == []++def test_stdlib_http_end_to_end():eng = FakeEngine()srv = QueueServer(eng, model_name="fake")diff --git a/python/tests/test_tool_calls.py b/python/tests/test_tool_calls.pyindex e4cd469..1b41187 100644--- a/python/tests/test_tool_calls.py+++ b/python/tests/test_tool_calls.py@@ -16,6 +16,8 @@ from __future__ import annotationsimport json+import pytest+from edge0.server.tool_calls import parse_ling_tool_calls, parse_qwen_tool_calls# Captured from edge0-8b/chat_template.jinja rendered with@@ -110,6 +112,45 @@ def test_qwen_malformed_block_kept_as_content():assert "<tool_call>" in content[email protected]("parser,body", [+ (parse_ling_tool_calls, "calculator<arg_key>x</arg_key>"),+ (parse_ling_tool_calls, "calculator<arg_key></arg_key><arg_value>1</arg_value>"),+ (parse_ling_tool_calls, "calculator<arg_key>x</arg_key><arg_value>1</arg_value>junk"),+ (parse_qwen_tool_calls, "<function=calculator><parameter=x>broken</function>"),+ (parse_qwen_tool_calls, "<function=calculator></function>junk"),+ (parse_qwen_tool_calls, "<function=calculator><parameter=>\n1\n</parameter></function>"),+])+def test_incomplete_or_unconsumed_arguments_are_preserved(parser, body):+ text = f"<tool_call>{body}</tool_call>"+ content, calls = parser(text)+ assert content == text+ assert calls == []++[email protected]("parser,text", [+ (parse_ling_tool_calls, "<tool_call>get_time</tool_call>"),+ (parse_qwen_tool_calls, "<tool_call><function=get_time></function></tool_call>"),+])+def test_zero_argument_tool_call(parser, text):+ content, calls = parser(text)+ assert content is None+ assert json.loads(calls[0]["function"]["arguments"]) == {}+++def test_qwen_function_allows_surrounding_whitespace():+ text = "<tool_call>\n <function=get_time></function>\n </tool_call>"+ content, calls = parse_qwen_tool_calls(text)+ assert content is None+ assert calls[0]["function"]["name"] == "get_time"+++def test_malformed_blocks_survive_between_valid_calls():+ malformed = "<tool_call>calculator<arg_key>x</arg_key></tool_call>"+ content, calls = parse_ling_tool_calls(LING_REAL_SPAN + malformed + LING_REAL_SPAN)+ assert content == malformed+ assert len(calls) == 2++def test_ling_argument_coercion_numbers_and_json():text = ("<tool_call>set_temperature"