From 9fe061a5371ebddbe6a2c8a361c2fc293f98328a Mon Sep 17 00:00:00 2001 From: Shoaib Date: Fri, 16 Oct 2026 15:17:43 +0500 Subject: [PATCH] fix(tools): accept integral JSON numbers for integer parameters JSON Schema accepts numbers such as 3.0 as integers, but Python decodes that representation as float. Tool calls with valid integral arguments were therefore rejected before execution, including nullable and union integer schemas. Share the integer check across schema validation and union matching, then convert integral floats to int before calling the tool. Keep fractional values, booleans, and non-finite numbers outside the integer type. Add schema and registry regressions for integral JSON numbers. Validation: 144 tool-validation tests; Ruff; BasedPyright; import-boundary checks; git diff --check. --- nanobot/agent/tools/base.py | 14 +++++++++++--- tests/tools/test_tool_validation.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/nanobot/agent/tools/base.py b/nanobot/agent/tools/base.py index 277bc21be..9614f02a0 100644 --- a/nanobot/agent/tools/base.py +++ b/nanobot/agent/tools/base.py @@ -27,6 +27,12 @@ _JSON_TYPE_MAP: dict[str, type | tuple[type, ...]] = { } +def _is_json_integer(value: object) -> bool: + return not isinstance(value, bool) and ( + isinstance(value, int) or (isinstance(value, float) and value.is_integer()) + ) + + class Schema(ABC): """Abstract base for JSON Schema fragments describing tool parameters. @@ -51,6 +57,8 @@ class Schema(ABC): continue if t in ("integer", "number") and isinstance(val, bool): continue + if t == "integer" and _is_json_integer(val): + return t if isinstance(val, _JSON_TYPE_MAP[t]): return t return None @@ -87,7 +95,7 @@ class Schema(ABC): t = Schema.match_json_schema_type(val, types) if t is None: return [f"{label} should match one of the types {types}"] - if t == "integer" and (not isinstance(val, int) or isinstance(val, bool)): + if t == "integer" and not _is_json_integer(val): return [f"{label} should be integer"] if t == "number" and ( not isinstance(val, _JSON_TYPE_MAP["number"]) or isinstance(val, bool) @@ -297,8 +305,8 @@ class Tool(ABC): if t == "boolean" and isinstance(val, bool): return val - if t == "integer" and isinstance(val, int) and not isinstance(val, bool): - return val + if t == "integer" and _is_json_integer(val): + return int(val) if isinstance(val, float) else val if t in self._TYPE_MAP and t not in ("boolean", "integer", "array", "object"): expected = self._TYPE_MAP[t] if isinstance(val, expected): diff --git a/tests/tools/test_tool_validation.py b/tests/tools/test_tool_validation.py index 10fb7bcac..aa5be9483 100644 --- a/tests/tools/test_tool_validation.py +++ b/tests/tools/test_tool_validation.py @@ -158,6 +158,20 @@ async def test_registry_executes_decorated_tool_end_to_end() -> None: assert "Invalid parameters" in err +@pytest.mark.parametrize("schema_type", ["integer", ["integer", "null"], ["integer", "string"]]) +def test_integer_schema_accepts_integral_json_numbers(schema_type) -> None: + assert Schema.validate_json_schema_value(3.0, {"type": schema_type}) == [] + + +async def test_registry_casts_integral_json_number_to_integer() -> None: + reg = ToolRegistry() + reg.register(DecoratedSampleTool()) + + result = await reg.execute("decorated_sample", {"query": "hello", "count": 3.0}) + + assert result == "ok:3" + + def test_validate_params_missing_required() -> None: tool = SampleTool() errors = tool.validate_params({"query": "hi"})