Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix(flow): let human_feedback emit steps expose the review in outputs (#7850)

Gabe Milani8078f9130c35
diff --git a/lib/crewai/src/crewai/flow/runtime/__init__.py b/lib/crewai/src/crewai/flow/runtime/__init__.py
index 75ecc3aca..0f5bf791b 100644
--- a/lib/crewai/src/crewai/flow/runtime/__init__.py
+++ b/lib/crewai/src/crewai/flow/runtime/__init__.py
@@ -1549,9 +1549,13 @@ class Flow(BaseModel, Generic[T], metaclass=FlowMeta):
# This allows methods to re-execute in loops (e.g., implement_changes → suggest_changes → implement_changes)
self._is_execution_resuming = False
- self._method_outputs.append(
- {"method": context.method_name, "output": resumed_method_output}
- )
+ method_output_entry: dict[str, Any] = {
+ "method": context.method_name,
+ "output": resumed_method_output,
+ }
+ if emit and isinstance(result, HumanFeedbackResult):
+ method_output_entry["human_feedback"] = result
+ self._method_outputs.append(method_output_entry)
try:
if emit and collapsed_outcome:
@@ -2981,12 +2985,16 @@ class Flow(BaseModel, Generic[T], metaclass=FlowMeta):
# For @human_feedback methods with emit, the result is the collapsed outcome
# (e.g., "approved") used for routing. But we want the actual method output
# to be the stored result (for final flow output). Replace the last entry
- # if a stashed output exists. Dict-based stash is concurrency-safe and
- # handles None return values (presence in dict = stashed, not value).
+ # if a stashed output exists, keeping the feedback alongside the output so
+ # expressions read `outputs.<method>` as the full feedback result.
+ # Dict-based stash is concurrency-safe.
if method_name in self._human_feedback_method_outputs:
- self._method_outputs[-1]["output"] = (
- self._human_feedback_method_outputs.pop(method_name)
- )
+ feedback_result = self._human_feedback_method_outputs.pop(method_name)
+ self._method_outputs[-1] = {
+ "method": str(method_name),
+ "output": feedback_result.output,
+ "human_feedback": feedback_result,
+ }
self._method_execution_counts[method_name] = (
self._method_execution_counts.get(method_name, 0) + 1
@@ -3660,10 +3668,10 @@ class Flow(BaseModel, Generic[T], metaclass=FlowMeta):
)
if emit:
- # Stash the real method output: the collapsed outcome routes
- # listeners, but the flow's final result stays the method's
- # actual return value.
- self._human_feedback_method_outputs[method_name] = method_output
+ # Stash the feedback result: the collapsed outcome routes listeners,
+ # but the flow's final result stays the method's actual return
+ # value (result.output).
+ self._human_feedback_method_outputs[method_name] = result
return result.outcome
return result
diff --git a/lib/crewai/src/crewai/flow/runtime/_outputs.py b/lib/crewai/src/crewai/flow/runtime/_outputs.py
index 321c5d456..2f111f167 100644
--- a/lib/crewai/src/crewai/flow/runtime/_outputs.py
+++ b/lib/crewai/src/crewai/flow/runtime/_outputs.py
@@ -3,7 +3,9 @@
from __future__ import annotations
from collections.abc import Mapping
-from typing import Any, TypedDict
+from typing import Any
+
+from typing_extensions import NotRequired, TypedDict
from crewai.utilities.serialization import to_serializable
@@ -11,6 +13,7 @@ from crewai.utilities.serialization import to_serializable
class _MethodOutput(TypedDict):
method: str
output: Any
+ human_feedback: NotRequired[Any]
def outputs_by_name(
@@ -21,7 +24,8 @@ def outputs_by_name(
) -> dict[str, Any]:
outputs: dict[str, Any] = {}
for entry in method_outputs:
- outputs[entry["method"]] = _output_value(entry["output"], serialize=serialize)
+ value = entry.get("human_feedback", entry["output"])
+ outputs[entry["method"]] = _output_value(value, serialize=serialize)
if local_outputs is not None:
outputs.update(
diff --git a/lib/crewai/src/crewai/flow/templates/flow_definition_skill.md.j2 b/lib/crewai/src/crewai/flow/templates/flow_definition_skill.md.j2
index 850364226..ed9c014ce 100644
--- a/lib/crewai/src/crewai/flow/templates/flow_definition_skill.md.j2
+++ b/lib/crewai/src/crewai/flow/templates/flow_definition_skill.md.j2
@@ -53,6 +53,7 @@ Pick the simplest action that does the job.
{% endif %}
{% if include_hitl %}
- Use `human_feedback` when a method needs a human checkpoint.
+- After a `human_feedback` method, read the review as `outputs.method_name.feedback` and `outputs.method_name.outcome`, and the method's own result as `outputs.method_name.output` (for example `${outputs.method_name.output.raw}`). This holds with or without `emit`.
{% endif %}
{% if include_script_action %}
- Use `call: script` only for trusted inline Python. Scripts are not sandboxed.
diff --git a/lib/crewai/tests/test_flow_from_definition.py b/lib/crewai/tests/test_flow_from_definition.py
index 1cc05cac0..fd45fb8b5 100644
--- a/lib/crewai/tests/test_flow_from_definition.py
+++ b/lib/crewai/tests/test_flow_from_definition.py
@@ -4006,6 +4006,119 @@ def test_human_feedback_pending_and_resume_from_declaration():
assert flow_id not in DefinitionStoreBackend.pending
+EMIT_REVIEW_EXPR_YAML = """
+schema: crewai.flow/v1
+name: EmitReviewExprFlow
+methods:
+ draft:
+ do:
+ call: expression
+ expr: "'draft-content'"
+ start: true
+ human_feedback:
+ message: "Review the draft:"
+ emit: [approved, rejected]
+ llm: gpt-4o-mini
+ default_outcome: rejected
+ rewrite:
+ do:
+ call: expression
+ expr: "outputs.draft.feedback + '|' + outputs.draft.outcome + '|' + outputs.draft.output"
+ listen: rejected
+"""
+
+
+def test_human_feedback_emit_exposes_feedback_in_outputs():
+ flow = Flow.from_declaration(contents=EMIT_REVIEW_EXPR_YAML)
+
+ with (
+ patch.object(flow, "_request_human_feedback", return_value="make it shorter"),
+ patch.object(flow, "_collapse_to_outcome", return_value="rejected"),
+ ):
+ result = flow.kickoff()
+
+ assert result == "make it shorter|rejected|draft-content"
+ assert flow.method_outputs[0] == "draft-content"
+
+
+def test_human_feedback_emit_feedback_renders_in_action_templates():
+ yaml_str = f"""
+schema: crewai.flow/v1
+name: EmitFeedbackTemplateFlow
+methods:
+ draft:
+ do:
+ call: expression
+ expr: "'draft-content'"
+ start: true
+ human_feedback:
+ message: "Review the draft:"
+ emit: [approved, rejected]
+ llm: gpt-4o-mini
+ rewrite:
+ do:
+ call: tool
+ ref: {__name__}:StaticSearchTool
+ with:
+ search_query: "Draft: ${{outputs.draft.output}}. Reason: ${{outputs.draft.feedback}}"
+ prefix: rewrite
+ listen: rejected
+"""
+ flow = Flow.from_declaration(contents=yaml_str)
+
+ with (
+ patch.object(flow, "_request_human_feedback", return_value="too long"),
+ patch.object(flow, "_collapse_to_outcome", return_value="rejected"),
+ ):
+ result = flow.kickoff()
+
+ assert result == "rewrite:Draft: draft-content. Reason: too long"
+
+
+PENDING_EMIT_REVIEW_YAML = f"""
+schema: crewai.flow/v1
+name: PendingEmitReviewFlow
+persist:
+ enabled: true
+ persistence:
+ persistence_type: DefinitionStoreBackend
+ store: hitl-pending-emit
+methods:
+ draft:
+ do:
+ call: expression
+ expr: "'draft-content'"
+ start: true
+ human_feedback:
+ message: "Review:"
+ emit: [approved, rejected]
+ llm: gpt-4o-mini
+ provider: {__name__}:PausingProvider
+ rewrite:
+ do:
+ call: expression
+ expr: "outputs.draft.feedback + '|' + outputs.draft.output"
+ listen: rejected
+"""
+
+
+def test_human_feedback_emit_exposes_feedback_in_outputs_after_resume():
+ definition = FlowDefinition.from_declaration(contents=PENDING_EMIT_REVIEW_YAML)
+ pending = Flow.from_declaration(contents=definition).kickoff()
+ assert isinstance(pending, HumanFeedbackPending)
+
+ resumed = Flow.from_pending(
+ pending.context.flow_id,
+ DefinitionStoreBackend(store="hitl-pending-emit"),
+ definition=definition,
+ )
+ with patch.object(resumed, "_collapse_to_outcome", return_value="rejected"):
+ result = resumed.resume("make it shorter")
+
+ assert result == "make it shorter|draft-content"
+ assert resumed.method_outputs[0] == "draft-content"
+
+
def test_flow_config_provider_fallback_from_declaration():
yaml_str = f"""
schema: crewai.flow/v1