From 6520710b5a10148a588e8b38056455aa0c6c8ece Mon Sep 17 00:00:00 2001 From: Feng Wu Date: Sun, 21 Jun 2026 13:36:05 +0800 Subject: [PATCH] fix(task): reorder agent assignment before null check Prevent self.agent from being set to None before the guard check, which would break subsequent retries or replay. Also fix falsy check on json_output to use 'is not None' so that empty dict {} is preserved as valid output. --- lib/crewai/src/crewai/task.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/crewai/src/crewai/task.py b/lib/crewai/src/crewai/task.py index c63cfe8666..6fb5836200 100644 --- a/lib/crewai/src/crewai/task.py +++ b/lib/crewai/src/crewai/task.py @@ -645,11 +645,11 @@ async def _aexecute_core( self._store_input_files() try: agent = agent or self.agent - self.agent = agent if not agent: raise Exception( f"The task '{self.description}' has no agent assigned, therefore it can't be executed directly and should be executed in a Crew using a specific process that support that, like hierarchical." ) + self.agent = agent self.prompt_context = context tools = tools or self.tools or [] @@ -740,7 +740,7 @@ async def _aexecute_core( if self.output_file: content = ( json_output - if json_output + if json_output is not None else ( pydantic_output.model_dump_json() if pydantic_output else result ) @@ -770,11 +770,11 @@ def _execute_core( self._store_input_files() try: agent = agent or self.agent - self.agent = agent if not agent: raise Exception( f"The task '{self.description}' has no agent assigned, therefore it can't be executed directly and should be executed in a Crew using a specific process that support that, like hierarchical." ) + self.agent = agent self.prompt_context = context tools = tools or self.tools or [] @@ -865,7 +865,7 @@ def _execute_core( if self.output_file: content = ( json_output - if json_output + if json_output is not None else ( pydantic_output.model_dump_json() if pydantic_output else result )