Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/packages/core/agent_framework/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions python/packages/core/tests/core/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading