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 tool whose function is not an object answered 500

kevin9327c64c8d6f01ac
diff --git a/c/openai_server.py b/c/openai_server.py
index 76b4a904..94fd6967 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -392,6 +392,21 @@ def _tool_choice_name(tool_choice):
or tool_choice.get("name"))
+def _tool_function(tool):
+ """The function object on a tools[] entry, or {} if it is missing or not an object.
+
+ OpenAI dual spelling: {"function": {"name": ...}} or a bare function object.
+ .items() is taken only from a dict. Writing the name where the object goes
+ ({"type": "function", "function": "search"}) raised AttributeError in the GLM
+ and DeepSeek declaration blocks, 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 tool_choice fix (#1598): read the
+ member, then check it.
+ """
+ fn = tool.get("function", tool) if isinstance(tool, dict) else {}
+ return fn if isinstance(fn, dict) else {}
+
+
def _tool_param_order(tools):
"""name -> ordered param names (required first) from the request schema, for de-mangling."""
out = {}
@@ -564,7 +579,7 @@ def _dsv4_tools_block(tools):
"""V4 tool-declaration block, rendered by the vendored reference template."""
schemas = []
for tool in (tools or []):
- fn = tool.get("function", tool) if isinstance(tool, dict) else {}
+ fn = _tool_function(tool)
# Gateway-side scrub: OpenAI clients attach routing hints the model
# schema must not carry.
schemas.append({k: v for k, v in fn.items() if k not in ("defer_loading", "strict")})
@@ -1798,7 +1813,7 @@ def render_chat(messages, enable_thinking=False, reasoning_effort=None, tools=No
"user query.\n\nYou are provided with function signatures within <tools></tools> "
"XML tags:\n<tools>\n")
for tool in tools:
- fn = tool.get("function", tool) if isinstance(tool, dict) else {}
+ fn = _tool_function(tool)
clean = {k: v for k, v in fn.items() if k not in ("defer_loading", "strict")}
prompt.append(json.dumps(clean, ensure_ascii=False) + "\n")
prompt.append("</tools>\n\nFor each function call, output the function name and arguments "
@@ -2124,8 +2139,7 @@ def _glm53_tool_block(tools):
somiglia a quello dell'addestramento non e' quello dell'addestramento."""
body = "".join(f"\n{_glm53_tool_json(tool)}\n\n"
for tool in tools
- if not (isinstance(tool, dict)
- and (tool.get("function", tool) or {}).get("defer_loading")))
+ if not _tool_function(tool).get("defer_loading"))
return GLM53_TOOL_PREAMBLE + body + GLM53_TOOL_EPILOGUE
@@ -2305,7 +2319,7 @@ def _dsv41_tools_block(tools):
"""V4.1 tool-declaration block, rendered by the vendored reference template."""
schemas = []
for tool in (tools or []):
- fn = tool.get("function", tool) if isinstance(tool, dict) else {}
+ fn = _tool_function(tool)
# Gateway-side scrub: OpenAI clients attach routing hints the model
# schema must not carry.
clean = {k: v for k, v in fn.items() if k not in ("defer_loading", "strict")}
diff --git a/c/tests/test_openai_server.py b/c/tests/test_openai_server.py
index afe7b80b..c6ce0e2c 100644
--- a/c/tests/test_openai_server.py
+++ b/c/tests/test_openai_server.py
@@ -458,6 +458,33 @@ class TemplateTest(unittest.TestCase):
render_chat(list(messages), tools=list(tools),
tool_choice=choice))
+ def test_tool_function_that_is_not_an_object_is_a_400(self):
+ """The same slip as tool_choice, on tools[] this time.
+
+ generation_options already has 400 "Tool function must be an object".
+ The GLM and DeepSeek declaration blocks then did fn.items() on
+ {"type": "function", "function": "search"} and raised AttributeError
+ before that 400 ran, so do_POST answered 500 "engine failed".
+ """
+ import openai_server as srv
+ messages = [{"role": "user", "content": "hi"}]
+ for function in ("search", ["search"], 5, True):
+ with self.subTest(function=function):
+ tools = [{"type": "function", "function": function}]
+ with self.assertRaises(APIError) as caught:
+ generation_options({"tools": tools}, 8)
+ self.assertEqual(caught.exception.status, 400)
+ self.assertEqual(caught.exception.param, "tools.0.function")
+ for render in (render_chat, srv.render_chat_glm53, render_chat_kimi,
+ render_chat_v4, srv.render_chat_dsv41):
+ try:
+ render(list(messages), tools=list(tools))
+ except APIError as error:
+ self.assertEqual(error.status, 400)
+ well = [{"type": "function", "function": {"name": "search"}}]
+ generation_options({"tools": well}, 8)
+ self.assertIn('"name": "search"', render_chat(list(messages), tools=well))
+
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)
@@ -1980,6 +2007,44 @@ class HTTPTest(unittest.TestCase):
self.assertEqual(response.status, 200)
self.assertIn("You must call the function `search`", self.engine.calls[-1][0])
+ def test_tool_with_a_non_object_function_is_a_client_error(self):
+ """{"type": "function", "function": "search"} on tools[] answered HTTP 500.
+
+ generation_options already has the 400, but /v1/chat/completions renders
+ first. GLM, GLM-5.3, DeepSeek V4 and V4.1 did fn.items() on a string
+ and the AttributeError became do_POST's 500 "The colibri engine failed
+ to process the request." for a request the engine never saw.
+ """
+ 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": [{"type": "function",
+ "function": function}]})
+ self.addCleanup(caught.exception.close)
+ self.assertEqual(caught.exception.code, 400)
+ with self.assertRaises(HTTPError) as caught:
+ self.request("/v1/completions", {
+ "model": "test-model", "prompt": "hi",
+ "tools": [{"type": "function", "function": "search"}]})
+ self.addCleanup(caught.exception.close)
+ self.assertEqual(caught.exception.code, 400)
+
+ def test_a_well_formed_tool_function_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"}}]}) as response:
+ self.assertEqual(response.status, 200)
+ self.assertIn('"name": "search"', self.engine.calls[-1][0])
+
def test_tool_call_arguments_that_are_not_an_object_do_not_fail_the_request(self):
"""A replayed tool call with `arguments: "[1, 2]"` answered HTTP 500.