diff --git a/src/aviary/tools/base.py b/src/aviary/tools/base.py index 14f40081..0ee0eee0 100644 --- a/src/aviary/tools/base.py +++ b/src/aviary/tools/base.py @@ -111,9 +111,9 @@ def from_tool(cls, tool: "Tool", *args, id: str | None = None, **kwargs) -> Self ) @classmethod - def from_name(cls, function_name: str, **kwargs) -> Self: + def from_name(cls, function_name: str, id: str | None = None, **kwargs) -> Self: # noqa: A002 return cls( - id=cls.generate_id(), + id=id or cls.generate_id(), function=ToolCallFunction(name=function_name, arguments=kwargs), ) diff --git a/tests/test_tools.py b/tests/test_tools.py index 23187176..9a012f7a 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -608,7 +608,7 @@ async def test_arg_types(self) -> None: tool._tool_fn(**call.function.arguments) @pytest.mark.asyncio - async def test_tool_serialization( + async def test_tool_serialization( # noqa: PLR0915 self, dummy_env: DummyEnv, subtests: SubTests ) -> None: def get_todo_list(n: int): @@ -662,6 +662,15 @@ def get_todo_list(n: int): new_messages = await dummy_env.exec_tool_calls(action) assert new_messages[0].content == "Go for a walk\nRead a book" + with subtests.test("tool call from name with custom id"): + custom_id = "custom123" + tool_call = ToolCall.from_name("get_todo_list", id=custom_id, n=2) + assert tool_call.id == custom_id + action = ToolRequestMessage(tool_calls=[tool_call]) + new_messages = await dummy_env.exec_tool_calls(action) + assert new_messages[0].content == "Go for a walk\nRead a book" + assert new_messages[0].tool_call_id == custom_id + with subtests.test("tool call from tool"): tool_call = ToolCall.from_tool(tool, n=2) action = ToolRequestMessage(tool_calls=[tool_call])