Skip to content

Commit 46846bc

Browse files
authored
fix: improve error handling for HumanFeedbackPending in flow execution (#4203)
* fix: handle HumanFeedbackPending in flow error management Updated the flow error handling to treat HumanFeedbackPending as expected control flow rather than an error. This change ensures that the flow can appropriately manage human feedback scenarios without signaling an error, improving the robustness of the flow execution. * fix: improve error handling for HumanFeedbackPending in flow execution Refined the flow error management to emit a paused event for HumanFeedbackPending exceptions instead of treating them as failures. This enhancement allows the flow to better manage human feedback scenarios, ensuring that the execution state is preserved and appropriately handled without signaling an error. Regular failure events are still emitted for other exceptions, maintaining robust error reporting.
1 parent d71e91e commit 46846bc

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

lib/crewai/src/crewai/flow/flow.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,29 +1602,45 @@ async def _execute_method(
16021602

16031603
return result
16041604
except Exception as e:
1605-
if not self.suppress_flow_events:
1606-
# Check if this is a HumanFeedbackPending exception (paused, not failed)
1607-
from crewai.flow.async_feedback.types import HumanFeedbackPending
1605+
# Check if this is a HumanFeedbackPending exception (paused, not failed)
1606+
from crewai.flow.async_feedback.types import HumanFeedbackPending
16081607

1609-
if isinstance(e, HumanFeedbackPending):
1610-
# Auto-save pending feedback (create default persistence if needed)
1611-
if self._persistence is None:
1612-
from crewai.flow.persistence import SQLiteFlowPersistence
1608+
if isinstance(e, HumanFeedbackPending):
1609+
# Auto-save pending feedback (create default persistence if needed)
1610+
if self._persistence is None:
1611+
from crewai.flow.persistence import SQLiteFlowPersistence
16131612

1614-
self._persistence = SQLiteFlowPersistence()
1613+
self._persistence = SQLiteFlowPersistence()
16151614

1616-
# Regular failure
1617-
future = crewai_event_bus.emit(
1618-
self,
1619-
MethodExecutionFailedEvent(
1620-
type="method_execution_failed",
1621-
method_name=method_name,
1622-
flow_name=self.name or self.__class__.__name__,
1623-
error=e,
1624-
),
1625-
)
1626-
if future:
1627-
self._event_futures.append(future)
1615+
# Emit paused event (not failed)
1616+
if not self.suppress_flow_events:
1617+
future = crewai_event_bus.emit(
1618+
self,
1619+
MethodExecutionPausedEvent(
1620+
type="method_execution_paused",
1621+
method_name=method_name,
1622+
flow_name=self.name or self.__class__.__name__,
1623+
state=self._copy_and_serialize_state(),
1624+
flow_id=e.context.flow_id,
1625+
message=e.context.message,
1626+
emit=e.context.emit,
1627+
),
1628+
)
1629+
if future:
1630+
self._event_futures.append(future)
1631+
elif not self.suppress_flow_events:
1632+
# Regular failure - emit failed event
1633+
future = crewai_event_bus.emit(
1634+
self,
1635+
MethodExecutionFailedEvent(
1636+
type="method_execution_failed",
1637+
method_name=method_name,
1638+
flow_name=self.name or self.__class__.__name__,
1639+
error=e,
1640+
),
1641+
)
1642+
if future:
1643+
self._event_futures.append(future)
16281644
raise e
16291645

16301646
def _copy_and_serialize_state(self) -> dict[str, Any]:

0 commit comments

Comments
 (0)