Skip to content
Open
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
61 changes: 39 additions & 22 deletions tests/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1803,10 +1803,6 @@ def test_toolcalling_agent_api_misformatted_output(self, mock_inference_client):
assert "Error while parsing" in capture.get()
assert len(agent.memory.steps) == 4

@pytest.mark.skip(
reason="Test is not properly implemented (GH-1255) because fake_tools should have the same name. "
"Additionally, it uses CodeAgent instead of ToolCallingAgent (GH-1409)"
)
def test_change_tools_after_init(self):
Copy link
Contributor Author

@suryabdev suryabdev Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #1571, it was mentioned that the tests were flaky

even though they may pass or fail inconsistently.

Do you remember the error message in the failed runs? I ran the tests multiple times (Ran the entire tests/test_agents.py file a few times, mentioned details in the PR description). test_change_tools_after_init used to always pass

from smolagents import tool

Expand All @@ -1815,21 +1811,45 @@ def fake_tool_1() -> str:
"""Fake tool"""
return "1"

class FakeToolCallModel(Model):
def generate(self, messages, stop_sequences=None, tools_to_call_from=None):
if len(messages) < 3:
return ChatMessage(
role=MessageRole.ASSISTANT,
content="I will call the fake tool.",
tool_calls=[
ChatMessageToolCall(
id="call_0",
type="function",
function=ChatMessageToolCallFunction(name="fake_tool_1", arguments={}),
)
],
)
else:
return ChatMessage(
role=MessageRole.ASSISTANT,
content="I will return the final answer.",
tool_calls=[
ChatMessageToolCall(
id="call_1",
type="function",
function=ChatMessageToolCallFunction(name="final_answer", arguments={"answer": "2"}),
)
],
)

agent = ToolCallingAgent(tools=[fake_tool_1], model=FakeToolCallModel())

@tool
def fake_tool_2() -> str:
def fake_tool_1() -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For

Test is not properly implemented (GH-1255) because fake_tools should have the same name.

Is this the expected change? Should I use the class method instead to create a fake tool with the same name?
If I understand correctly we want to replace an existing tool
https://huggingface.co/docs/smolagents/en/tutorials/tools#manage-your-agents-toolbox

"""Fake tool"""
return "2"

class FakeCodeModel(Model):
def generate(self, messages, stop_sequences=None):
return ChatMessage(role=MessageRole.ASSISTANT, content="<code>\nfinal_answer(fake_tool_1())\n</code>")

agent = CodeAgent(tools=[fake_tool_1], model=FakeCodeModel())

agent.tools["final_answer"] = CustomFinalAnswerTool()
agent.tools["fake_tool_1"] = fake_tool_2
agent.tools["fake_tool_1"] = fake_tool_1

answer = agent.run("Fake task.")
assert "2" in agent.memory.steps[1].observations
assert answer == "2CUSTOM"

def test_custom_final_answer_with_custom_inputs(self, test_tool):
Expand Down Expand Up @@ -2094,9 +2114,6 @@ def test_end_code_appending(self):
assert messages
assert all(m.content.endswith("</code>") for m in messages)

@pytest.mark.skip(
reason="Test is not properly implemented (GH-1255) because fake_tools should have the same name. "
)
def test_change_tools_after_init(self):
from smolagents import tool

Expand All @@ -2105,19 +2122,19 @@ def fake_tool_1() -> str:
"""Fake tool"""
return "1"

@tool
def fake_tool_2() -> str:
"""Fake tool"""
return "2"

class FakeCodeModel(Model):
def generate(self, messages, stop_sequences=None):
return ChatMessage(role=MessageRole.ASSISTANT, content="<code>\nfinal_answer(fake_tool_1())\n</code>")

agent = CodeAgent(tools=[fake_tool_1], model=FakeCodeModel())

agent.tools["final_answer"] = CustomFinalAnswerTool()
agent.tools["fake_tool_1"] = fake_tool_2

@tool
def fake_tool_1() -> str:
"""Fake tool"""
return "2"

agent.tools["fake_tool_1"] = fake_tool_1

answer = agent.run("Fake task.")
assert answer == "2CUSTOM"
Expand Down