Skip to content

Build and run agents you can see, understand and trust.

fix(utils): apply the NaN guard on the schema-less parse path too (#3626)

ffffff557cb5a7f7f3ba
diff --git a/src/agentscope/_utils/_common.py b/src/agentscope/_utils/_common.py
index 8b2b4456b..1f7586a11 100644
--- a/src/agentscope/_utils/_common.py
+++ b/src/agentscope/_utils/_common.py
@@ -138,7 +138,18 @@ def _json_loads_with_repair(
)
elif schema is None:
- return parsed
+ try:
+ # NaN and Infinity are accepted as numbers by jsonschema, but
+ # silently bypass the minimum/maximum constraints. Apply the
+ # same guard as the schema path so both paths treat them
+ # identically.
+ json.dumps(parsed, allow_nan=False)
+ except ValueError:
+ error_message = (
+ "Error: NaN and Infinity are not valid JSON numbers."
+ )
+ else:
+ return parsed
except json.JSONDecodeError as e:
error_message = (
f"Error: When decoding your tool arguments from JSON format "
diff --git a/tests/tool_argument_repair_test.py b/tests/tool_argument_repair_test.py
index 14826e74a..072532bf5 100644
--- a/tests/tool_argument_repair_test.py
+++ b/tests/tool_argument_repair_test.py
@@ -140,6 +140,13 @@ class JsonLoadsWithRepairTest(unittest.TestCase):
},
)
+ # The same guard applies on the schema-less path: its early return
+ # used to bypass it, letting NaN reserialize into invalid JSON
+ for json_str in ['{"ratio": NaN}', '{"ratio": Infinity}']:
+ with self.subTest(json_str=json_str, schema=None):
+ with self.assertRaises(ToolJSONDecodeError):
+ _json_loads_with_repair(json_str)
+
class ToolCallArgumentRepairTest(IsolatedAsyncioTestCase):
"""Unittest for repairing the arguments within an agent reply."""