docs: fix the guide on persistence (#2849)
Some checks failed
Notify Downstream / notify-downstream (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled

Co-authored-by: Lucas Gomide <lucaslg200@gmail.com>
This commit is contained in:
Richard Luo
2025-06-09 14:09:56 -04:00
committed by GitHub
parent e9d9dd2a79
commit 5b740467cb

View File

@@ -277,22 +277,23 @@ This pattern allows you to combine direct data passing with state updates for ma
One of CrewAI's most powerful features is the ability to persist flow state across executions. This enables workflows that can be paused, resumed, and even recovered after failures. One of CrewAI's most powerful features is the ability to persist flow state across executions. This enables workflows that can be paused, resumed, and even recovered after failures.
### The @persist Decorator ### The @persist() Decorator
The `@persist` decorator automates state persistence, saving your flow's state at key points in execution. The `@persist()` decorator automates state persistence, saving your flow's state at key points in execution.
#### Class-Level Persistence #### Class-Level Persistence
When applied at the class level, `@persist` saves state after every method execution: When applied at the class level, `@persist()` saves state after every method execution:
```python ```python
from crewai.flow.flow import Flow, listen, persist, start from crewai.flow.flow import Flow, listen, start
from crewai.flow.persistence import persist
from pydantic import BaseModel from pydantic import BaseModel
class CounterState(BaseModel): class CounterState(BaseModel):
value: int = 0 value: int = 0
@persist # Apply to the entire flow class @persist() # Apply to the entire flow class
class PersistentCounterFlow(Flow[CounterState]): class PersistentCounterFlow(Flow[CounterState]):
@start() @start()
def increment(self): def increment(self):
@@ -319,10 +320,11 @@ print(f"Second run result: {result2}") # Will be higher due to persisted state
#### Method-Level Persistence #### Method-Level Persistence
For more granular control, you can apply `@persist` to specific methods: For more granular control, you can apply `@persist()` to specific methods:
```python ```python
from crewai.flow.flow import Flow, listen, persist, start from crewai.flow.flow import Flow, listen, start
from crewai.flow.persistence import persist
class SelectivePersistFlow(Flow): class SelectivePersistFlow(Flow):
@start() @start()
@@ -330,7 +332,7 @@ class SelectivePersistFlow(Flow):
self.state["count"] = 1 self.state["count"] = 1
return "First step" return "First step"
@persist # Only persist after this method @persist() # Only persist after this method
@listen(first_step) @listen(first_step)
def important_step(self, prev_result): def important_step(self, prev_result):
self.state["count"] += 1 self.state["count"] += 1