Skip to content

Your lightweight open-source personal agent.

fix(tools): accept integral JSON numbers for integer parameters

Shoaib9fe061a5371e
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
[email protected]("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"})