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

api: `tool_choice: "required"` instructs the model on every engine

Yi-111-a803c244cea10
diff --git a/c/openai_server.py b/c/openai_server.py
index fdab0091..0410b4a8 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -546,6 +546,24 @@ def _project_span(span_map, start, end):
return (low, high) if high > low else None
+# `tool_choice: "required"` as one line of prompt, for every renderer that has a tool
+# block to put it after. It is a prompt-level instruction and nothing more: no
+# renderer here constrains sampling, so a model that answers in prose anyway has done
+# nothing the API promised would be impossible. Grammar forcing is not a remedy -- that
+# path feeds a draft the engine then verifies, so it degrades to "no speedup" rather
+# than to an enforced tool call.
+#
+# One constant, because it used to live in three copies (GLM-5.2, DeepSeek V4,
+# DeepSeek V4.1) and be absent from four renderers that accept `required`, render the
+# tools and then drop the instruction on the floor -- the "accepted in silence" outcome
+# #1698 rules out. A renderer with no tool block to attach it to (Inkling, OLMoE
+# without COLI_TOOL_FALLBACK) answers HTTP 400 instead, the honest third outcome. Kimi
+# K3 spells its own, because its wire format carries a dedicated tool-choice message
+# rather than prose.
+TOOL_CHOICE_REQUIRED_INSTRUCTION = (
+ "\n\nYou must call one of the functions above. Do not answer directly.")
+
+
def _tool_choice_name(tool_choice):
"""The tool name a dict `tool_choice` forces, or None.
@@ -1571,7 +1589,7 @@ def render_chat_v4(messages, enable_thinking=False, reasoning_effort=None, tools
if forced:
tools_text += f"\n\nYou must call the function `{forced}`. Do not answer directly."
elif tool_choice == "required":
- tools_text += "\n\nYou must call one of the functions above. Do not answer directly."
+ tools_text += TOOL_CHOICE_REQUIRED_INSTRUCTION
for msg in merged:
if msg["role"] in ("system", "developer"):
msg["content"] += "\n\n" + tools_text
@@ -1644,7 +1662,11 @@ def render_chat_olmoe(messages, enable_thinking=False, reasoning_effort=None, to
boundary = "|||IP_ADDRESS|||" # bos_token == eos_token in this tokenizer
parts = [boundary]
if tools and _TOOL_FALLBACK:
- parts.append(f"<|system|>\n{_fallback_tool_preamble(tools)}\n")
+ # Fallback only: without it, `required` is already a 400 above.
+ preamble = _fallback_tool_preamble(tools)
+ if tool_choice == "required":
+ preamble += TOOL_CHOICE_REQUIRED_INSTRUCTION
+ parts.append(f"<|system|>\n{preamble}\n")
last = len(messages) - 1
for index, message in enumerate(messages):
if not isinstance(message, dict):
@@ -1737,6 +1759,8 @@ def render_chat_qwen(messages, enable_thinking=False, reasoning_effort=None, too
start = 1
if tools:
block = _qwen_tool_block(tools)
+ if tool_choice == "required":
+ block += TOOL_CHOICE_REQUIRED_INSTRUCTION
if system_text:
block += "\n\n" + system_text
parts.append(f"<|im_start|>system\n{block}<|im_end|>\n")
@@ -1991,6 +2015,8 @@ def render_chat_qwen38(messages, enable_thinking=True, reasoning_effort=None, to
# text last -- not the other way round.
head = (instruction + "\n\n") if instruction else ""
block = head + _qwen_tool_block(tools)
+ if tool_choice == "required":
+ block += TOOL_CHOICE_REQUIRED_INSTRUCTION
if system_text:
block += "\n\n" + system_text
parts.append(f"<|im_start|>system\n{block}<|im_end|>\n")
@@ -2185,7 +2211,7 @@ def render_chat(messages, enable_thinking=False, reasoning_effort=None, tools=No
if forced:
prompt.append(f"\n\nYou must call the function `{forced}`. Do not answer directly.")
elif tool_choice == "required":
- prompt.append("\n\nYou must call one of the functions above. Do not answer directly.")
+ prompt.append(TOOL_CHOICE_REQUIRED_INSTRUCTION)
prev_tool = False
for index, message in enumerate(messages):
if not isinstance(message, dict):
@@ -2625,6 +2651,11 @@ def render_chat_glm53(messages, enable_thinking=False, reasoning_effort=None, to
prompt.append(f"<|system|>Reasoning Effort: {effort}")
if tools:
prompt.append(_glm53_tool_block(tools))
+ # Fuori dal blocco, non dentro: il blocco e' confrontato byte a byte con
+ # jinja2 (glm53_chat_template_harness.py) e l'istruzione non c'e' nel
+ # template, quindi va dopo -- come fa render_chat per GLM-5.2.
+ if tool_choice == "required":
+ prompt.append(TOOL_CHOICE_REQUIRED_INSTRUCTION)
for index, message in enumerate(messages):
if not isinstance(message, dict):
@@ -2823,7 +2854,7 @@ def render_chat_dsv41(messages, enable_thinking=False, reasoning_effort=None, to
if forced:
tools_text += f"\n\nYou must call the function `{forced}`. Do not answer directly."
elif tool_choice == "required":
- tools_text += "\n\nYou must call one of the functions above. Do not answer directly."
+ tools_text += TOOL_CHOICE_REQUIRED_INSTRUCTION
for turn in turns:
if turn["role"] == "system":
turn["content"] += "\n\n" + tools_text
diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.py
index 72eaf450..56f17fe4 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -38,7 +38,8 @@ from openai_server import (APIError, APIHandler, APIServer, ClientCancelled,
parse_tool_calls_spans, parse_arch_tool_calls_spans,
THINK_OPEN, THINK_CLOSE,
_compose_span_maps, _cut_span_map, _project_span,
- stop_policy, tune_child_env)
+ stop_policy, tune_child_env,
+ TOOL_CHOICE_REQUIRED_INSTRUCTION)
def echo_record(pos, data, lp, topk):
@@ -3660,6 +3661,86 @@ class ToolChoiceTest(unittest.TestCase):
with self.assertRaises(APIError):
generation_options({"messages": [], "tool_choice": "required"}, 128)
+ # #1698 Tier 2: `required` was prompt-level on four renderers and dropped by
+ # the four others that accept it, so a client got the tools and no instruction
+ # to use them. These pin the one-line difference per renderer, because the
+ # failure is silent: the request succeeds, the model just answers in prose.
+
+ # The renderers that render a tool block and therefore have somewhere to put
+ # the instruction. Kimi K3 is absent on purpose: its wire format carries a
+ # dedicated tool-choice message rather than prose (tested below).
+ BLOCK_RENDERERS = (render_chat, render_chat_v4, render_chat_dsv41, render_chat_qwen,
+ render_chat_qwen38, render_chat_glm53)
+
+ def _auto_and_required(self, render, **kwargs):
+ messages = [{"role": "user", "content": "Where is order 7?"}]
+ return (render(messages, tools=ORDER_TOOL, tool_choice="auto", **kwargs),
+ render(messages, tools=ORDER_TOOL, tool_choice="required", **kwargs))
+
+ def test_required_is_auto_plus_exactly_the_instruction(self):
+ for render in self.BLOCK_RENDERERS:
+ with self.subTest(renderer=render.__name__):
+ auto, required = self._auto_and_required(render)
+ self.assertEqual(required.count(TOOL_CHOICE_REQUIRED_INSTRUCTION), 1)
+ # Nothing else moves: the instruction is the whole difference
+ # between `auto` and `required` on every one of these families.
+ self.assertEqual(
+ required.replace(TOOL_CHOICE_REQUIRED_INSTRUCTION, "", 1), auto)
+
+ def test_required_instruction_comes_after_the_declarations(self):
+ # "the functions above" has to be true, so the instruction goes after the
+ # last declared function and not inside or before the tool block.
+ for render in self.BLOCK_RENDERERS:
+ with self.subTest(renderer=render.__name__):
+ _, required = self._auto_and_required(render)
+ self.assertLess(required.rindex("lookup_order"),
+ required.index(TOOL_CHOICE_REQUIRED_INSTRUCTION))
+
+ def test_required_sits_between_the_tool_block_and_the_system_text(self):
+ # Qwen3.6/3.8 build one system turn as [tool block][client system text];
+ # the instruction belongs to the tool half, so it goes between the two
+ # and not after the client's own text.
+ system = [{"role": "system", "content": "Be terse."},
+ {"role": "user", "content": "Where is order 7?"}]
+ for render in (render_chat_qwen, render_chat_qwen38):
+ with self.subTest(renderer=render.__name__):
+ required = render(system, tools=ORDER_TOOL, tool_choice="required")
+ self.assertLess(required.index("# Tools"),
+ required.index(TOOL_CHOICE_REQUIRED_INSTRUCTION))
+ self.assertLess(required.index(TOOL_CHOICE_REQUIRED_INSTRUCTION),
+ required.index("Be terse."))
+
+ def test_olmoe_required_under_the_tool_fallback(self):
+ with patch("openai_server._TOOL_FALLBACK", True):
+ auto, required = self._auto_and_required(render_chat_olmoe)
+ self.assertEqual(required.count(TOOL_CHOICE_REQUIRED_INSTRUCTION), 1)
+ self.assertEqual(required.replace(TOOL_CHOICE_REQUIRED_INSTRUCTION, "", 1), auto)
+ self.assertLess(required.rindex("lookup_order"),
+ required.index(TOOL_CHOICE_REQUIRED_INSTRUCTION))
+
+ def test_olmoe_without_the_fallback_refuses_required(self):
+ # No tool block, so no instruction to give: the honest outcome is the
+ # 400, not a `required` that is accepted and then ignored.
+ with patch("openai_server._TOOL_FALLBACK", False):
+ with self.assertRaisesRegex(APIError, "Tool use"):
+ render_chat_olmoe([{"role": "user", "content": "Hi"}], tools=ORDER_TOOL,
+ tool_choice="required")
+
+ def test_inkling_refuses_required(self):
+ with self.assertRaisesRegex(APIError, "not wired up"):
+ render_chat_inkling([{"role": "user", "content": "Hi"}], tools=ORDER_TOOL,
+ tool_choice="required")
+
+ def test_kimi_keeps_its_own_required_wording(self):
+ # Same policy, different wire format: a dedicated tool-choice message
+ # instead of prose appended to the tool block.
+ required = render_chat_kimi([{"role": "user", "content": "Hi"}],
+ tools=ORDER_TOOL, tool_choice="required")
+ self.assertIn("tool-choiceThe system is invoked with `tool_choice=required`.",
+ required)
+ self.assertIn("You MUST call tools in the next message.", required)
+ self.assertNotIn(TOOL_CHOICE_REQUIRED_INSTRUCTION, required)
+
class TrailingAssistantTurnTest(unittest.TestCase):
"""A trailing `assistant` message is a turn to CONTINUE, not one already finished.
diff --git a/docs/api.md b/docs/api.md
index fce28398..2b046992 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -232,6 +232,7 @@ Known limitations, current build:
| Engine | OpenAI `tools` | Anthropic `tool_use` | Native format |
|---|---|---|---|
| GLM-5.2 (`colibri`) | yes | yes | `<tool_call>` blocks |
+| GLM-5.3-Flash | yes | yes | `<tool_call>` blocks, with the 5.3 declaration block |
| DeepSeek V4 | yes | yes | native DSML tool-call blocks |
| Inkling | no | no | active tool declarations/choices return HTTP 400 |
| Kimi K3 | yes | yes | native XTML `tools`/`call`/`argument` blocks (#1143) |
@@ -247,6 +248,22 @@ Protocol support does not guarantee that every quantized model emits valid
tool syntax; `COLI_TOOL_SALVAGE=1` is an opt-in recovery path for malformed GLM
int4 tool calls. DeepSeek V4 uses its strict native DSML parser instead.
+`tool_choice: "required"` is a prompt-level instruction, not a sampling
+constraint. Every renderer that offers a tool block appends the same one line to
+it, and no renderer filters tokens or forces the sampler, so a model that
+answers in prose anyway has done nothing the API said was impossible.
+Grammar forcing is not a remedy: that path feeds a draft the engine then
+verifies, so a schema the engine cannot compile costs the speedup and nothing
+else. An engine with no tool block to attach the instruction to (Inkling, OLMoE
+without `COLI_TOOL_FALLBACK=1`) answers HTTP 400 rather than accept the choice
+and ignore it.
+
+A forced choice, `tool_choice: {"type": "function", "function": {"name": …}}`,
+is applied per engine and the engines do not agree on how: some narrow the
+offered tools to the named one, some keep the full list and name the tool in
+prose instead, and some (Qwen3.6, Qwen3.8) do neither. Read the rendered prompt
+rather than assuming the request was honoured.
+
When a reverse proxy or MagicDNS hostname preserves a public `Host` header,
trust that exact hostname with repeatable `--allowed-host` options. The
comma-separated `COLI_ALLOWED_HOSTS` environment variable is equivalent: