Skip to content

docs(flows): fix class-level persistence example to pass flow_id for state resumption#5694

Open
Ghraven wants to merge 1 commit intocrewAIInc:mainfrom
Ghraven:docs/fix-flow-state-persistence-example
Open

docs(flows): fix class-level persistence example to pass flow_id for state resumption#5694
Ghraven wants to merge 1 commit intocrewAIInc:mainfrom
Ghraven:docs/fix-flow-state-persistence-example

Conversation

@Ghraven
Copy link
Copy Markdown

@Ghraven Ghraven commented May 4, 2026

Fixes #5378

Problem

The class-level @persist() example in mastering-flow-state.mdx shows a second PersistentCounterFlow() that is expected to resume from the first run's persisted state. In practice both runs produce result 2 — the same output — because every PersistentCounterFlow() call generates a fresh UUID and thus writes to a new persistence slot rather than loading the previous one.

First run result: 2
Second run result: 2   ← should be 6, not 2

Root Cause

Flow.__init__ assigns state.id = uuid4() on every construction. The @persist() decorator saves and loads state keyed by this ID. When flow2 = PersistentCounterFlow() is called without arguments, it gets a different ID than flow1 and no prior state is found to restore.

Fix

Capture flow1.flow_id after the first kickoff and pass it to the second instance via inputs={"id": flow_id}. This routes flow2 to the correct persisted entry, so the second run resumes from value=2 and produces 6 (increment → 3, double → 6).

# First run
flow1 = PersistentCounterFlow()
result1 = flow1.kickoff()
print(f"First run result: {result1}")  # Output: 2

flow_id = flow1.flow_id

# Second run - pass the same flow_id to load persisted state
flow2 = PersistentCounterFlow(inputs={"id": flow_id})
result2 = flow2.kickoff()
print(f"Second run result: {result2}")  # Output: 6

Inline output comments are also added so expected values are explicit.

…state resumption

Fixes crewAIInc#5378

The original code example created a second `PersistentCounterFlow()` without
any arguments, which generates a fresh UUID and therefore does not load the
first run's persisted state. Both runs produced result `2` instead of the
expected increasing value.

Fix: capture `flow1.flow_id` after the first kickoff and pass it via
`inputs={"id": flow_id}` to the second instance. This instructs the
persistence layer to restore state from the previous run, so the second
run resumes from `value=2` and produces `6` (increment: 3, double: 6).

Also add inline output comments so the expected values are explicit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Wrong code in document

1 participant