Schema Validation errors while using delegation

I have a standard setup from the docs using the manager, plus the few simple agents, but it fails with pedantic error. My code:

from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task

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

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

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

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

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

	@agent
	def writer(self) -> Agent:
		return Agent(
			config=self.agents_config['writer'],
			verbose=True,
			allow_delegation=False,
		)

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

	def manager(self) -> Agent:
		return Agent(
		role="Project Manager",
		goal="Efficiently manage the crew and ensure high-quality task completion",
		backstory="You're an experienced project manager, skilled in overseeing complex projects and guiding teams to success. Your role is to coordinate the efforts of the crew members, ensuring that each task is completed on time and to the highest standard.",
		allow_delegation=True,
		verbose=True
		)

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

Logs:

# Agent: Project Manager
## Thought: Thought: I need to delegate the task of ensuring that the user request is fully completed and verified according to the specified criteria to the Senior Data Researcher. I will carefully construct the task description and context to include all necessary information for them to execute the task effectively.
## Using tool: Delegate work to coworker
## Tool Input:
"{\"task\": {\"description\": \"Complete the user request by validating the output using the specified criteria and verification agents.\", \"type\": \"str\"}, \"context\": {\"description\": \"This user request requires thorough completion and verification. Verification criteria include: 1. Accuracy - ensuring data correctness; 2. Completeness - verifying all elements of the request are addressed; 3. Relevance - checking pertinence to the user request; 4. Consistency - maintaining uniformity in language and formatting; 5. Clarity and Readability - ensuring the output is easily understandable. Use verification agents such as peer reviews, software tools for grammar and clarity checks, source validation for claims, a checklist for systematic evaluation, and a feedback loop for future improvements. Document all findings and results in markdown format.\", \"type\": \"str\"}, \"coworker\": \"Senior Data Researcher\"}"
## Tool Output:

I encountered an error while trying to use the tool. This was the error: 2 validation errors for DelegateWorkToolSchema
task
  Input should be a valid string [type=string_type, input_value={'description': 'Complete...agents.', 'type': 'str'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/string_type
context
  Input should be a valid string [type=string_type, input_value={'description': 'This use...format.', 'type': 'str'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/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'}}
1 Like

I am facing this issue as well. I notice that its due to the generation of {‘description’, ‘type’} when the ‘task’ argument is only accepting a str.

I tried to change my llm from 4o-mini to 4o and its working. I would assume that the prompt for generating the task didnt quite work for 4o-mini.

2 Likes

Using 4o instead of 4o-mini worked for me as well!

3 Likes

Same error here when running with default LLM and with Ollama (llama3.2)

@lucasrosa90 There can be two reasons:

  • Such an error can happen because of the LLM you’re using (source). Unfortunately, smaller LLMs sometimes struggle to work with CrewAI. Try to switch the LLM to a more capable one.
  • Such an error can happen because the task configuration is too long or strong (source), which changes some internal CrewAI keywords like action, thought, etc. Try to rephrase the task configuration.
1 Like

First of all, thanks @Bhavik_Shah, because of your tip I could successfully run it here and see what was the problem.

I noticed these differences between gpt-4o and other LLMs regarding the “Tool Input”

In my tool example:

class ChatHistoryToolInput(BaseModel):
    """Input schema for ChatHistoryTool."""
    user: str = Field(..., description="User ID to fetch chat history for.")

GPT-4o

## Tool Input:
"{\"user\": \"USER_a1s2d3f4\"}"

Other LLMs

# Tool Input:
"{\"user\": {\"description\": \"User ID to fetch chat history for.\", \"type\": \"str\"}}"

The same problem was happening for agents with allow_delegation=True and with process=Process.hierarchical because it uses tools to delegate (DelegateWorkTool and AskQuestionTool).

With that said, instead of using manager_llm I just created mine here and looks to be working as expected:

	def manager_agent(self) -> Agent:
		return Agent(
			role="Crew Manager",
			goal="""
			Manage the team to complete the task in the best way possible.
			""",
			backstory="""
			You are a seasoned manager with a knack for getting the best out of your team.\nYou are also known for your ability to delegate work to the right people, and to ask the right questions to get the best out of your team.\nEven though you don't perform tasks by yourself, you have a lot of experience in the field, which allows you to properly evaluate the work of your team members.

			Additional rules for Tools:
			-----------------
			1. Regarding the Action Input (the input to the action, just a simple python dictionary, enclosed
			in curly braces, using \" to wrap keys and values.)
			
			For example for the following schema:
			```
			class ExampleToolInput(BaseModel):
				task: str = Field(..., description="The task to delegate")
				context: str = Field(..., description="The context for the task")
				coworker: str = Field(..., description="The role/name of the coworker to delegate to")
			```
			Then the input should be a JSON object with the user ID:
			- task: The task to delegate
			- context: The context for the task
			- coworker: The role/name of the coworker to delegate to
			""",
		)

	@crew
	def crew(self) -> Crew:
		"""Creates the CustomerService crew"""
		return Crew(
			agents=self.agents,
			tasks=self.tasks,
			process=Process.hierarchical,
			verbose=True,
			manager_agent=self.manager_agent(),
		)

I tested using:

  • MODEL=ollama/openhermes
  • MODEL=ollama/llama3.2
  • MODEL=gpt-4o-mini

cc @buger @knivore


@rokbenko Giving the right direction, I got the expected result even with smaller LLMs.

1 Like

Changing to 40 instead of 4o-mini fixed it for me as well. I only needed to change it for the manager_llm. I left the agent llm as 4o-mini

Thanks for this @lucasrosa90. Managed to get smaller LLMs working with this tecnique.