From 5be3d0dffad6f2d1d8ac87f7d919001fd84278aa Mon Sep 17 00:00:00 2001 From: Shanto Date: Mon, 12 Oct 2026 16:36:42 +0600 Subject: [PATCH] Keep the JSON schema of dict-schema tools in FunctionTool.to_dict --- .../packages/core/agent_framework/_tools.py | 2 +- python/packages/core/tests/core/test_tools.py | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/python/packages/core/agent_framework/_tools.py b/python/packages/core/agent_framework/_tools.py index e508a4cc51e..eab23d5699f 100644 --- a/python/packages/core/agent_framework/_tools.py +++ b/python/packages/core/agent_framework/_tools.py @@ -1429,7 +1429,7 @@ def to_json_schema_spec(self) -> dict[str, Any]: @override def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True) -> dict[str, Any]: as_dict = super().to_dict(exclude=exclude, exclude_none=exclude_none) - if (exclude and "input_model" in exclude) or not self.input_model: + if exclude and "input_model" in exclude: return as_dict as_dict["input_model"] = self.parameters() # Use cached parameters() return as_dict diff --git a/python/packages/core/tests/core/test_tools.py b/python/packages/core/tests/core/test_tools.py index cbbe57edb60..c75d3ec88a5 100644 --- a/python/packages/core/tests/core/test_tools.py +++ b/python/packages/core/tests/core/test_tools.py @@ -1306,6 +1306,29 @@ def serialize_test(x: int, y: int) -> int: assert restored_tool_2(10, 4) == 6 +@pytest.mark.parametrize("with_func", [True, False]) +def test_tool_serialization_with_json_schema_input_model(with_func: bool): + """Test that a tool declared with a JSON schema keeps its parameters through serialization.""" + schema = { + "type": "object", + "properties": {"city": {"type": "string"}}, + "required": ["city"], + } + + def get_weather(city: str) -> str: + return f"Sunny in {city}" + + func = get_weather if with_func else None + weather_tool = FunctionTool(name="get_weather", description="Get the weather", func=func, input_model=schema) + + tool_dict = weather_tool.to_dict() + assert tool_dict["input_model"] == schema + + restored_tool = FunctionTool.from_dict(tool_dict, dependencies={"function_tool": {"func": func}}) + assert restored_tool.parameters() == schema + assert restored_tool.func is func + + # region _parse_inputs tests