diff --git a/serve/frontend.py b/serve/frontend.py
index 29f8febe55..adf627afbe 100644
--- a/serve/frontend.py
+++ b/serve/frontend.py
@@ -1103,6 +1103,31 @@ def lenient_json(text: str):
return v
+def declared_type(prop, depth: int = 0):
+ """#2371: a parameter's declared JSON type(s) for coerce_value: its "type", or the union of its anyOf / oneOf
+ branches' types (["boolean", "null"] for {"anyOf": [{"type": "boolean"}, {"type": "null"}]}, a nested branch
+ included). None when a branch names no type (a $ref, an open schema): the value is then read as with no schema.
+ Callers used to read the top-level "type" only, so a boolean declared through anyOf / oneOf stayed the text "True"."""
+ if not isinstance(prop, dict) or depth > 8:
+ return None
+ t = prop.get("type")
+ if t is not None:
+ return t
+ for key in ("anyOf", "oneOf"):
+ branches = prop.get(key)
+ if isinstance(branches, list) and branches:
+ out = []
+ for b in branches:
+ bt = declared_type(b, depth + 1)
+ if bt is None:
+ return None
+ for x in bt if isinstance(bt, list) else [bt]:
+ if x not in out:
+ out.append(x)
+ return out[0] if len(out) == 1 else out
+ return None
+
+
def coerce_value(value: str, declared, notes: list | None = None):
"""A non-string parameter's text -> its value by the tool's declared type: JSON when it parses; else an array or an
object read with lenient_json (a note says so in `notes`); else the text itself, as before. An array or object that
@@ -1154,7 +1179,7 @@ def parse_tool_call(body: str, schema: dict | None = None, notes: list | None =
value = value[1:]
if value.endswith("\n"):
value = value[:-1]
- args[pname] = coerce_value(value, (props.get(pname) or {}).get("type"), notes)
+ args[pname] = coerce_value(value, declared_type(props.get(pname)), notes)
return ToolCall(name=name, arguments=args)
@@ -1507,7 +1532,7 @@ class OutputParser:
return out
self.scall = ToolCall(name=name, arguments={})
props = ((self.schemas.get(name) or {}).get("parameters") or {}).get("properties") or {}
- self.sdeclared = {k: (v or {}).get("type") for k, v in props.items()}
+ self.sdeclared = {k: declared_type(v) for k, v in props.items()}
out.append(Event("tool_start", call=self.scall))
args("{")
self.sp += b + 1
diff --git a/serve/test_bool_args.py b/serve/test_bool_args.py
index 6419d28a7f..520193c703 100644
--- a/serve/test_bool_args.py
+++ b/serve/test_bool_args.py
@@ -32,5 +32,58 @@ class BooleanArguments(unittest.TestCase):
self.assertEqual(calls[0].call.arguments["title"], "True")
+# #2371: the type declared through anyOf / oneOf (what Pydantic and many MCP servers write for Optional[bool], unions)
+UNION = {"name": "set_flags", "parameters": {"type": "object", "properties": {
+ "any_bool": {"anyOf": [{"type": "boolean"}, {"type": "null"}]},
+ "one_bool": {"oneOf": [{"type": "boolean"}, {"type": "null"}], "description": "Optional[bool]"},
+ "nested": {"anyOf": [{"oneOf": [{"type": "integer"}, {"type": "boolean"}]}, {"type": "null"}]},
+ "text_or_bool": {"anyOf": [{"type": "boolean"}, {"type": "string"}]},
+ "by_ref": {"anyOf": [{"$ref": "#/$defs/Flag"}, {"type": "null"}]},
+ "count": {"oneOf": [{"type": "integer"}, {"type": "null"}]}}}}
+UNION_CALL = ("\n\nTrue\n\n\nfalse\n\n"
+ "\nTrue\n\n\nTrue\n\n"
+ "\nTrue\n\n\n42\n\n")
+UNION_WANT = {"any_bool": True, "one_bool": False, "nested": True, "text_or_bool": "True", "by_ref": "True", "count": 42}
+
+
+class UnionTypes(unittest.TestCase):
+ def test_declared_type_reads_the_branches(self):
+ from serve.frontend import declared_type
+ props = UNION["parameters"]["properties"]
+ self.assertEqual(declared_type(props["any_bool"]), ["boolean", "null"])
+ self.assertEqual(declared_type(props["one_bool"]), ["boolean", "null"])
+ self.assertEqual(declared_type(props["nested"]), ["integer", "boolean", "null"])
+ self.assertEqual(declared_type(props["text_or_bool"]), ["boolean", "string"])
+ self.assertIsNone(declared_type(props["by_ref"]), "a $ref branch: unknown, read as with no schema")
+ self.assertEqual(declared_type({"type": "boolean", "anyOf": [{"type": "string"}]}), "boolean", "an explicit type wins")
+ self.assertEqual(declared_type({"anyOf": [{"type": "boolean"}]}), "boolean", "one branch: that type")
+ self.assertIsNone(declared_type({"anyOf": []}))
+ self.assertIsNone(declared_type("boolean"))
+ deep = {"type": "boolean"}
+ for _ in range(20):
+ deep = {"anyOf": [deep]}
+ self.assertIsNone(declared_type(deep), "a schema nested past the bound is read as with no schema")
+
+ def test_a_whole_call_reads_union_booleans(self):
+ self.assertEqual(parse_tool_call(UNION_CALL, UNION).arguments, UNION_WANT)
+
+ def test_the_streamed_parser_reads_union_booleans(self):
+ parser = OutputParser(thinking=False, tools=[UNION], stream_tools=True)
+ events = parser.feed("\n" + UNION_CALL + "\n") + parser.finish()
+ calls = [e for e in events if e.kind == "tool_call"]
+ self.assertEqual(len(calls), 1, events)
+ self.assertEqual(calls[0].call.arguments, UNION_WANT)
+
+ def test_the_streamed_parser_in_pieces(self):
+ parser = OutputParser(thinking=False, tools=[UNION], stream_tools=True)
+ events = []
+ text = "\n" + UNION_CALL + "\n"
+ for i in range(0, len(text), 7):
+ events += parser.feed(text[i:i + 7])
+ events += parser.finish()
+ calls = [e for e in events if e.kind == "tool_call"]
+ self.assertEqual([c.call.arguments for c in calls], [UNION_WANT])
+
+
if __name__ == "__main__":
unittest.main()