Skip to content

Fix incomplete final answers in hierarchical process mode #2769

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

Closed
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
8 changes: 7 additions & 1 deletion src/crewai/tools/agent_tools/delegate_work_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ class DelegateWorkToolSchema(BaseModel):


class DelegateWorkTool(BaseAgentTool):
"""Tool for delegating work to coworkers"""
"""Tool for delegating work to other agents in the crew.

Attributes:
result_as_answer (bool): When True, returns the delegated agent's result
as the final answer instead of metadata about delegation.
"""

name: str = "Delegate work to coworker"
args_schema: type[BaseModel] = DelegateWorkToolSchema
result_as_answer: bool = True

def _run(
self,
Expand Down
55 changes: 55 additions & 0 deletions tests/test_hierarchical_issue_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Test to ensure hierarchical process mode returns complete final answers."""

import pytest

from crewai.agent import Agent
from crewai.crew import Crew
from crewai.process import Process
from crewai.task import Task


@pytest.mark.vcr(filter_headers=["authorization"])
def test_hierarchical_process_delegation_result():
"""Tests hierarchical process delegation result handling.

Ensures that:
1. The output is derived from the delegated agent's actual work.
2. The response does not contain delegation-related metadata.
3. The content meets minimum length requirements.
4. Expected topic-related keywords are present in the output.
"""
researcher = Agent(
role="Researcher",
goal="Make the best research and analysis on content about AI and AI agents",
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
allow_delegation=False,
)

writer = Agent(
role="Senior Writer",
goal="Write the best content about AI and AI agents.",
backstory="You're a senior writer, specialized in technology, software engineering, AI and startups. You work as a freelancer and are now working on writing content for a new customer.",
allow_delegation=False,
)

task = Task(
description="Come up with a list of 3 interesting ideas to explore for an article, then write one amazing paragraph highlight for each idea that showcases how good an article about this topic could be. Return the list of ideas with their paragraph and your notes.",
expected_output="3 bullet points with a paragraph for each idea.",
)

crew = Crew(
agents=[researcher, writer],
process=Process.hierarchical,
manager_llm="gpt-4o",
tasks=[task],
)

result = crew.kickoff()

assert "idea" in result.raw.lower() or "article" in result.raw.lower()
assert len(result.raw) > 100 # Ensure we have substantial content
assert result.raw.count('\n') >= 6 # At least 3 ideas with paragraphs

assert "delegate" not in result.raw.lower()
assert "delegating" not in result.raw.lower()
assert "assigned" not in result.raw.lower()
Loading