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 tool's "parameters" / "input_schema" that is not an object is a 400 naming it, not an AttributeError on its first call (#1175)

Avicennasis20ebd1ebf4e2
diff --git a/serve/frontend.py b/serve/frontend.py
index d2d90f6cd..beedd5fdc 100644
--- a/serve/frontend.py
+++ b/serve/frontend.py
@@ -320,7 +320,9 @@ def _tool_list(value, wrapper: str | None) -> list[dict]:
{"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."""
+ thread down with no reply at all. No value (or an empty one) is no tools, as always. A tool's schema -
+ "parameters" in the OpenAI shape, "input_schema" in the Anthropic one - is an object when it is there, for the
+ same reason: a string or a list passed and raised later instead, on the model's first call of that tool."""
if not value:
return []
shape = ('{"type": "function", "function": {"name": ..., "parameters": {...}}}' if wrapper else
@@ -329,10 +331,17 @@ def _tool_list(value, wrapper: str | None) -> list[dict]:
tools = _object_list(value, "tools")
except ValueError:
raise ValueError(f"tools must be a list of tool objects, each {shape}") from None
+ key = "parameters" if wrapper else "input_schema"
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}")
+ schema = fn.get(key)
+ if schema is not None and not isinstance(schema, dict):
+ # it reached parse_tool_call and the stream parser as the tool's schema, whose .get("properties") raised
+ # AttributeError in the request thread - after the 200 and whatever the model had said before the call
+ raise ValueError(f'tools[{i}] ({fn["name"]}): "{key}" must be an object (the JSON schema of its '
+ f"parameters), not {type(schema).__name__}")
return tools
diff --git a/serve/test_server.py b/serve/test_server.py
index 427f0aa2d..c510d9db7 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -1227,6 +1227,30 @@ class ClientShapes(unittest.TestCase):
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_a_tool_schema_that_is_not_an_object_is_a_400(self):
+ # #592 follow-up: a tool whose "parameters" (OpenAI) / "input_schema" (Anthropic) is a string, a number or a
+ # list passed the check above and raised later instead - AttributeError in parse_tool_call / the stream
+ # parser's `.get("properties")` on the model's first call of that tool, after the 200 and part of the reply
+ # had gone out. Now a 400 naming the tool and the field, before anything is sent.
+ msgs = [{"role": "user", "content": "hi"}]
+ bad = {"/v1/chat/completions": ([{"type": "function", "function": {"name": "f", "parameters": "x"}}],
+ [{"name": "f", "parameters": ["a"]}], [{"name": "f", "parameters": 5}]),
+ "/v1/messages": ([{"name": "f", "input_schema": "x"}], [{"name": "f", "input_schema": [1]}],
+ [{"name": "f", "input_schema": True}])}
+ for path, shapes in bad.items():
+ for tools in shapes:
+ 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[0] (f)", b["error"]["message"])
+ self.assertIn("must be an object", b["error"]["message"])
+ for path, tools in (("/v1/chat/completions", [{"name": "f"}]), # absent, null and {} go on
+ ("/v1/chat/completions", [{"name": "f", "parameters": None}]),
+ ("/v1/messages", [{"name": "f", "input_schema": {}}])):
+ 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)
+
def test_well_formed_tools_still_work(self):
msgs = [{"role": "user", "content": "hi"}]
fn = {"name": "get_weather", "description": "the weather", "parameters": {"type": "object", "properties": {}}}