Skip to content
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

bug fix for max_consecutive_auto_reply #1374

Merged
merged 5 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,9 @@ def my_message(
if isinstance(max_turns, int):
self._prepare_chat(recipient, clear_history, reply_at_receive=False)
for i in range(max_turns):
# check recipient max consecutive auto reply limit
if self._consecutive_auto_reply_counter[recipient] >= recipient._max_consecutive_auto_reply:
break
if i == 0:
if isinstance(message, Callable):
msg2send = message(_chat_info["sender"], _chat_info["recipient"], kwargs)
Expand Down
26 changes: 26 additions & 0 deletions test/agentchat/test_conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,31 @@ def test_max_consecutive_auto_reply():
assert agent1.reply_at_receive[agent] is False and agent.reply_at_receive[agent1] is True


def test_max_consecutive_auto_reply_with_max_turns(capsys: pytest.CaptureFixture[str]):
agent1 = ConversableAgent("agent1", max_consecutive_auto_reply=1, llm_config=False, human_input_mode="NEVER")
agent2 = ConversableAgent("agent2", max_consecutive_auto_reply=100, llm_config=False, human_input_mode="NEVER")

# max_consecutive_auto_reply parameter on the agent that initiates chat
agent1.initiate_chat(agent2, message="hello", max_turns=50)
assert len(agent2.chat_messages[agent1]) == 4
assert len(agent1.chat_messages[agent2]) == 4
# checking captured output
captured = capsys.readouterr()
assert "TERMINATING RUN" in captured.out
assert "Maximum number of consecutive auto-replies reached" in captured.out

_ = capsys.readouterr() # Explicitly clear buffer

# max_consecutive_auto_reply parameter on the recipient agent
agent2.initiate_chat(agent1, message="hello", max_turns=50)
assert len(agent1.chat_messages[agent2]) == 3
assert len(agent2.chat_messages[agent1]) == 3
# checking captured output
captured = capsys.readouterr()
assert "TERMINATING RUN" in captured.out
assert "Maximum number of consecutive auto-replies reached" in captured.out


def test_conversable_agent():
dummy_agent_1 = ConversableAgent(name="dummy_agent_1", llm_config=False, human_input_mode="ALWAYS")
dummy_agent_2 = ConversableAgent(name="dummy_agent_2", llm_config=False, human_input_mode="TERMINATE")
Expand Down Expand Up @@ -1916,4 +1941,5 @@ def test_validate_llm_config(
# test_process_gemini_carryover()
# test_process_carryover()
# test_context_variables()
# test_max_consecutive_auto_reply_with_max_turns()
test_invalid_functions_parameter()
Loading