Hello everyone,
I’m currently following the official CrewAI documentation (Mastering Flow State Management - CrewAI) and testing the example provided, but I’m running into an issue with the persist
decorator. It doesn’t seem to function as expected.
Despite following the steps in the documentation, the decorator doesn’t store or retrieve values across executions as intended. Additionally, I noticed that the import path mentioned in the documentation is incorrect. I had to import the decorator using:
from crewai.flow.persistence import persist
Also, it seems like the persist
decorator requires a parameter, which isn’t mentioned clearly in the documentation. This is causing further confusion, as I wasn’t expecting this behavior.
Below the revides code
from crewai.flow.flow import Flow, listen, start
from crewai.flow.persistence import persist
from pydantic import BaseModel
class CounterState(BaseModel):
value: int = 0
@persist(verbose=False) # Apply to the entire flow class
class PersistentCounterFlow(Flow[CounterState]):
@start()
def increment(self):
self.state.value += 1
print(f"Incremented to {self.state.value}")
return self.state.value
@listen(increment)
def double(self, value):
self.state.value = value * 2
print(f"Doubled to {self.state.value}")
return self.state.value
# First run
flow1 = PersistentCounterFlow()
result1 = flow1.kickoff()
print(f"First run result: {result1}")
# Second run - state is automatically loaded
flow2 = PersistentCounterFlow()
result2 = flow2.kickoff()
print(f"Second run result: {result2}") # Will be higher due to persisted state
The output I’m expecting is:
First run result: 2
Second run result: 6
However, I’m getting:
First run result: 2
Second run result: 2
Has anyone else encountered this? Any insights or advice would be much appreciated!
Thanks in advance!