Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

registry and gateway: Qwen3-Coder-30B-A3B and its REAP prunes

JustVugge69870beaa6c
diff --git a/c/family_registry.py b/c/family_registry.py
index 9093f44b..f3151a70 100644
--- a/c/family_registry.py
+++ b/c/family_registry.py
@@ -234,6 +234,9 @@ def _qwen36_layer_types(config, layers, model_dir):
kinds = meta.get("layer_types")
if isinstance(kinds, list) and len(kinds) == layers:
return kinds
+ if config.get("model_type") == "qwen3_moe":
+ # Qwen3 MoE (Qwen3-Coder-30B-A3B): every layer is attention, no DeltaNet.
+ return ["full_attention"] * layers
interval = config.get("full_attention_interval")
if isinstance(interval, int) and not isinstance(interval, bool) and interval >= 1:
return ["full_attention" if (i + 1) % interval == 0 else "linear_attention"
@@ -253,6 +256,11 @@ def _qwen36_geometry(config, context, _model_dir):
full = sum(kind == "full_attention" for kind in kinds)
kv = (full * context * _required_int(config, "num_key_value_heads", "qwen36") *
_required_int(config, "head_dim", "qwen36") * 2 * 4)
+ if full == layers:
+ # all attention (qwen3_moe): no recurrent state, and no linear_* keys to read
+ if "num_experts" not in config:
+ return PlannerGeometry(kv, 0, 0, 0, dense=True)
+ return PlannerGeometry(kv, 0, 0, _required_int(config, "num_experts", "qwen36"))
key_heads = _required_int(config, "linear_num_key_heads", "qwen36")
key_dim = _required_int(config, "linear_key_head_dim", "qwen36")
value_heads = _required_int(config, "linear_num_value_heads", "qwen36")
@@ -1371,7 +1379,9 @@ FAMILIES = (
# qwen3_5 / qwen3_5_text: the dense checkpoints of the same architecture
# (Qwen3.8-27B, #1757). The engine loads their MLP as an ungated shared
# expert and routes nothing.
- model_types=("qwen3_5_moe", "qwen3_5_moe_text", "qwen3_5", "qwen3_5_text"),
+ # qwen3_moe: Qwen3-Coder-30B-A3B and its REAP prunes, every layer attention,
+ # no shared expert (tools/convert_qwen36.py writes their container).
+ model_types=("qwen3_5_moe", "qwen3_5_moe_text", "qwen3_5", "qwen3_5_text", "qwen3_moe"),
display_name="Qwen3.6-35B-A3B",
display_scale="35B",
# Both checkpoints declare qwen3_5_moe_text. Keyed on the three
@@ -1387,6 +1397,13 @@ FAMILIES = (
DisplayVariant((("num_hidden_layers", 64), ("hidden_size", 5120),
("intermediate_size", 17408)),
"Qwen3.8-27B", "27B", model_id="qwen3.8-27b-colibri"),
+ DisplayVariant((("num_hidden_layers", 48),
+ ("num_experts", 128), ("hidden_size", 2048)),
+ "Qwen3-Coder-30B-A3B", "30B", model_id="qwen3-coder-30b-a3b-colibri"),
+ DisplayVariant((("num_hidden_layers", 48),
+ ("num_experts", 103), ("hidden_size", 2048)),
+ "Qwen3-Coder-REAP-25B-A3B", "25B",
+ model_id="qwen3-coder-reap-25b-a3b-colibri"),
),
engine_artifact="qwen36",
engine_aliases=(),
diff --git a/c/openai_server.py b/c/openai_server.py
index 3d6847b4..39fa1d60 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -1023,7 +1023,7 @@ def _parse_arch_tool_calls(reply, tools, tool_reply, track_spans):
_sideband_text, calls = parse_k3_tool_calls(tool_reply, tools)
return reply.strip(), calls, None, None
return parse_k3_tool_calls(reply, tools) + (None, None) # pre-#1147 engines
- if chat_flavor() in ("qwen36", "qwen38"):
+ if chat_flavor() in ("qwen36", "qwen38", "qwen3_coder"):
return parse_qwen_tool_calls(reply, tools) + (None, None)
if ARCH == "mimo":
return parse_mimo_tool_calls(reply, tools) + (None, None)
@@ -1085,6 +1085,9 @@ def detect_chat_flavor(family_id, model_dir):
# the line that makes a template Qwen3.8's: reasoning is on by default, at xhigh
if "reasoning_effort|default('xhigh')" in text:
return "qwen38"
+ # Qwen3-Coder (qwen3_moe): its own XML tool block, no thinking
+ if "interact with a computer to solve tasks" in text:
+ return "qwen3_coder"
return None
INK_THINK, INK_TEXT = "<|content_thinking|>", "<|content_text|>"
@@ -1932,6 +1935,162 @@ def _qwen_tool_calls(tool_calls, has_content, index):
return "".join(out)
+QWEN3_CODER_SYSTEM = ("You are Qwen, a helpful AI assistant that can interact with a computer "
+ "to solve tasks.")
+QWEN3_CODER_TOOL_RULES = (
+ "\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>\n"
+ "value_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>")
+
+
+def _jinja_string(value):
+ """jinja's `string` filter: a str as it is, anything else through str()."""
+ return value if isinstance(value, str) else str(value)
+
+
+def _qwen3_coder_extra_keys(fields, handled):
+ """The template's render_extra_keys macro: every key it does not name, in order."""
+ if not isinstance(fields, dict):
+ return ""
+ out = []
+ for key, value in fields.items():
+ if key in handled:
+ continue
+ shown = (json.dumps(value, ensure_ascii=False) if isinstance(value, (dict, list, tuple))
+ else _jinja_string(value))
+ out.append(f"\n<{key}>{shown}</{key}>")
+ return "".join(out)
+
+
+def _qwen3_coder_tool_block(tools):
+ """Qwen3-Coder's `# Tools` section: each function as XML, its parameters one by one."""
+ out = ["\n\n# Tools\n\nYou have access to the following functions:\n\n<tools>"]
+ for index, tool in enumerate(tools):
+ if not isinstance(tool, dict):
+ raise APIError(400, "Each tool must be an object.", f"tools.{index}")
+ fn = tool["function"] if isinstance(tool.get("function"), dict) else tool
+ out.append(f"\n<function>\n<name>{_jinja_string(fn.get('name', ''))}</name>")
+ if "description" in fn:
+ out.append(f"\n<description>{_jinja_string(fn['description']).strip()}</description>")
+ out.append("\n<parameters>")
+ params = fn.get("parameters")
+ if isinstance(params, dict) and isinstance(params.get("properties"), dict):
+ for name, field in params["properties"].items():
+ out.append(f"\n<parameter>\n<name>{name}</name>")
+ if isinstance(field, dict) and "type" in field:
+ out.append(f"\n<type>{_jinja_string(field['type'])}</type>")
+ if isinstance(field, dict) and "description" in field:
+ out.append(f"\n<description>{_jinja_string(field['description']).strip()}"
+ "</description>")
+ out.append(_qwen3_coder_extra_keys(field, ("name", "type", "description")))
+ out.append("\n</parameter>")
+ out.append(_qwen3_coder_extra_keys(params, ("type", "properties")))
+ out.append("\n</parameters>")
+ out.append(_qwen3_coder_extra_keys(fn, ("type", "name", "description", "parameters")))
+ out.append("\n</function>")
+ out.append("\n</tools>")
+ out.append(QWEN3_CODER_TOOL_RULES)
+ return "".join(out)
+
+
+def render_chat_qwen3_coder(messages, tools=None, tool_choice=None, add_generation_prompt=True):
+ """Qwen3-Coder's chat_template (qwen3_moe), byte for byte: ChatML with a newline after
+ every <|im_end|>, no thinking at all, the system turn carrying the client's system text
+ and then the XML `# Tools` block (with the template's own system line when the client
+ sent none), assistant calls as <tool_call><function=...><parameter=...>, and runs of
+ `tool` messages sharing one user turn of <tool_response> blocks.
+
+ Tool-call arguments sent as a JSON string, the OpenAI shape, are read as the object the
+ template expects. add_generation_prompt=False leaves a trailing assistant turn open."""
+ if not isinstance(messages, list) or not messages:
+ raise APIError(400, "`messages` must be a non-empty array.", "messages")
+ if tool_choice == "none":
+ tools = None
+ if tools is not None and not isinstance(tools, list):
+ raise APIError(400, "`tools` must be an array.", "tools")
+ parts = []
+ first = messages[0]
+ start = 0
+ system = None
+ if isinstance(first, dict) and first.get("role") in ("system", "developer"):
+ raw = first.get("content")
+ system = content_text(raw, "messages.0.content") if raw is not None else ""
+ start = 1
+ if system is not None:
+ parts.append("<|im_start|>system\n" + system)
+ elif tools:
+ parts.append("<|im_start|>system\n" + QWEN3_CODER_SYSTEM)
+ if tools:
+ parts.append(_qwen3_coder_tool_block(tools))
+ if system is not None or tools:
+ parts.append("<|im_end|>\n")
+ loop = messages[start:]
+ for offset, message in enumerate(loop):
+ index = start + offset
+ if not isinstance(message, dict):
+ raise APIError(400, "Each message must be an object.", f"messages.{index}")
+ role = message.get("role")
+ if role == "developer":
+ role = "system"
+ if role not in ("system", "user", "assistant", "tool"):
+ raise APIError(400, f"Unsupported role {role!r}.", f"messages.{index}.role")
+ raw = message.get("content")
+ text = content_text(raw, f"messages.{index}.content") if raw is not None else ""
+ calls = message.get("tool_calls") if role == "assistant" else None
+ if calls is not None and not isinstance(calls, list):
+ raise APIError(400, "`tool_calls` must be an array.", f"messages.{index}.tool_calls")
+ if not add_generation_prompt and role == "assistant" and not calls and \
+ offset == len(loop) - 1:
+ parts.append(f"<|im_start|>assistant\n{text}") # continued, still open
+ continue
+ if calls:
+ parts.append("<|im_start|>assistant")
+ if text.strip():
+ parts.append("\n" + text.strip() + "\n")
+ for position, call in enumerate(calls):
+ where = f"messages.{index}.tool_calls.{position}"
+ fn = call.get("function", call) if isinstance(call, dict) else None
+ if not isinstance(fn, dict) or not isinstance(fn.get("name"), str) or not fn["name"]:
+ raise APIError(400, "Each tool call needs a `function.name`.", where)
+ args = fn.get("arguments", {})
+ if isinstance(args, str):
+ try:
+ args = json.loads(args) if args.strip() else {}
+ except ValueError:
+ raise APIError(400, "`function.arguments` must be a JSON object.",
+ f"{where}.function.arguments")
+ if not isinstance(args, dict):
+ raise APIError(400, "`function.arguments` must be a JSON object.",
+ f"{where}.function.arguments")
+ parts.append(f"\n<tool_call>\n<function={fn['name']}>\n")
+ for key, value in args.items():
+ shown = (json.dumps(value, ensure_ascii=False)
+ if isinstance(value, (dict, list, tuple)) else _jinja_string(value))
+ parts.append(f"<parameter={key}>\n{shown}\n</parameter>\n")
+ parts.append("</function>\n</tool_call>")
+ parts.append("<|im_end|>\n")
+ elif role == "tool":
+ previous = loop[offset - 1] if offset > 0 else None
+ if isinstance(previous, dict) and previous.get("role") != "tool":
+ parts.append("<|im_start|>user\n")
+ parts.append(f"<tool_response>\n{text}\n</tool_response>\n")
+ following = loop[offset + 1] if offset + 1 < len(loop) else None
+ if following is None or (isinstance(following, dict) and following.get("role") != "tool"):
+ parts.append("<|im_end|>\n")
+ else:
+ parts.append(f"<|im_start|>{role}\n{text}<|im_end|>\n")
+ if add_generation_prompt:
+ parts.append("<|im_start|>assistant\n")
+ return "".join(parts)
+
+
QWEN_CALL_RE = re.compile(
r"<tool_call>\s*<function=([^>\n]+)>\s*(.*?)</function>\s*</tool_call>", re.S)
QWEN_PARAM_RE = re.compile(r"<parameter=([^>\n]+)>\n(.*?)\n</parameter>", re.S)
@@ -1962,6 +2121,10 @@ def parse_qwen_tool_calls(reply, tools=None):
kind = declared.get("type") if isinstance(declared, dict) else None
if kind in (None, "string"):
args[key] = raw
+ elif kind == "boolean" and raw.strip().lower() in ("true", "false"):
+ # The templates print a bool through jinja's `string`, so the model
+ # writes `True`; Qwen's own parser reads it case-insensitively.
+ args[key] = raw.strip().lower() == "true"
else:
try:
args[key] = json.loads(raw)
@@ -3236,6 +3399,8 @@ def render_chat_for_arch(messages, enable_thinking=False, reasoning_effort=None,
if chat_flavor() == "qwen38":
return render_chat_qwen38(messages, enable_thinking, reasoning_effort, tools,
tool_choice, add_generation_prompt)
+ if chat_flavor() == "qwen3_coder":
+ return render_chat_qwen3_coder(messages, tools, tool_choice, add_generation_prompt)
if ARCH == "qwen36":
return render_chat_qwen(messages, enable_thinking, reasoning_effort, tools,
tool_choice, add_generation_prompt, preserve_thinking)
@@ -7001,7 +7166,8 @@ class APIHandler(BaseHTTPRequestHandler):
enable_thinking = body.get("enable_thinking", reasoning_effort not in (None, "none"))
if not isinstance(enable_thinking, bool):
raise APIError(400, "`enable_thinking` must be a boolean.", "enable_thinking")
- if ARCH == "olmoe" and enable_thinking:
+ if (ARCH == "olmoe" or chat_flavor() == "qwen3_coder") and enable_thinking:
+ # Qwen3-Coder's template has no thinking mode either (no <think> anywhere).
# OLMoE's template has no thinking mode (render_chat_olmoe: "accepted
# but unused"), so the engine never emits <think>/</think>. Left on,
# the reasoning splitter files the ENTIRE answer as reasoning_content
@@ -7115,7 +7281,7 @@ class APIHandler(BaseHTTPRequestHandler):
enable_thinking = True
elif os.environ.get("COLI_THINK", "0") == "1":
enable_thinking = True
- if ARCH == "olmoe":
+ if ARCH == "olmoe" or chat_flavor() == "qwen3_coder":
enable_thinking = False # #984: OLMoE has no thinking mode (see the OpenAI path)
if body.get("max_tokens") is None:
raise APIError(400, "`max_tokens` is required.", "max_tokens")
diff --git a/c/tests/fixtures/qwen3_coder_chat_template.jinja b/c/tests/fixtures/qwen3_coder_chat_template.jinja
new file mode 100644
index 00000000..49b0e8d0
--- /dev/null
+++ b/c/tests/fixtures/qwen3_coder_chat_template.jinja
@@ -0,0 +1,117 @@
+{% macro render_extra_keys(json_dict, handled_keys) %}
+ {%- if json_dict is mapping %}
+ {%- for json_key in json_dict if json_key not in handled_keys %}
+ {%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}
+ {{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}
+ {%- else %}
+ {{-'\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}
+ {%- endif %}
+ {%- endfor %}
+ {%- endif %}
+{% endmacro %}
+
+{%- if messages[0]["role"] == "system" %}
+ {%- set system_message = messages[0]["content"] %}
+ {%- set loop_messages = messages[1:] %}
+{%- else %}
+ {%- set loop_messages = messages %}
+{%- endif %}
+
+{%- if not tools is defined %}
+ {%- set tools = [] %}
+{%- endif %}
+
+{%- if system_message is defined %}
+ {{- "<|im_start|>system\n" + system_message }}
+{%- else %}
+ {%- if tools is iterable and tools | length > 0 %}
+ {{- "<|im_start|>system\nYou are Qwen, a helpful AI assistant that can interact with a computer to solve tasks." }}
+ {%- endif %}
+{%- endif %}
+{%- if tools is iterable and tools | length > 0 %}
+ {{- "\n\n# Tools\n\nYou have access to the following functions:\n\n" }}
+ {{- "<tools>" }}
+ {%- for tool in tools %}
+ {%- if tool.function is defined %}
+ {%- set tool = tool.function %}
+ {%- endif %}
+ {{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
+ {%- if tool.description is defined %}
+ {{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
+ {%- endif %}
+ {{- '\n<parameters>' }}
+ {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}
+ {%- for param_name, param_fields in tool.parameters.properties|items %}
+ {{- '\n<parameter>' }}
+ {{- '\n<name>' ~ param_name ~ '</name>' }}
+ {%- if param_fields.type is defined %}
+ {{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
+ {%- endif %}
+ {%- if param_fields.description is defined %}
+ {{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
+ {%- endif %}
+ {%- set handled_keys = ['name', 'type', 'description'] %}
+ {{- render_extra_keys(param_fields, handled_keys) }}
+ {{- '\n</parameter>' }}
+ {%- endfor %}
+ {%- endif %}
+ {% set handled_keys = ['type', 'properties'] %}
+ {{- render_extra_keys(tool.parameters, handled_keys) }}
+ {{- '\n</parameters>' }}
+ {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
+ {{- render_extra_keys(tool, handled_keys) }}
+ {{- '\n</function>' }}
+ {%- 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>' }}
+{%- endif %}
+{%- if system_message is defined %}
+ {{- '<|im_end|>\n' }}
+{%- else %}
+ {%- if tools is iterable and tools | length > 0 %}
+ {{- '<|im_end|>\n' }}
+ {%- endif %}
+{%- endif %}
+{%- for message in loop_messages %}
+ {%- if message.role == "assistant" and message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}
+ {{- '<|im_start|>' + message.role }}
+ {%- if message.content is defined and message.content is string and message.content | trim | length > 0 %}
+ {{- '\n' + message.content | trim + '\n' }}
+ {%- endif %}
+ {%- for tool_call in message.tool_calls %}
+ {%- if tool_call.function is defined %}
+ {%- set tool_call = tool_call.function %}
+ {%- endif %}
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
+ {%- 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 | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
+ {{- args_value }}
+ {{- '\n</parameter>\n' }}
+ {%- endfor %}
+ {%- endif %}
+ {{- '</function>\n</tool_call>' }}
+ {%- endfor %}
+ {{- '<|im_end|>\n' }}
+ {%- elif message.role == "user" or message.role == "system" or message.role == "assistant" %}
+ {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
+ {%- elif message.role == "tool" %}
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
+ {{- '<|im_start|>user\n' }}
+ {%- endif %}
+ {{- '<tool_response>\n' }}
+ {{- message.content }}
+ {{- '\n</tool_response>\n' }}
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
+ {{- '<|im_end|>\n' }}
+ {%- elif loop.last %}
+ {{- '<|im_end|>\n' }}
+ {%- endif %}
+ {%- else %}
+ {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' }}
+ {%- endif %}
+{%- endfor %}
+{%- if add_generation_prompt %}
+ {{- '<|im_start|>assistant\n' }}
+{%- endif %}
diff --git a/c/tests/test_cli_output.py b/c/tests/test_cli_output.py
index 82f04522..9a973483 100644
--- a/c/tests/test_cli_output.py
+++ b/c/tests/test_cli_output.py
@@ -297,9 +297,9 @@ class BannerModelLineTest(unittest.TestCase):
def test_unknown_model_states_its_own_type(self):
"""No forcing into the roster: an unknown checkpoint speaks for itself."""
- line = self.line({"model_type": "qwen3_moe", "num_hidden_layers": 48,
+ line = self.line({"model_type": "mixtral", "num_hidden_layers": 48,
"n_routed_experts": 128})
- self.assertIn("qwen3_moe", line)
+ self.assertIn("mixtral", line)
self.assertIn("48L x 128E", line)
self.assertNotIn("GLM", line)
diff --git a/c/tests/test_family_registry.py b/c/tests/test_family_registry.py
index 49b7a5de..d5d42148 100644
--- a/c/tests/test_family_registry.py
+++ b/c/tests/test_family_registry.py
@@ -144,7 +144,7 @@ class FamilyRegistryTest(unittest.TestCase):
def test_unknown_or_invalid_config_never_falls_back_to_glm(self):
with self.assertRaises(UnknownFamilyError):
- family_for_config({"model_type": "qwen3_moe"})
+ family_for_config({"model_type": "mixtral"})
for config in ({}, {"model_type": ""}, {"model_type": []}, None):
with self.subTest(config=config), self.assertRaises(FamilyConfigError):
family_for_config(config)
@@ -185,7 +185,7 @@ class FamilyRegistryTest(unittest.TestCase):
self.assertEqual(geometry.configured_experts, 8)
self.assertEqual(geometry.context_state_bytes, 16_384)
self.assertEqual(geometry.fixed_state_bytes, 6 * (8 * 8 * 8 + 128 * 3) * 4)
- for model_type in ("qwen2", "qwen3_moe", "my_qwen_model"):
+ for model_type in ("qwen2", "qwen3", "my_qwen_model"):
self.assertNotIn(model_type, by_type)
def test_qwen38_fixture_resolves_nested_text_config_and_sizes_all_state(self):
@@ -1080,7 +1080,7 @@ class FamilyRegistryTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "config.json").write_text(
- json.dumps({"model_type": "qwen3_moe"}), encoding="utf-8")
+ json.dumps({"model_type": "mixtral"}), encoding="utf-8")
(root / "tokenizer.json").write_text("{}", encoding="utf-8")
report = run_doctor(root, engine_path=root / "colibri",
available_memory=16_000_000_000,
@@ -1585,7 +1585,10 @@ class DisplayVariantTest(unittest.TestCase):
def test_variants_are_public(self):
meta = public_metadata(next(f for f in FAMILIES if f.id == "qwen36"))
self.assertEqual([v["display_name"] for v in meta["display_variants"]],
- ["Qwen3.6-35B-A3B", "Qwen3.8-2.4T-A95B", "Qwen3.8-27B"])
+ ["Qwen3.6-35B-A3B", "Qwen3.8-2.4T-A95B", "Qwen3.8-27B",
+ "Qwen3-Coder-30B-A3B", "Qwen3-Coder-REAP-25B-A3B"])
+ self.assertEqual(meta["display_variants"][3]["geometry"],
+ {"num_hidden_layers": 48, "num_experts": 128, "hidden_size": 2048})
self.assertEqual(meta["display_variants"][2]["geometry"],
{"num_hidden_layers": 64, "hidden_size": 5120, "intermediate_size": 17408})
self.assertEqual(meta["display_variants"][1]["geometry"],
diff --git a/c/tests/test_qwen3_coder_chat_template.py b/c/tests/test_qwen3_coder_chat_template.py
new file mode 100644
index 00000000..8c7bb78d
--- /dev/null
+++ b/c/tests/test_qwen3_coder_chat_template.py
@@ -0,0 +1,185 @@
+#!/usr/bin/env python3
+"""The gateway's Qwen3-Coder renderer against the release's own template.
+
+The gateway renders prompts by hand instead of running jinja per request, and a hand copy
+can drift from the original while the model keeps answering. Here the real template
+(tests/fixtures/qwen3_coder_chat_template.jinja, Apache-2.0, from
+Qwen/Qwen3-Coder-30B-A3B-Instruct, sha256 5a38bfa0...) is rendered with jinja2 the way
+transformers does and compared byte for byte with render_chat_qwen3_coder.
+
+One documented difference: an OpenAI client sends tool-call arguments as a JSON string,
+which the template cannot iterate; the gateway reads it as the object it is. The reference
+side of each case gets the object.
+"""
+import copy
+import hashlib
+import json
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+HERE = Path(__file__).resolve().parent
+sys.path.insert(0, str(HERE.parent))
+
+import openai_server # noqa: E402
+
+TEMPLATE = HERE / "fixtures" / "qwen3_coder_chat_template.jinja"
+TEMPLATE_SHA256 = "5a38bfa05833266240066aedc497decc9b00cc0d3e3b8cceea98cf530196ab06"
+
+try:
+ import jinja2
+ import jinja2.sandbox
+except ImportError: # pragma: no cover
+ jinja2 = None
+
+WEATHER = {"type": "function", "function": {
+ "name": "get_weather", "description": " Current weather of a city. ",
+ "parameters": {"type": "object", "properties": {
+ "city": {"type": "string", "description": "the city"},
+ "days": {"type": "integer", "minimum": 1},
+ "units": {"type": "array", "items": {"type": "string"}, "enum": [["C"], ["F"]]}},
+ "required": ["city"]}}}
+SEARCH = {"type": "function", "function": {
+ "name": "search", "strict": True, "parameters": {"type": "object", "properties": {
+ "query": {"type": "string"}}}}}
+
+CASES = {
+ "one user turn": {"messages": [{"role": "user", "content": "ciao"}]},
+ "system and user": {"messages": [
+ {"role": "system", "content": "Be brief."},
+ {"role": "user", "content": "Capital of France?"}]},
+ "history": {"messages": [
+ {"role": "user", "content": "1+1?"},
+ {"role": "assistant", "content": "2"},
+ {"role": "user", "content": "and 2+2?"}]},
+ "tools, no system": {"tools": [WEATHER, SEARCH], "messages": [
+ {"role": "user", "content": "Weather in Rome?"}]},
+ "tools and a system": {"tools": [WEATHER], "messages": [
+ {"role": "system", "content": "Use the tools."},
+ {"role": "user", "content": "Weather in Rome?"}]},
+ "an agent loop": {"tools": [WEATHER, SEARCH], "messages": [
+ {"role": "user", "content": "Weather in Rome and Milan for 3 days?"},
+ {"role": "assistant", "content": "Checking both.", "tool_calls": [
+ {"id": "a", "type": "function", "function": {
+ "name": "get_weather", "arguments": {"city": "Rome", "days": 3,
+ "units": ["C", "mm"]}}},
+ {"id": "b", "type": "function", "function": {
+ "name": "get_weather", "arguments": {"city": "Milan", "days": 3}}}]},
+ {"role": "tool", "tool_call_id": "a", "content": "sun, 24 C"},
+ {"role": "tool", "tool_call_id": "b", "content": "rain, 17 C"},
+ {"role": "assistant", "content": "Rome is sunny, Milan rainy."},
+ {"role": "user", "content": "And Naples?"}]},
+ "a tool result last": {"tools": [SEARCH], "messages": [
+ {"role": "user", "content": "Find colibri"},
+ {"role": "assistant", "content": "", "tool_calls": [
+ {"function": {"name": "search", "arguments": {"query": "colibri"}}}]},
+ {"role": "tool", "content": "a pure-C engine"}]},
+}
+
+
+def reference_render(case, add_generation_prompt=True):
+ """The template, rendered the way transformers' apply_chat_template does."""
+ env = jinja2.sandbox.ImmutableSandboxedEnvironment(
+ trim_blocks=True, lstrip_blocks=True, extensions=["jinja2.ext.loopcontrols"])
+
+ def tojson(x, ensure_ascii=False, indent=None, separators=None, sort_keys=False):
+ return json.dumps(x, ensure_ascii=ensure_ascii, indent=indent,
+ separators=separators, sort_keys=sort_keys)
+ env.filters["tojson"] = tojson
+ template = env.from_string(TEMPLATE.read_text(encoding="utf-8"))
+ kwargs = {"messages": case["messages"], "add_generation_prompt": add_generation_prompt}
+ if case.get("tools"):
+ kwargs["tools"] = case["tools"]
+ return template.render(**kwargs)
+
+
+def as_client_sends(messages):
+ """The same history with tool-call arguments as JSON strings, the OpenAI shape."""
+ out = copy.deepcopy(messages)
+ for message in out:
+ for call in message.get("tool_calls") or []:
+ fn = call["function"]
+ if isinstance(fn.get("arguments"), dict):
+ fn["arguments"] = json.dumps(fn["arguments"], ensure_ascii=False)
+ return out
+
+
[email protected](jinja2, "jinja2 is not installed")
+class Qwen3CoderTemplateTest(unittest.TestCase):
+
+ def setUp(self):
+ self.saved = (openai_server.ARCH, openai_server.CHAT_FLAVOR)
+ openai_server.ARCH, openai_server.CHAT_FLAVOR = "qwen36", "qwen3_coder"
+
+ def tearDown(self):
+ openai_server.ARCH, openai_server.CHAT_FLAVOR = self.saved
+
+ def test_template_fixture_is_the_release(self):
+ self.assertEqual(hashlib.sha256(TEMPLATE.read_bytes()).hexdigest(), TEMPLATE_SHA256)
+
+ def test_byte_identical(self):
+ for name, case in CASES.items():
+ with self.subTest(name):
+ want = reference_render(case)
+ got = openai_server.render_chat_qwen3_coder(
+ as_client_sends(case["messages"]), tools=case.get("tools"))
+ self.assertEqual(got, want)
+
+ def test_dispatch_never_thinks(self):
+ for thinking in (False, True):
+ prompt = openai_server.render_chat_for_arch(
+ [{"role": "user", "content": "ciao"}], enable_thinking=thinking)
+ self.assertEqual(prompt, "<|im_start|>user\nciao<|im_end|>\n<|im_start|>assistant\n")
+
+ def test_open_turn_is_the_past_turn_without_its_terminator(self):
+ case = {"messages": [{"role": "user", "content": "Write a loop"},
+ {"role": "assistant", "content": "for i in"}]}
+ full = reference_render(case, add_generation_prompt=False)
+ self.assertTrue(full.endswith("<|im_end|>\n"))
+ got = openai_server.render_chat_qwen3_coder(case["messages"],
+ add_generation_prompt=False)
+ self.assertEqual(got, full[:-len("<|im_end|>\n")])
+
+ def test_tool_choice_none_offers_no_tools(self):
+ got = openai_server.render_chat_qwen3_coder([{"role": "user", "content": "x"}],
+ tools=[WEATHER], tool_choice="none")
+ self.assertNotIn("<tools>", got)
+
+ def test_bad_arguments_are_a_400(self):
+ messages = [{"role": "assistant", "content": "", "tool_calls": [
+ {"function": {"name": "f", "arguments": "{not json"}}]}]
+ with self.assertRaises(openai_server.APIError) as caught:
+ openai_server.render_chat_qwen3_coder(messages)
+ self.assertEqual(caught.exception.status, 400)
+
+ def test_its_calls_parse_with_types_from_the_schema(self):
+ reply = ("Let me check.\n<tool_call>\n<function=get_weather>\n<parameter=city>\nRome\n"
+ "</parameter>\n<parameter=days>\n3\n</parameter>\n</function>\n</tool_call>")
+ text, calls = openai_server.parse_arch_tool_calls(reply, [WEATHER])
+ self.assertEqual(text.strip(), "Let me check.")
+ self.assertEqual(json.loads(calls[0]["function"]["arguments"]), {"city": "Rome", "days": 3})
+
+ def test_a_python_style_boolean_is_a_boolean(self):
+ # seen on the real model: the template prints past bools as True/False
+ tool = {"type": "function", "function": {"name": "run_tests", "parameters": {
+ "type": "object", "properties": {"verbose": {"type": "boolean"},
+ "workers": {"type": "integer"}}}}}
+ reply = ("<tool_call>\n<function=run_tests>\n<parameter=verbose>\nTrue\n</parameter>\n"
+ "<parameter=workers>\n4\n</parameter>\n</function>\n</tool_call>")
+ _text, calls = openai_server.parse_arch_tool_calls(reply, [tool])
+ self.assertEqual(json.loads(calls[0]["function"]["arguments"]),
+ {"verbose": True, "workers": 4})
+
+
+class Qwen3CoderFlavorTest(unittest.TestCase):
+
+ def test_the_template_names_the_flavor(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ Path(tmp, "chat_template.jinja").write_bytes(TEMPLATE.read_bytes())
+ self.assertEqual(openai_server.detect_chat_flavor("qwen36", tmp), "qwen3_coder")
+ self.assertIsNone(openai_server.detect_chat_flavor("mimo", tmp))
+
+
+if __name__ == "__main__":
+ unittest.main()