tool calls: a parameter's type declared through anyOf / oneOf is read (#2371)

coerce_value reads a parameter's value by its declared JSON type (a boolean's "True" is the boolean,
a number is a number), but both callers - parse_tool_call and the streamed OutputParser - took only the
property's top-level "type". A parameter declared as {"anyOf": [{"type": "boolean"}, {"type": "null"}]}
(Pydantic's Optional[bool], many MCP servers) came back as the text "True". The plain boolean and
type-list cases were fixed before (#5979); this residual of #2371 was not.

declared_type(prop) gives the property's "type", or the union of its anyOf / oneOf branches' types
(nested branches included, bounded at depth 8); a branch without a type (a $ref, an open schema) makes
it None, read as with no schema. Both callers use it, so a whole call and a streamed call read the same.

Test: serve/test_bool_args.py, class UnionTypes: anyOf / oneOf booleans with null, a nested union, a
boolean-or-string union (stays text), a $ref branch (no schema), an integer union; whole call, streamed
call, streamed in 7-character pieces. l620 (job l620-20261116-1516-1f08): with the 17 other tool-call and
parser test files, 166 passed; on the previous commit the 4 UnionTypes tests fail.
This commit is contained in:
Niko1221 committed 2026-11-16 15:35:55 +01:00
1 parent 02deda1305
commit a54f9dbbb7
2 files changed
+80 -2

No files matched your search

+27 -2
View File
@@ -1179,6 +1179,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
@@ -1230,7 +1255,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)
@@ -1583,7 +1608,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
+53
View File
@@ -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 = ("<function=set_flags>\n<parameter=any_bool>\nTrue\n</parameter>\n<parameter=one_bool>\nfalse\n</parameter>\n"
"<parameter=nested>\nTrue\n</parameter>\n<parameter=text_or_bool>\nTrue\n</parameter>\n"
"<parameter=by_ref>\nTrue\n</parameter>\n<parameter=count>\n42\n</parameter>\n</function>")
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("<tool_call>\n" + UNION_CALL + "\n</tool_call>") + 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 = "<tool_call>\n" + UNION_CALL + "\n</tool_call>"
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()