Crew delegation is not working as expected

I created a crew to review the provided code and to give me the modified or improved version. I have a set of agents where I used delegation to refer back to the previous agents, but I’m encountering an error during the tool call. Below is the code:

import os
from crewai import Agent, LLM, Crew, Task
from dotenv import load_dotenv
os.environ['OPENAI_MODEL_NAME'] = 'gpt-4o-mini'
junior_developer_agent = Agent(
    name="Junior Developer Agent",
    role="Junior Software Developer",
    goal="Review the provided code for adherence to PEP8 guidelines and make necessary modifications to improve readability, maintainability, and performance. Identify potential bugs and logical errors.",
    backstory="As a junior software developer, you are eager to learn and grow. You have a solid understanding of Python and PEP8 guidelines, enabling you to review code effectively. Your goal is to enhance code quality while developing your skills in software development.",
    allow_delegation=False,
    memory=True,
    verbose=True,
)

senior_developer_agent = Agent(
    name="Senior Developer Agent",
    role="Senior Software Developer",
    goal="Review the modifications made by the junior developer. Make further enhancements if necessary or delegate back to the junior developer for additional changes.",
    backstory="With extensive experience in software development, you are a senior developer known for your ability to optimize code and mentor junior developers. You ensure that all modifications align with best practices and improve overall functionality.",
    allow_delegation=True,
    allow_code_execution=True,
    memory=True,
    verbose=True
)

lead_engineer_agent = Agent(
    name="Lead Engineer Agent",
    role="Lead Software Engineer",
    goal="Conduct a final review of the modified code from the senior developer. Ensure all feedback has been addressed and decide whether to approve the changes or delegate further modifications.",
    backstory="As a lead engineer, you possess a wealth of experience in software architecture and design. You are responsible for overseeing code quality and ensuring that all team members adhere to high standards of excellence.",
    allow_delegation=True,
    allow_code_execution=True,
    memory=True,
    verbose=True
)

rating_agent = Agent(
    name="Code Rating Agent",
    role="Code Rating Provider",
    goal="Evaluate the quality of the modified code based on efficiency, readability, and adherence to best practices. Provide a detailed rating and feedback.",
    backstory="An experienced examiner who rates coding proficiency by analyzing quality and structure. You assess adherence to best practices and provide constructive feedback on improvements.",
    allow_delegation=False,
    verbose=True,
)

comparison_agent = Agent(
    name="Comparison Agent",
    role="Code Comparer",
    goal="Compare the old and new code snippets, evaluating improvements in efficiency, readability, and adherence to best practices. Rate both versions on a scale of 1 to 10 with detailed assessments.",
    backstory="A skilled comparator specializing in evaluating modifications in code. You analyze improvements in structure and efficiency, providing comprehensive ratings on how well the new code surpasses its predecessor.",
    allow_delegation=False,
    verbose=True,
)

code_reviewing_task = Task(
    description="Review the provided code for potential bugs and adherence to PEP8 guidelines. Code:\n{python_file}",
    expected_output="A comprehensive list of code improvements, including identified bugs, logical errors, and suggestions for enhancing readability and maintainability.",
    agent=junior_developer_agent,
)

coding_task = Task(
    description="Implement the improvements identified in the review. Modify the given code accordingly. Code:\n{python_file} \n Ensure that all improvements have been applied correctly and check for any additional enhancements.",
    expected_output="Output only the improved code without any additional commentary.",
    dependencies=[code_reviewing_task],
    agent=senior_developer_agent,
)

feedback_task = Task(
    description="Verify whether all feedback provided has been addressed in the modified code. Code:\n{python_file}, Feedback:\n{code_feedback}",
    expected_output="Respond with 'Yes' if all feedback is resolved, otherwise respond with 'No'.",
    agent=lead_engineer_agent,
    context = [code_reviewing_task,coding_task]
)

rating_task = Task(
    description="Evaluate the skills of the coder based on the quality of the modified code. Code:\n{python_file}",
    expected_output="Provide a rating between 1 to 10, along with a brief justification for the rating.",
    agent=rating_agent,
)

code_comparison_task = Task(
    description="Compare the old and new code snippets, evaluating improvements in efficiency, readability, and adherence to best practices. Old Code:\n{python_file1}, New Code:\n{python_file2}",
    expected_output="Provide separate ratings between 1 to 10 for both old and new code, including detailed reasons for each rating. Do not include the actual code snippets in your output.",
    agent=comparison_agent,
)

crew1 = Crew(
    tasks=[
        code_reviewing_task,  # Junior Developer reviews the code
        coding_task           # Senior Developer implements improvements
    ],
    agents=[
        junior_developer_agent,  # Assigned to review the code
        senior_developer_agent    # Assigned to implement modifications
    ]
)


crew2 = Crew(
    tasks=[
        feedback_task,          # Lead Engineer verifies feedback resolution
        rating_task,            # Rating Agent evaluates coder's skills
        code_comparison_task     # Comparison Agent compares old and new code
    ],
    agents=[
        lead_engineer_agent,    # Assigned to verify feedback
        rating_agent,            # Assigned to rate the coder's skills
        comparison_agent         # Assigned to compare old and new code
    ]
)
def run_crews(py_file):
    # Read the original code from the specified Python file
    with open(py_file, 'r') as f:
        code = f.read()
    
    # Format the descriptions of the tasks with the original code
    code_reviewing_task.description = code_reviewing_task.description.format(python_file=code)
    coding_task.description = coding_task.description.format(python_file=code)

    # Kick off the first crew for code reviewing and modification
    crew1.kickoff()

    # Capture outputs from the first crew
    code_feedback = code_reviewing_task.output.raw  # Feedback from the junior developer
    final_code = coding_task.output.raw            # Modified code from the senior developer

    # Format the descriptions of the second crew's tasks with the modified code
    rating_task.description = rating_task.description.format(python_file=final_code)
    code_comparison_task.description = code_comparison_task.description.format(python_file1=code, python_file2=final_code)
    feedback_task.description = feedback_task.description.format(python_file=final_code, code_feedback=code_feedback)

    # Kick off the second crew for feedback verification, rating, and comparison
    crew2.kickoff()

    # Capture outputs from the second crew
    feedback_op = feedback_task.output.raw           # Feedback verification result
    rating_op = rating_task.output.raw                # Rating of the modified code
    code_compare_rating = code_comparison_task.output.raw  # Comparison ratings

    return {
        "original_code": code,
        "final_code": final_code,
        "feedback": feedback_op,
        "rating": rating_op,
        "comparison_rating": code_compare_rating
    }
py_file = "./app_resnet.py"
results = run_crews(py_file)

The crew is executing but at the same time i see the below response as well. what could be the issue? can anyone please review the code?

Error Response:

    I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 2 validation errors for DelegateWorkToolSchema
    task
      Input should be a valid string [type=string_type, input_value={'description': 'Implemen...bility.', 'type': 'str'}, input_type=dict]
        For further information visit https://errors.pydantic.dev/2.10/v/string_type
    context
      Input should be a valid string [type=string_type, input_value={'description': 'The exis...ormats.', 'type': 'str'}, input_type=dict]
        For further information visit https://errors.pydantic.dev/2.10/v/string_type.
     Tool Delegate work to coworker accepts these inputs: Tool Name: Delegate work to coworker
    Tool Arguments: {'task': {'description': 'The task to delegate', 'type': 'str'}, 'context': {'description': 'The context for the task', 'type': 'str'}, 'coworker': {'description': 'The role/name of the coworker to delegate to', 'type': 'str'}}
    Tool Description: Delegate a specific task to one of the following coworkers: Junior Software Developer
    The input to this tool should be the coworker, the task you want them to do, and ALL necessary context to execute the task, they know nothing about the task, so share absolute everything you know, don't reference things but instead explain them.

I encountered this same issue- it is a problem with gpt-4o-mini. Unfortunately, the solution is to switch to gpt-4o which increases cost. Otherwise it is possible to add custom code to try to force gpt-4o-mini to work better at matching the required delegation schema; but even though some reports are that this works, I have still observed issues - especially working with a manager agent. Try a run with gpt-4o and see if that helps. Let me know. Good luck.