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 5 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
20 changes: 15 additions & 5 deletions haystack/components/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Agent:
The component processes messages and executes tools until a exit_condition condition is met.
The exit_condition can be triggered either by a direct text response or by invoking a specific designated tool.

When you call an Agent without any tools it acts like a ChatGenerator. It will exit after generating a text
response.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dfokina Could you give this docstring a review?

Copy link
Contributor

Choose a reason for hiding this comment

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

I propose to order it a bit: "When you call an Agent without tools, it acts as a ChatGenerator, produces one response, then exits."


### Usage example
```python
from haystack.components.agents import Agent
Expand Down Expand Up @@ -129,7 +132,14 @@ 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. The Agent will behave like a ChatGenerator and only return text "
"responses. To enable tool usage, pass tools directly to the Agent, not to the chat_generator."
)
self._tool_invoker = None
self._is_warmed_up = False

def warm_up(self) -> None:
Expand Down Expand Up @@ -224,8 +234,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 Expand Up @@ -300,8 +310,8 @@ async def run_async(

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.

25 changes: 25 additions & 0 deletions test/components/agents/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,31 @@ 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, caplog):
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")]

with caplog.at_level("WARNING"):
agent = Agent(chat_generator=generator, tools=[], max_agent_steps=3)
agent.warm_up()
assert "No tools provided to the Agent." in caplog.text

# 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