Skip to content

Commit 22739a9

Browse files
committed
Update docstring and warning message
1 parent 9d0fce2 commit 22739a9

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

haystack/components/agents/agent.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Agent:
3030
The component processes messages and executes tools until a exit_condition condition is met.
3131
The exit_condition can be triggered either by a direct text response or by invoking a specific designated tool.
3232
33+
When you call an Agent without any tools it acts like a ChatGenerator. It will exit after generating a text
34+
response.
35+
3336
### Usage example
3437
```python
3538
from haystack.components.agents import Agent
@@ -132,7 +135,10 @@ def __init__(
132135
if self.tools:
133136
self._tool_invoker = ToolInvoker(tools=self.tools, raise_on_failure=self.raise_on_tool_invocation_failure)
134137
else:
135-
logger.warning("No tools provided to the Agent. It will only return text responses.")
138+
logger.warning(
139+
"No tools provided to the Agent. The Agent will behave like a ChatGenerator and only return text "
140+
"responses. To enable tool usage, pass tools directly to the Agent, not to the chat_generator."
141+
)
136142
self._tool_invoker = None
137143
self._is_warmed_up = False
138144

@@ -304,8 +310,8 @@ async def run_async(
304310

305311
state.set("messages", llm_messages)
306312

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

311317
# 3. Call the ToolInvoker

test/components/agents/test_agent.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,17 @@ def test_exit_conditions_checked_across_all_llm_messages(self, monkeypatch, weat
491491
== "{'weather': 'mostly sunny', 'temperature': 7, 'unit': 'celsius'}"
492492
)
493493

494-
def test_agent_with_no_tools(self, monkeypatch):
494+
def test_agent_with_no_tools(self, monkeypatch, caplog):
495495
monkeypatch.setenv("OPENAI_API_KEY", "fake-key")
496496
generator = OpenAIChatGenerator()
497497

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

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

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

0 commit comments

Comments
 (0)