Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: a malformed "tools" value is a 400, not a dropped connection (#592)

Niko1221b92b78b09296
diff --git a/serve/frontend.py b/serve/frontend.py
index 07e369d0a..007d06df2 100644
--- a/serve/frontend.py
+++ b/serve/frontend.py
@@ -160,6 +160,27 @@ def _object_list(value, name: str) -> list[dict]:
return value
+def _tool_list(value, wrapper: str | None) -> list[dict]:
+ """#592: a request's "tools" as a list of tool objects, each with a name - in the OpenAI shape
+ {"type": "function", "function": {"name": ...}} (`wrapper` "function"; a bare {"name": ...} is still taken), or
+ in the Anthropic shape {"name": ...} (`wrapper` None). A value that is not (a string such as "auto", a list of
+ names, an object without a name) is a ValueError - a 400 naming the field - where it used to take the request
+ thread down with no reply at all. No value (or an empty one) is no tools, as always."""
+ if not value:
+ return []
+ shape = ('{"type": "function", "function": {"name": ..., "parameters": {...}}}' if wrapper else
+ '{"name": ..., "input_schema": {...}}')
+ try:
+ tools = _object_list(value, "tools")
+ except ValueError:
+ raise ValueError(f"tools must be a list of tool objects, each {shape}") from None
+ for i, t in enumerate(tools):
+ fn = t.get(wrapper, t) if wrapper and t.get("type") == wrapper else t
+ if not isinstance(fn, dict) or not isinstance(fn.get("name"), str) or not fn["name"]:
+ raise ValueError(f"tools[{i}] has no name: each tool must be {shape}")
+ return tools
+
+
def openai_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dict]:
"""OpenAI Chat Completions -> (template messages, template tools, template kwargs)."""
messages = []
@@ -182,8 +203,8 @@ def openai_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dict]:
calls.append({"function": {"name": fn.get("name"), "arguments": args or {}}})
out["tool_calls"] = calls
messages.append(out)
- tools = [t.get("function", t) if isinstance(t, dict) and t.get("type") == "function" else t
- for t in req.get("tools") or []] or None
+ tools = [t.get("function", t) if t.get("type") == "function" else t
+ for t in _tool_list(req.get("tools"), "function")] or None
kwargs = {}
# OpenAI Chat Completions: "reasoning_effort"; Responses style: "reasoning": {"effort": ...}
reasoning = req.get("reasoning") if isinstance(req.get("reasoning"), dict) else {}
@@ -233,7 +254,7 @@ def anthropic_to_messages(req: dict, think_unasked: bool = True) -> tuple[list[d
out["tool_calls"] = calls
messages.append(out)
tools = [{"name": t["name"], "description": t.get("description", ""), "parameters": t.get("input_schema", {})}
- for t in req.get("tools") or []] or None
+ for t in _tool_list(req.get("tools"), None)] or None
kwargs = {}
# Anthropic: "thinking": {"type": "disabled"} or {"type": "enabled", "budget_tokens": N};
# "output_config": {"effort": "low" | "medium" | "high"}
diff --git a/serve/server.py b/serve/server.py
index 46e3fc369..606bad5dd 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -2419,7 +2419,7 @@ def make_handler(svc: Service):
svc.load()
max_req = max_new = int(req.get("max_completion_tokens") or req.get("max_tokens") or 0) # 0/-1: the rest
use_mcp = req.get("strata_mcp") is True and svc.mcp is not None # the web app's opt-in (serve/mcp.py)
- own = {t.get("name") for t in tools or []}
+ own = {t.get("name") for t in tools or [] if isinstance(t, dict)} # #592: a second line of defence
if use_mcp:
if not self._own_page("MCP tools can be used"): # tools run with the user's rights on this PC
return
diff --git a/serve/test_server.py b/serve/test_server.py
index 60a192696..51101e695 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -499,6 +499,36 @@ class ClientShapes(unittest.TestCase):
self.assertEqual(status, 400, b)
self.assertIn("messages must be a list of objects", b["error"]["message"])
+ def test_malformed_tools_are_a_400(self):
+ # #592: a "tools" value that is not a list of named tool objects is a 400 naming the field, on both APIs,
+ # not a dropped connection (an AttributeError/TypeError in the request thread)
+ msgs = [{"role": "user", "content": "hi"}]
+ bad = ("auto", ["get_weather"], [{"description": "no name"}], {"name": "x"},
+ [{"type": "function", "function": "get_weather"}], [{"type": "function", "function": {"name": ""}}])
+ for path in ("/v1/chat/completions", "/v1/messages"):
+ for tools in bad:
+ with self.subTest(path=path, tools=tools):
+ status, b = self.post(path, {"model": "x", "max_tokens": 8, "messages": msgs, "tools": tools})
+ self.assertEqual(status, 400, b)
+ self.assertIn("tools", b["error"]["message"])
+ status, b = self.post("/v1/chat/completions", {"model": "x", "max_tokens": 8, "messages": msgs})
+ self.assertEqual(status, 200, b) # the server goes on
+
+ def test_well_formed_tools_still_work(self):
+ msgs = [{"role": "user", "content": "hi"}]
+ fn = {"name": "get_weather", "description": "the weather", "parameters": {"type": "object", "properties": {}}}
+ for path, tools in (("/v1/chat/completions", [{"type": "function", "function": fn}]),
+ ("/v1/chat/completions", [fn]), # the bare shape some clients send
+ ("/v1/chat/completions", json.dumps([{"type": "function", "function": fn}])),
+ ("/v1/chat/completions", []), ("/v1/chat/completions", None),
+ ("/v1/messages", [{"name": "get_weather", "input_schema": {"type": "object"}}]),
+ ("/v1/messages", [])):
+ with self.subTest(path=path, tools=tools):
+ status, b = self.post(path, {"model": "x", "max_tokens": 8, "messages": msgs, "tools": tools})
+ self.assertEqual(status, 200, b)
+ if tools:
+ self.assertIn("get_weather", self.prompt_text())
+
def test_vision_temp_image_removed_when_the_pipe_fails(self):
# #352: the temporary image goes even when the encoder's pipe raises
from serve.server import Vision