From ae2e9de893c85f7922eb4607dc5ab6770d355358 Mon Sep 17 00:00:00 2001 From: Lorenze Jay Date: Wed, 3 Jul 2024 12:27:21 -0700 Subject: [PATCH] added validator for async_execution true in tasks whenever in hierarchical run --- src/crewai/crew.py | 21 ++++++++++++++++----- tests/crew_test.py | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/crewai/crew.py b/src/crewai/crew.py index e7967ab99..441b859f0 100644 --- a/src/crewai/crew.py +++ b/src/crewai/crew.py @@ -226,11 +226,8 @@ class Crew(BaseModel): @model_validator(mode="after") def validate_tasks(self): - process = self.process - tasks = self.tasks - - if process == Process.sequential: - for task in tasks: + if self.process == Process.sequential: + for task in self.tasks: if task.agent is None: raise PydanticCustomError( "missing_agent_in_task", @@ -240,6 +237,20 @@ class Crew(BaseModel): return self + @model_validator(mode="after") + def check_tasks_in_hierarchical_process_not_async(self): + """Validates that the tasks in hierarchical process are not flagged with async_execution.""" + if self.process == Process.hierarchical: + for task in self.tasks: + if task.async_execution: + raise PydanticCustomError( + "async_execution_in_hierarchical_process", + "Hierarchical process error: Tasks cannot be flagged with async_execution.", + {}, + ) + + return self + def _setup_from_config(self): assert self.config is not None, "Config should not be None." diff --git a/tests/crew_test.py b/tests/crew_test.py index cbf07c649..af2c52b87 100644 --- a/tests/crew_test.py +++ b/tests/crew_test.py @@ -1109,6 +1109,33 @@ def test_hierarchical_crew_creation_tasks_with_agents(): ) +@pytest.mark.vcr(filter_headers=["authorization"]) +def test_hierarchical_crew_creation_tasks_without_async_execution(): + from langchain_openai import ChatOpenAI + + task = Task( + description="Come up with a list of 5 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="5 bullet points with a paragraph for each idea.", + async_execution=True, # should throw an error + ) + + with pytest.raises(pydantic_core._pydantic_core.ValidationError) as exec_info: + Crew( + tasks=[task], + agents=[researcher], + process=Process.hierarchical, + manager_llm=ChatOpenAI(model="gpt-4o"), + ) + + assert ( + exec_info.value.errors()[0]["type"] == "async_execution_in_hierarchical_process" + ) + assert ( + "Hierarchical process error: Tasks cannot be flagged with async_execution." + in exec_info.value.errors()[0]["msg"] + ) + + def test_crew_inputs_interpolate_both_agents_and_tasks(): agent = Agent( role="{topic} Researcher",