Fix router path detection for listener methods with @human_feedback #4173
+96
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fix incorrect router path detection when combining
@listenand@human_feedback(emit=...)decorators.Problem
When a listener method is decorated with both
@listenand@human_feedbackwithemit, the Flow metaclass incorrectly detects router paths by parsing the method's source code instead of using the__router_paths__attribute set by@human_feedback.Example:
Before fix:
_router_paths["review"]=["content to review"](wrong - parsed from return statement)After fix:
_router_paths["review"]=["approved", "rejected"](correct - from emit parameter)This bug could also cause
IndentationErrorwhen parsing source code of multi-decorated methods, becauseinspect.getsource()includes decorators with class-level indentation which can confuseast.parse().Root Cause
In
FlowMeta.__new__, the code for listener methods that are also routers directly callsget_possible_return_constants()without first checking the__router_paths__attribute:However, the code for start methods that are routers correctly checks
__router_paths__first:Solution
Apply the same pattern to listener methods: check
__router_paths__first before falling back to source parsing.Files Changed
lib/crewai/src/crewai/flow/flow.py- Fix router path detection for listenerslib/crewai/tests/test_human_feedback_decorator.py- Add regression testsTests
Added
TestListenerRouterPathDetectionclass with 2 tests:test_listener_with_human_feedback_emit_has_router_paths- Basic casetest_multiple_listener_routers_detected- Multiple listener-routers in same flowTest results:
Related Documentation
This is a documented use case per
docs/en/learn/human-feedback-in-flows.mdx:Note
Ensures correct router path detection for listener methods decorated with
@human_feedback(emit=...), avoiding brittle source parsing and potentialIndentationError.flow.py(FlowMeta.__new__), when a listener is also a router, first use__router_paths__from the decorator; fall back toget_possible_return_constantsonly if absentTestListenerRouterPathDetectionintest_human_feedback_decorator.pywith cases for single and multiple listener-routersWritten by Cursor Bugbot for commit 59118dc. This will update automatically on new commits. Configure here.