Skip to content

fix: Allow Agent to run with no tools #9230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 7 additions & 3 deletions haystack/components/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ def __init__(
component.set_input_type(self, name=param, type=config["type"], default=None)
component.set_output_types(self, **output_types)

self._tool_invoker = ToolInvoker(tools=self.tools, raise_on_failure=self.raise_on_tool_invocation_failure)
if self.tools:
self._tool_invoker = ToolInvoker(tools=self.tools, raise_on_failure=self.raise_on_tool_invocation_failure)
else:
logger.warning("No tools provided to the Agent. It will only return text responses.")
self._tool_invoker = None
self._is_warmed_up = False

def warm_up(self) -> None:
Expand Down Expand Up @@ -224,8 +228,8 @@ def run(
llm_messages = self.chat_generator.run(messages=messages, **generator_inputs)["replies"]
state.set("messages", llm_messages)

# 2. Check if any of the LLM responses contain a tool call
if not any(msg.tool_call for msg in llm_messages):
# 2. Check if any of the LLM responses contain a tool call or if the LLM is not using tools
if not any(msg.tool_call for msg in llm_messages) or self._tool_invoker is None:
return {**state.data}

# 3. Call the ToolInvoker
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Now when you call an Agent with no tools it acts like a ChatGenerator which means it returns a ChatMessage based on a user input.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can update the Agent's docstring to also highlight this update.

23 changes: 23 additions & 0 deletions test/components/agents/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,29 @@ def test_exit_conditions_checked_across_all_llm_messages(self, monkeypatch, weat
== "{'weather': 'mostly sunny', 'temperature': 7, 'unit': 'celsius'}"
)

def test_agent_with_no_tools(self, monkeypatch):
monkeypatch.setenv("OPENAI_API_KEY", "fake-key")
generator = OpenAIChatGenerator()

# Mock messages where the exit condition appears in the second message
mock_messages = [ChatMessage.from_assistant("Berlin")]

agent = Agent(chat_generator=generator, tools=[], max_agent_steps=3)
agent.warm_up()

# Patch agent.chat_generator.run to return mock_messages
agent.chat_generator.run = MagicMock(return_value={"replies": mock_messages})

response = agent.run([ChatMessage.from_user("What is the capital of Germany?")])

assert isinstance(response, dict)
assert "messages" in response
assert isinstance(response["messages"], list)
assert len(response["messages"]) == 2
assert [isinstance(reply, ChatMessage) for reply in response["messages"]]
assert response["messages"][0].text == "What is the capital of Germany?"
assert response["messages"][1].text == "Berlin"

@pytest.mark.skipif(not os.environ.get("OPENAI_API_KEY"), reason="OPENAI_API_KEY not set")
@pytest.mark.integration
def test_run(self, weather_tool):
Expand Down
Loading