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

fix(serve): a non-object tool_choice.function answered 500 "engine failed"

kevin9327966801a05038
diff --git a/c/openai_server.py b/c/openai_server.py
index 59890c21..9f385390 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -259,6 +259,22 @@ _PARTIAL_END_RE = re.compile(r"<(?:/(?:t(?:o(?:o(?:l(?:_(?:c(?:a(?:l)?)?)?)?)?)?
_SALVAGE = os.environ.get("COLI_TOOL_SALVAGE", "0") == "1"
+def _tool_choice_name(tool_choice):
+ """The tool name a dict `tool_choice` forces, or None.
+
+ `.get` is taken only from a dict. Writing the name where the object goes --
+ {"type": "function", "function": "search"} instead of
+ {"function": {"name": "search"}} -- raised AttributeError in the five
+ renderers that read this and in generation_options() itself, and do_POST
+ answered HTTP 500 "The colibri engine failed to process the request." for a
+ payload generation_options() already has a 400 for. Same shape as the
+ json_schema fix (#1587): read the member, then check it.
+ """
+ function = tool_choice.get("function")
+ return ((function if isinstance(function, dict) else {}).get("name")
+ or tool_choice.get("name"))
+
+
def _tool_param_order(tools):
"""name -> ordered param names (required first) from the request schema, for de-mangling."""
out = {}
@@ -992,7 +1008,7 @@ def render_chat_kimi(messages, enable_thinking=False, reasoning_effort=None, too
raise APIError(400, "`messages` must be a non-empty array.", "messages")
forced = None
if isinstance(tool_choice, dict):
- forced = ((tool_choice.get("function") or {}).get("name") or tool_choice.get("name"))
+ forced = _tool_choice_name(tool_choice)
if forced:
tools = [t for t in (tools or [])
if ((t.get("function", t) if isinstance(t, dict) else {}).get("name") == forced)]
@@ -1083,8 +1099,7 @@ def render_chat_v4(messages, enable_thinking=False, reasoning_effort=None, tools
raise APIError(400, "`messages` must be a non-empty array.", "messages")
forced = None
if isinstance(tool_choice, dict):
- forced = ((tool_choice.get("function") or {}).get("name")
- or tool_choice.get("name"))
+ forced = _tool_choice_name(tool_choice)
if forced:
tools = [t for t in (tools or [])
if ((t.get("function", t) if isinstance(t, dict) else {}).get("name") == forced)]
@@ -1554,8 +1569,7 @@ def render_chat(messages, enable_thinking=False, reasoning_effort=None, tools=No
prompt.append(f"<|system|>Reasoning Effort: {effort}")
forced = None
if isinstance(tool_choice, dict):
- forced = ((tool_choice.get("function") or {}).get("name")
- or tool_choice.get("name"))
+ forced = _tool_choice_name(tool_choice)
if forced:
tools = [t for t in (tools or [])
if ((t.get("function", t) if isinstance(t, dict) else {}).get("name") == forced)]
@@ -1938,8 +1952,7 @@ def render_chat_glm53(messages, enable_thinking=False, reasoning_effort=None, to
forced = None
if isinstance(tool_choice, dict):
- forced = ((tool_choice.get("function") or {}).get("name")
- or tool_choice.get("name"))
+ forced = _tool_choice_name(tool_choice)
if forced:
tools = [t for t in (tools or [])
if ((t.get("function", t) if isinstance(t, dict) else {}).get("name") == forced)]
@@ -2138,8 +2151,7 @@ def render_chat_dsv41(messages, enable_thinking=False, reasoning_effort=None, to
raise APIError(400, "`messages` must be a non-empty array.", "messages")
forced = None
if isinstance(tool_choice, dict):
- forced = ((tool_choice.get("function") or {}).get("name")
- or tool_choice.get("name"))
+ forced = _tool_choice_name(tool_choice)
if forced:
tools = [t for t in (tools or [])
if ((t.get("function", t) if isinstance(t, dict) else {}).get("name") == forced)]
@@ -2659,7 +2671,7 @@ def generation_options(body, limit):
raise APIError(400, "`tool_choice` must be one of \"auto\", \"none\", \"required\", "
"or a function object.", "tool_choice", "unsupported_value")
elif isinstance(choice, dict):
- name = (choice.get("function") or {}).get("name") or choice.get("name")
+ name = _tool_choice_name(choice)
if not name:
raise APIError(400, "`tool_choice` function object must include a name.",
"tool_choice", "invalid_value")
diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.py
index fa525d86..75e691e7 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -384,6 +384,39 @@ class TemplateTest(unittest.TestCase):
opts = generation_options({"response_format": {"type": "gbnf", "grammar": "not a grammar ::="}}, 8)
self.assertEqual(opts[3], "not a grammar ::=")
+ def test_tool_choice_function_that_is_not_an_object_is_a_400(self):
+ """The same read, six places: generation_options and five renderers.
+
+ `(tool_choice.get("function") or {}).get("name")` raised AttributeError
+ on {"type": "function", "function": "search"} -- the name written where
+ the object goes -- and that became a 500 "engine failed" instead of the
+ 400 generation_options already had two lines further down. The renderers
+ crashed on the identical expression before validation was even reached.
+ """
+ import openai_server as srv
+ tools = [{"type": "function", "function": {"name": "search"}}]
+ messages = [{"role": "user", "content": "hi"}]
+ for function in ("search", ["search"], 5, True):
+ with self.subTest(function=function):
+ choice = {"type": "function", "function": function}
+ with self.assertRaises(APIError) as caught:
+ generation_options({"tools": tools, "tool_choice": choice}, 8)
+ self.assertEqual(caught.exception.status, 400)
+ self.assertEqual(caught.exception.param, "tool_choice")
+ # the renderers must get past the read so that 400 is what runs
+ for render in (render_chat, srv.render_chat_glm53, render_chat_kimi,
+ render_chat_v4, srv.render_chat_dsv41):
+ render(list(messages), tools=list(tools), tool_choice=choice)
+ # A well-formed choice, and the legacy {"type": "function", "name": ...}
+ # spelling, still force the tool.
+ for choice in ({"type": "function", "function": {"name": "search"}},
+ {"type": "function", "name": "search"}):
+ with self.subTest(choice=choice):
+ generation_options({"tools": tools, "tool_choice": choice}, 8)
+ self.assertIn("You must call the function `search`",
+ render_chat(list(messages), tools=list(tools),
+ tool_choice=choice))
+
def test_coli_temp_is_the_default_for_requests_that_omit_temperature(self):
with patch.dict("openai_server.os.environ", {"COLI_TEMP": "0.25"}):
self.assertEqual(generation_options({}, 8)[1], 0.25)
@@ -1539,6 +1572,60 @@ class HTTPTest(unittest.TestCase):
self.addCleanup(caught.exception.close)
self.assertEqual(caught.exception.code, 400)
+ def test_tool_choice_with_a_non_object_function_is_a_client_error(self):
+ """{"type": "function", "function": "search"} answered HTTP 500.
+
+ generation_options() already has the 400 for a `tool_choice` function
+ object with no name, but it -- and the five renderers that read the
+ forced tool the same way -- did `(tool_choice.get("function") or {})
+ .get("name")`, so a `function` that is not an object raised
+ AttributeError first and do_POST answered 500 "The colibri engine failed
+ to process the request." for a request the engine never saw. Every arch
+ and both OpenAI endpoints, because the crash was on the read.
+ """
+ tools = [{"type": "function", "function": {"name": "search"}}]
+ for arch in ("glm", "glm53", "kimi", "deepseek_v4", "deepseek_v41",
+ "olmoe", "inkling", "qwen36", "qwen38"):
+ for function in ("search", ["search"], 5):
+ with self.subTest(arch=arch, function=function):
+ with patch("openai_server.ARCH", arch):
+ with self.assertRaises(HTTPError) as caught:
+ self.request("/v1/chat/completions", {
+ "model": "test-model",
+ "messages": [{"role": "user", "content": "hi"}],
+ "tools": tools,
+ "tool_choice": {"type": "function", "function": function}})
+ self.addCleanup(caught.exception.close)
+ self.assertEqual(caught.exception.code, 400)
+ # /v1/completions has no renderer in front of generation_options, so it
+ # reached the same read directly.
+ with self.assertRaises(HTTPError) as caught:
+ self.request("/v1/completions", {
+ "model": "test-model", "prompt": "hi", "tools": tools,
+ "tool_choice": {"type": "function", "function": "search"}})
+ self.addCleanup(caught.exception.close)
+ self.assertEqual(caught.exception.code, 400)
+
+ def test_a_well_formed_forced_tool_choice_still_runs(self):
+ """The read above must not change the shape clients actually send."""
+ with self.request("/v1/chat/completions", {
+ "model": "test-model",
+ "messages": [{"role": "user", "content": "hi"}],
+ "tools": [{"type": "function", "function": {"name": "search"}}],
+ "tool_choice": {"type": "function",
+ "function": {"name": "search"}}}) as response:
+ self.assertEqual(response.status, 200)
+ self.assertIn("You must call the function `search`", self.engine.calls[-1][0])
+ # The OpenAI-legacy spelling {"type": "function", "name": ...} still
+ # forces the tool: _tool_choice_name keeps that fallback.
+ with self.request("/v1/chat/completions", {
+ "model": "test-model",
+ "messages": [{"role": "user", "content": "hi"}],
+ "tools": [{"type": "function", "function": {"name": "search"}}],
+ "tool_choice": {"type": "function", "name": "search"}}) as response:
+ self.assertEqual(response.status, 200)
+ self.assertIn("You must call the function `search`", self.engine.calls[-1][0])
+
class ClientHangupTest(unittest.TestCase):
"""A client that disconnects mid-response must not print a traceback.