crewAIInc / crewAIInc/crewAI

Delegating to a coworker fails when the model emits a Python-repr list: coworker="['researcher']"

Open Beginner friendly
#7,614 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
58.8k
Forks
8.5k
Avg merge
1d 15h
Merged PRs (30d)
109

Description

Description

The delegation tools tolerate a coworker that a weak model emitted as a list instead of a string — _get_coworker() unwraps [...] and takes the first entry. But the unwrap keeps the entry's quotes, and role matching only strips double quotes, so the Python-repr spelling (['researcher'], what str(list) and most small models produce) never matches, while the JSON spelling (["researcher"]) does.

The failure is self-contradicting: the tool refuses the role and then prints that same role as an available option.

coworker="['Senior Researcher']"
-> Error executing tool. coworker mentioned not found, it must be one of the following options:
   - senior researcher
   - analyst

Steps to Reproduce

from unittest.mock import patch
from crewai.agent import Agent
from crewai.tools.agent_tools.agent_tools import AgentTools

senior = Agent(role="Senior Researcher", goal="g", backstory="b", allow_delegation=False)
analyst = Agent(role="Analyst", goal="g", backstory="b", allow_delegation=False)
delegate = AgentTools(agents=[senior, analyst]).tools()[0]

with patch.object(Agent, "execute_task", lambda self, task, context=None, tools=None: self.role):
    for coworker in ["Senior Researcher", '["Senior Researcher"]', "['Senior Researcher']"]:
        print(repr(coworker), "->", delegate.run(coworker=coworker, task="t", context="c")[:60])

Actual behavior (measured on main @ 3831e8b)

coworker as the model emits it value used for role matching outcome
Senior Researcher 'senior researcher' delegates
[Senior Researcher] 'senior researcher' delegates
["Senior Researcher"] 'senior researcher' delegates
['Senior Researcher'] "'senior researcher'" "coworker mentioned not found"
['Senior Researcher', 'Analyst'] "'senior researcher'" "coworker mentioned not found"
'Senior Researcher' (quoted scalar) "'senior researcher'" "coworker mentioned not found"

Expected behavior

All six spellings resolve to the Senior Researcher agent. A quoted name is the same defect as a padded name, and a padded name is already tolerated.

Root cause

_get_coworker() (tools/agent_tools/base_agent_tools.py:37) hands the unwrapped entry straight to matching, and sanitize_agent_name() (:20) normalizes whitespace, case, and " only:

normalized = " ".join(name.split())
return normalized.replace('"', "").casefold()

Its own docstring says the value comes back with "quotes removed", and the comment at :65 explains why the tolerance exists at all ("less-powerful LLM's have difficulty producing valid JSON"). Single quotes are the other half of that JSON-shaped problem and are the natural output of str(list) / a Python-repr argument, so the branch misses its main case.

AskQuestionTool shares _get_coworker(), so asking a coworker a question fails the same way.

Possible Solution

Strip the wrapping quotes where the malformed value is already being normalized, in _get_coworker(), so both the list and the scalar form are handled in one place and the agents' own role names are never touched:

if coworker:
    is_list = coworker.startswith("[") and coworker.endswith("]")
    if is_list:
        coworker = coworker[1:-1].split(",")[0]
    coworker = coworker.strip().strip("'\"")

I would not widen sanitize_agent_name() to drop ' instead: it is applied to the agents' real role names as well, so an apostrophe in a role (Data Steward's Office) would stop matching. _get_coworker() only ever sees a model-produced argument.

Additional context

Nothing under lib/crewai/tests/ asserts the current single-quote behavior; the two existing list tests use the unquoted coworker="[researcher]" form, which is why the gap is invisible.

Authored with an AI coding assistant. .github/CONTRIBUTING.md requires the llm-generated label for agent-authored contributions and external contributors cannot apply labels here — maintainers, please add it.

Operating System

Windows 11 (nothing OS specific)

Python Version

3.13

crewAI Version

1.15.22 (main @ 3831e8b)

crewAI Tools Version

Not involved — the defect is in the crewai package.

Virtual Environment

Venv

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in tools/agent_tools/base_agent_tools.py, especially _get_coworker() and the shared AskQuestionTool path, then review the existing delegation list tests under lib/crewai/tests. Add regression coverage for Python-repr list and quoted scalar coworker values, and confirm all six spellings resolve to the Senior Researcher agent without breaking role names containing apostrophes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
86/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.