Skip to content

_mentioned_agents() in the SelectorGroupChatManager doesn't work with the <thinking> tag #6891

@mohamedaliELfeky

Description

@mohamedaliELfeky

What happened?

Describe the bug
When the model response includes a <thinking> section or similar inner monologue, _mentioned_agents() mistakenly counts agent mentions inside that section toward deciding the next speaker. This skews the selection logic.


🧠 Why It's a Problem

  • Thinking segments may include analysis like:

    <thinking>Maybe AgentA could do X, or AgentB could answer Y</thinking>
    I suggest AgentC takes the next step.
    
  • In this case, AgentA and AgentB get mentioned more (inside <thinking>), while the actual intended next speaker (AgentC) is mentioned once — in the final suggestion.

  • Since _mentioned_agents() tallies all mentions blindly, it may incorrectly pick AgentA or AgentB.


⚠️ Actual Behavior

  • _mentioned_agents() internally likely does:

    mention_counts = Counter(agent.name in message.content for agent in self._participants)
  • This doesn’t distinguish between <thinking> and actual answer content.

  • It selects the most frequently mentioned agent, even if that mention was not a decision but part of reasoning.


✅ Expected Behavior

  • Only consider mentions outside <thinking> or other model "reflection" blocks.
  • Prioritize mentions near the end of the message (especially after reasoning).
  • Ideally, allow devs to override or pass in custom selection logic.

🛠️ Fix / Workaround (Patch _mentioned_agents)

You can monkey-patch or subclass it (once it's made extensible) like this:

import re
from collections import Counter

class CustomSelectorGroupChatManager(SelectorGroupChatManager):
    def _mentioned_agents(self, message):
        # Remove model reasoning sections
        cleaned = re.sub(r"<thinking>.*?</thinking>", "", message.content, flags=re.DOTALL | re.IGNORECASE)

        mention_counts = Counter()
        for agent in self._participants:
            # Optional: use regex with boundaries to avoid substring matches
            mentions = re.findall(rf"\b{re.escape(agent.name)}\b", cleaned)
            mention_counts[agent.name] += len(mentions)

        if not mention_counts:
            return []

        # Return the top mentioned agent(s)
        top_count = max(mention_counts.values())
        return [name for name, count in mention_counts.items() if count == top_count]

📬 Recommended Change to Upstream Codebase

In SelectorGroupChatManager, fix _mentioned_agents() or introduce:

def _preprocess_selector_message(self, message: AssistantMessage) -> str:
    """Strip reasoning sections like <thinking> from selector output."""
    return re.sub(r"<thinking>.*?</thinking>", "", message.content, flags=re.DOTALL)

Then use that inside _mentioned_agents() before counting mentions.

Which packages was the bug in?

Python AgentChat (autogen-agentchat>=0.4.0)

AutoGen library version.

Python dev (main branch)

Other library version.

No response

Model used

Qwen3

Model provider

Other (please specify below)

Other model provider

Groq

Python version

3.12

.NET version

None

Operating system

Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions