From b6b97e479ec9c7a4df5667c3a4bef1f5888d040c Mon Sep 17 00:00:00 2001 From: Greyson LaLonde Date: Fri, 3 Apr 2026 05:46:11 +0800 Subject: [PATCH] fix: convert AgentAction to AgentFinish in finalize instead of skipping When the flow-based AgentExecutor reaches finalize() with an AgentAction still in current_answer (e.g. after max iterations or all todos complete), the previous code returned "skipped" which left invoke() with a non-AgentFinish answer, raising RuntimeError('Agent execution ended without reaching a final answer.'). Now we convert the AgentAction to AgentFinish using its result or text, preventing the crash. --- .../src/crewai/experimental/agent_executor.py | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/crewai/src/crewai/experimental/agent_executor.py b/lib/crewai/src/crewai/experimental/agent_executor.py index bbd14f518..be555ffe7 100644 --- a/lib/crewai/src/crewai/experimental/agent_executor.py +++ b/lib/crewai/src/crewai/experimental/agent_executor.py @@ -2114,15 +2114,28 @@ class AgentExecutor(Flow[AgentExecutorState], CrewAgentExecutorMixin): text=fallback_text, ) - if not isinstance(self.state.current_answer, AgentFinish): - skip_text = Text() - skip_text.append("⚠️ ", style="yellow bold") - skip_text.append( - f"Finalize called with {type(self.state.current_answer).__name__} instead of AgentFinish - skipping", - style="yellow", + if isinstance(self.state.current_answer, AgentAction): + action = self.state.current_answer + fallback_output = str( + action.result + or action.text + or "Agent completed execution but produced no final output." + ) + + if self.agent.verbose: + warn_text = Text() + warn_text.append("⚠️ ", style="yellow bold") + warn_text.append( + "Finalize: converting AgentAction to AgentFinish", + style="yellow", + ) + self._console.print(warn_text) + + self.state.current_answer = AgentFinish( + thought="Converted from AgentAction during finalization", + output=fallback_output, + text=fallback_output, ) - self._console.print(skip_text) - return "skipped" self.state.is_finished = True self._show_logs(self.state.current_answer)