Compare commits

...

1 Commits

Author SHA1 Message Date
Greyson LaLonde
b6b97e479e 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.
2026-04-03 05:46:11 +08:00

View File

@@ -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)