crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] initial_state = FlowState() gives every Flow instance the same state.id

Open bballhaus opened this issue 2 weeks ago • 1 comments

Description

When a Flow sets initial_state to a FlowState instance, every instance of that Flow gets the same state.id, so separate runs share one flow_id. With @persist, runs with different inputs get saved under one flow_uuid, and load_state(flow_id) returns whichever run saved last.

Steps to Reproduce

  1. Define a Flow with initial_state = S() where S subclasses FlowState (snippet below)
  2. Create two instances: a, b = F(), F()
  3. Compare a.state.id and b.state.id

Expected behavior

Each Flow instance gets its own id, the same way it does for a dict, a plain BaseModel instance, or initial_state = S (the class).

Screenshots/Code snippets

from crewai.flow.flow import Flow, FlowState, start

class S(FlowState):
    counter: int = 0

class F(Flow[S]):
    initial_state = S()

    @start()
    def begin(self):
        pass

a, b = F(), F()
print(a.state.id, b.state.id, a.state.id == b.state.id)

Operating System

macOS Catalina

Python Version

3.12

crewAI Version

1.15.41

crewAI Tools Version

1.15.41

Virtual Environment

Venv

Evidence

The snippet prints the same id twice and True. With a plain BaseModel instance instead of a FlowState, the ids differ.

With @persist(SQLiteFlowPersistence(...)) and two kickoffs with different inputs, the flow_states table ends up with 2 rows under 1 flow_uuid.

Possible Solution

_create_initial_state in lib/crewai/src/crewai/flow/runtime/__init__.py only generates a new id when the model's id is empty. A FlowState() instance already got an id from default_factory=uuid4 when the class was defined, so that id gets copied into every instance.

I'd generate a new id unless the user set one explicitly ("id" not in model.model_fields_set), so S(id="fixed") still works, and add a regression test. I'd like to work on this.

Additional context

Related to #9383 / #9433, which change the dict branch of the same function but leave id handling alone.

AI disclosure: I used Claude Code to help find and reproduce this. Could a maintainer add the llm-generated label?

bballhaus avatar Nov 16 '26 23:11 bballhaus

Reproduced the mechanism by reading the code (could not run it here — no local crewai venv on this machine, so this is a code-read verification, not an execution trace).

FlowState.id is Field(default_factory=lambda: str(uuid4())) (lib/crewai/src/crewai/flow/runtime/__init__.py:376), so S() bakes one id in at class-definition time. In Flow._create_initial_state (runtime/__init__.py:1708+), the isinstance(init_state, BaseModel) branch does state_dict = model.model_dump() and only regenerates the id if not state_dict.get("id") — a FlowState() instance always has a truthy id, so every F() reuses the same one via model_class(**state_dict). The type branch (initial_state = S, the class) constructs a fresh state_class() per Flow, which is why the class form gets a fresh id and the instance form does not. The dict branch has the same shape of bug guarded differently ("id" not in new_state regenerates), so the instance branch is the odd one out.

The proposed fix ("id" not in model.model_fields_set, regenerating otherwise) matches the code: S(id="fixed") keeps its explicit id, S() gets a fresh one per Flow. One extra check for the fix: model_dump() + re-construct also re-runs field validation on every Flow instantiation, so the regeneration should happen on the dumped dict before model_class(**state_dict) (as proposed), not by mutating the shared initial_state instance in place.

surlymochan avatar Nov 17 '26 05:11 surlymochan