mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-02 05:38:12 +00:00
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.
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user