Skip to content

[BUG] akickoff_for_each fails inside of flows #4385

@deanchanter

Description

@deanchanter

Description

Summary

akickoff_for_each fails with a Pydantic validation error when a Crew is created inside a Flow method, even when explicitly setting parent_flow=None. The copy() method called internally by akickoff_for_each attempts to copy the parent_flow reference, which fails validation.

Environment

  • CrewAI Version: 1.9.3
  • Python Version: 3.13
  • Platform: macOS Darwin 25.1.0

Steps to Reproduce

Steps to Reproduce

  1. Create a Flow class that inherits from Flow[SomeState]
  2. Inside a Flow method (decorated with @listen or @start), create a Crew
  3. Call await crew.akickoff_for_each(inputs=[...]) on the crew
from crewai import Agent, Crew, Task, Process
from crewai.flow.flow import Flow, start, listen

class MyState(BaseModel):
    question: str = ""

class MyFlow(Flow[MyState]):

    @start()
    async def run_batch(self):
        agent = Agent(role="Worker", goal="Do work", backstory="...")
        task = Task(description="{task_input}", agent=agent, expected_output="...")

        # Even with parent_flow=None, this fails
        crew = Crew(
            agents=[agent],
            tasks=[task],
            process=Process.sequential,
            parent_flow=None,  # Explicitly set to None
        )

        inputs = [
            {"task_input": "Task 1"},
            {"task_input": "Task 2"},
        ]

        # This call fails
        results = await crew.akickoff_for_each(inputs=inputs)
        return results

Expected behavior

Expected Behavior

akickoff_for_each should execute each input in parallel using native async, respecting the parent_flow=None setting.

Actual Behavior

The call fails with:

pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
parent_flow
  Input should be an instance of Flow.__class_getitem__.<locals>._FlowGeneric [type=is_instance_of, input_value=<my_module.MyFlow object at 0x...>, input_type=MyFlow]

Screenshots/Code snippets

2026-02-05 17:08:00,235 - deep_agent.tools.planning_tool - DEBUG - Todo [dcee3a9a] status: pending -> in_progress
2026-02-05 17:08:00,235 - deep_agent.tools.planning_tool - DEBUG - Todo [303158ba] status: pending -> in_progress
2026-02-05 17:08:00,235 - deep_agent.tools.planning_tool - DEBUG - Todo [3ab75fbc] status: pending -> in_progress
2026-02-05 17:08:00,235 - deep_agent.flow - DEBUG - Executing batch of 3 todos via akickoff_for_each
2026-02-05 17:08:00,238 - deep_agent.flow - ERROR - Batch execution failed
Traceback (most recent call last):
File "/Users/deanchanter/Documents/BitBucket/DIU/diu-agentic-service/src/deep_agent/flow.py", line 162, in _execute_batch
crew_outputs = await explorer_crew.crew().akickoff_for_each(inputs=inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 977, in akickoff_for_each
return await run_for_each_async(self, inputs, kickoff_fn)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crews/utils.py", line 385, in run_for_each_async
crew_copies = [crew.copy() for _ in inputs]
~~~~~~~~~^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 1704, in copy
return Crew(
**copied_data,
...<5 lines>...
manager_llm=manager_llm,
)
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/pydantic/main.py", line 253, in init
validated_self = self.pydantic_validator.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.flow - WARNING - Todo [dcee3a9a] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [dcee3a9a] status: in_progress -> blocked
2026-02-05 17:08:00,241 - deep_agent.flow - WARNING - Todo [303158ba] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [303158ba] status: in_progress -> blocked
2026-02-05 17:08:00,241 - deep_agent.flow - WARNING - Todo [3ab75fbc] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [3ab75fbc] status: in_progress -> blocked
2026-02-05 17:08:00,241 - deep_agent.flow - DEBUG - No completed results in batch, skipping plan update
2026-02-05 17:08:00,241 - deep_agent.flow - DEBUG - Executing batch 2: 2 todos
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [1c7dcd6b] status: pending -> in_progress
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [3a62e5cc] status: pending -> in_progress
2026-02-05 17:08:00,241 - deep_agent.flow - DEBUG - Executing batch of 2 todos via akickoff_for_each
2026-02-05 17:08:00,242 - deep_agent.flow - ERROR - Batch execution failed
Traceback (most recent call last):
File "/Users/deanchanter/Documents/BitBucket/DIU/diu-agentic-service/src/deep_agent/flow.py", line 162, in _execute_batch
crew_outputs = await explorer_crew.crew().akickoff_for_each(inputs=inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 977, in akickoff_for_each
return await run_for_each_async(self, inputs, kickoff_fn)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crews/utils.py", line 385, in run_for_each_async
crew_copies = [crew.copy() for _ in inputs]
~~~~~~~~~^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 1704, in copy
return Crew(
**copied_data,
...<5 lines>...
manager_llm=manager_llm,
)
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/pydantic/main.py", line 253, in init
validated_self = self.pydantic_validator.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,243 - deep_agent.flow - WARNING - Todo [1c7dcd6b] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,243 - deep_agent.tools.planning_tool - DEBUG - Todo [1c7dcd6b] status: in_progress -> blocked
2026-02-05 17:08:00,243 - deep_agent.flow - WARNING - Todo [3a62e5cc] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,243 - deep_agent.tools.planning_tool - DEBUG - Todo [3a62e5cc] status: in_progress -> blocked
2026-02-05 17:08:00,243 - deep_agent.flow - DEBUG - No completed results in batch, skipping plan update
2026-02-05 17:08:00,243 - deep_agent.flow - DEBUG - No more pending graph explorer todos
2026-02-05 17:08:00,243 - deep_agent.flow - DEBUG - Plan execution complete: {'total': 6, 'pending': 0, 'in_progress': 0, 'completed': 0, 'blocked': 6}

Operating System

macOS Sonoma

Python Version

3.12

crewAI Version

1.9.3

crewAI Tools Version

1.9.3

Virtual Environment

Poetry

Evidence

2026-02-05 17:08:00,235 - deep_agent.tools.planning_tool - DEBUG - Todo [dcee3a9a] status: pending -> in_progress
2026-02-05 17:08:00,235 - deep_agent.tools.planning_tool - DEBUG - Todo [303158ba] status: pending -> in_progress
2026-02-05 17:08:00,235 - deep_agent.tools.planning_tool - DEBUG - Todo [3ab75fbc] status: pending -> in_progress
2026-02-05 17:08:00,235 - deep_agent.flow - DEBUG - Executing batch of 3 todos via akickoff_for_each
2026-02-05 17:08:00,238 - deep_agent.flow - ERROR - Batch execution failed
Traceback (most recent call last):
File "/Users/deanchanter/Documents/BitBucket/DIU/diu-agentic-service/src/deep_agent/flow.py", line 162, in _execute_batch
crew_outputs = await explorer_crew.crew().akickoff_for_each(inputs=inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 977, in akickoff_for_each
return await run_for_each_async(self, inputs, kickoff_fn)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crews/utils.py", line 385, in run_for_each_async
crew_copies = [crew.copy() for _ in inputs]
~~~~~~~~~^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 1704, in copy
return Crew(
**copied_data,
...<5 lines>...
manager_llm=manager_llm,
)
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/pydantic/main.py", line 253, in init
validated_self = self.pydantic_validator.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.flow - WARNING - Todo [dcee3a9a] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [dcee3a9a] status: in_progress -> blocked
2026-02-05 17:08:00,241 - deep_agent.flow - WARNING - Todo [303158ba] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [303158ba] status: in_progress -> blocked
2026-02-05 17:08:00,241 - deep_agent.flow - WARNING - Todo [3ab75fbc] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [3ab75fbc] status: in_progress -> blocked
2026-02-05 17:08:00,241 - deep_agent.flow - DEBUG - No completed results in batch, skipping plan update
2026-02-05 17:08:00,241 - deep_agent.flow - DEBUG - Executing batch 2: 2 todos
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [1c7dcd6b] status: pending -> in_progress
2026-02-05 17:08:00,241 - deep_agent.tools.planning_tool - DEBUG - Todo [3a62e5cc] status: pending -> in_progress
2026-02-05 17:08:00,241 - deep_agent.flow - DEBUG - Executing batch of 2 todos via akickoff_for_each
2026-02-05 17:08:00,242 - deep_agent.flow - ERROR - Batch execution failed
Traceback (most recent call last):
File "/Users/deanchanter/Documents/BitBucket/DIU/diu-agentic-service/src/deep_agent/flow.py", line 162, in _execute_batch
crew_outputs = await explorer_crew.crew().akickoff_for_each(inputs=inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 977, in akickoff_for_each
return await run_for_each_async(self, inputs, kickoff_fn)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crews/utils.py", line 385, in run_for_each_async
crew_copies = [crew.copy() for _ in inputs]
~~~~~~~~~^^
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/crewai/crew.py", line 1704, in copy
return Crew(
**copied_data,
...<5 lines>...
manager_llm=manager_llm,
)
File "/Users/deanchanter/Library/Caches/pypoetry/virtualenvs/genai-agentic-service-s6IDVAfs-py3.13/lib/python3.13/site-packages/pydantic/main.py", line 253, in init
validated_self = self.pydantic_validator.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,243 - deep_agent.flow - WARNING - Todo [1c7dcd6b] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,243 - deep_agent.tools.planning_tool - DEBUG - Todo [1c7dcd6b] status: in_progress -> blocked
2026-02-05 17:08:00,243 - deep_agent.flow - WARNING - Todo [3a62e5cc] failed: 1 validation error for Crew
parent_flow
Input should be an instance of Flow.class_getitem.._FlowGeneric [type=is_instance_of, input_value=<deep_agent.flow.Neo4jDee...w object at 0x118f5f230>, input_type=Neo4jDeepAgentFlow]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
2026-02-05 17:08:00,243 - deep_agent.tools.planning_tool - DEBUG - Todo [3a62e5cc] status: in_progress -> blocked
2026-02-05 17:08:00,243 - deep_agent.flow - DEBUG - No completed results in batch, skipping plan update
2026-02-05 17:08:00,243 - deep_agent.flow - DEBUG - No more pending graph explorer todos
2026-02-05 17:08:00,243 - deep_agent.flow - DEBUG - Plan execution complete: {'total': 6, 'pending': 0, 'in_progress': 0, 'completed': 0, 'blocked': 6}

Possible Solution

Suggested Fix

Option 1: Modify copy() to exclude parent_flow from copied_data:

def copy(self) -> "Crew":
    copied_data = self.model_dump(exclude={"parent_flow", ...})
    ...

Option 2: Modify run_for_each_async to create fresh crew instances instead of copying:

async def run_for_each_async(crew, inputs, kickoff_fn):
    # Instead of crew.copy(), recreate crews fresh
    ...

Option 3: Fix the Flow context detection to not attach parent_flow when explicitly set to None.

Additional context

Root Cause Analysis

The issue occurs in crewai/crews/utils.py at line 385:

async def run_for_each_async(crew, inputs, kickoff_fn):
    crew_copies = [crew.copy() for _ in inputs]  # <-- This fails
    ...

The crew.copy() method in crewai/crew.py at line 1704 includes parent_flow in copied_data:

def copy(self) -> "Crew":
    ...
    return Crew(
        **copied_data,  # <-- copied_data includes parent_flow from the original
        ...
    )

Even when parent_flow=None is set during Crew construction, CrewAI appears to detect the Flow context and attach a reference anyway. When copy() is called, this reference is included in copied_data, causing the validation error because the Flow type doesn't match the expected generic type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions