Diablo3093
Diablo3093

Reputation: 1063

How to resolve the crewai error: Input should be a valid dictionary or instance of BaseAgent?

I am using crewai to set up 3 agents with one as manager agent and 2 worker agents. This works perfectly fine when I use a sequential process but when I switch to hierarchical processing, I see the following error

manager_agent
  Input should be a valid dictionary or instance of BaseAgent [type=model_type, input_value=<bound method memoize.<lo... object at 0x10bd9e390>>, input_type=method]
    For further information visit https://errors.pydantic.dev/2.8/v/model_type

Here is the code

from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from dotenv import load_dotenv
from langchain_openai import AzureChatOpenAI
import os

# Uncomment the following line to use an example of a custom tool
# from sample.tools.custom_tool import MyCustomTool

# Check our tools documentations for more information on how to use them
# from crewai_tools import SerperDevTool

load_dotenv()

azure_llm = AzureChatOpenAI(
    azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"),
    api_key=os.environ.get("AZURE_OPENAI_KEY"),
    api_version=os.environ.get("AZURE_OPENAI_VERSION"),
)

@CrewBase
class SampleCrew():
    """Sample crew"""
    agents_config = 'config/agents.yaml'
    tasks_config = 'config/tasks.yaml'

    @agent
    def manager(self) -> Agent:
        return Agent(
            config=self.agents_config['manager'],
            # tools=[MyCustomTool()], # Example of custom tool, loaded on the beginning of file
            verbose=True,
            allow_delegation=True,
            llm=azure_llm
        )

    @agent
    def field_engineer(self) -> Agent:
        return Agent(
            config=self.agents_config['field_engineer'],
            # tools=[MyCustomTool()], # Example of custom tool, loaded on the beginning of file
            verbose=True,
            llm=azure_llm
        )

    @agent
    def data_scientist(self) -> Agent:
        return Agent(
            config=self.agents_config['data_scientist'],
            verbose=True,
            llm=azure_llm
        )
    
    @task
    def manager_task(self) -> Task:
        return Task(
            config=self.tasks_config['manager_task'],
            agent=self.manager()
        )

    @task
    def field_engineer_task(self) -> Task:
        return Task(
            config=self.tasks_config['field_engineer_task'],
            agent=self.field_engineer()
        )

    @task
    def data_scientist_task(self) -> Task:
        return Task(
            config=self.tasks_config['data_scientist_task'],
            agent=self.data_scientist(),
            output_file='report.md'
        )

    @crew
    def crew(self) -> Crew:
        """Creates the Sample crew"""
        return Crew(
            agents=self.agents, # Automatically created by the @agent decorator
            tasks=self.tasks, # Automatically created by the @task decorator
            # process=Process.sequential,
            verbose=2,
            manager_agent=self.manager,
            memory=True,
            # manager_llm=azure_llm,
            process=Process.hierarchical, # In case you wanna use that instead https://docs.crewai.com/how-to/Hierarchical/
        )

Upvotes: 2

Views: 1217

Answers (2)

Clay H
Clay H

Reputation: 680

Your manager should not by annotated with @agent. Instead you should reference the instance directly (e.g. via self.manager()) and not implicitly add it to the agent list via annotation. The example docs do not include the manager in the agent list either.

I ran into this myself and it took some time to track down the relevant error message.

Note also that the docs do not mention giving the manager a task - the manager is implicitly given the task of ensuring the enumerated tasks are completed correctly, but is not explicitly given a task itself via code.

Upvotes: 0

Harsha Gullapalli
Harsha Gullapalli

Reputation: 11

I hit a roadblock when importing .yaml files as well. Although I don't have an answer for the .yaml issue, directly initializing the agents in the main.py file worked well for me. I hope that helps.

Example:

# Initialize Agents
leader_agent = Agent(
    role='xxx',
    goal=(
        "xxx"
    ),
    backstory=(
        "xxx"
        "xxx"
        "xxx"
        "xxx"
    ),
    tools=[
        code_access_tool,
        documentation_generator_tool,
        directory_file_manager_tool
    ],
    verbose=True,  # Optional
)

Upvotes: 0

Related Questions