okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0215-boolargs: serve: a declared boolean tool argument written True / False is the boolean (#5979)f5b255007

main
2.0 KiB36 linesraw
1"""#5979: a declared boolean parameter came back as the string "True" / "False" (the model's Python spelling), not as true / false."""
2
3import unittest
4
5from serve.frontend import OutputParser, coerce_value, parse_tool_call
6
7TOOL = {"name": "write_note", "parameters": {"type": "object", "properties": {
8    "formatted": {"type": "boolean"}, "title": {"type": "string"}, "pinned": {"type": ["boolean", "null"]},
9    "label": {"type": ["boolean", "string"]}}}}
10
11
12class BooleanArguments(unittest.TestCase):
13    def test_the_python_spelling_of_a_declared_boolean_is_the_boolean(self):
14        for text, want in (("True", True), ("False", False), ("true", True), ("false", False), (" TRUE ", True)):
15            self.assertIs(coerce_value(text, "boolean"), want, text)
16        self.assertIs(coerce_value("False", ["boolean", "null"]), False)
17
18    def test_strings_and_other_words_are_left_as_they_were(self):
19        self.assertEqual(coerce_value("True", "string"), "True")
20        self.assertEqual(coerce_value("True", ["boolean", "string"]), "True", "a string is allowed: the text stays text")
21        self.assertEqual(coerce_value("yes", "boolean"), "yes")
22        self.assertEqual(coerce_value("True", None), "True", "no schema: as before")
23
24    def test_a_whole_call_and_the_streamed_parser_give_booleans(self):
25        body = "<function=write_note>\n<parameter=formatted>\nTrue\n</parameter>\n<parameter=title>\nTrue\n</parameter>\n</function>"
26        self.assertEqual(parse_tool_call(body, TOOL).arguments, {"formatted": True, "title": "True"})
27        parser = OutputParser(thinking=False, tools=[TOOL], stream_tools=True)
28        events = parser.feed("<tool_call>\n" + body + "\n</tool_call>") + parser.finish()
29        calls = [e for e in events if e.kind == "tool_call"]
30        self.assertEqual(len(calls), 1, events)
31        self.assertEqual(calls[0].call.arguments["formatted"], True)
32        self.assertEqual(calls[0].call.arguments["title"], "True")
33
34
35if __name__ == "__main__":
36    unittest.main()