Skip to content

fix(flow): keep fields added after a checkpoint on dict state restore - #6707

Open
bardock-2393 wants to merge 1 commit into
crewAIInc:mainfrom
bardock-2393:fix/flow-dict-state-restore-clear
Open

fix(flow): keep fields added after a checkpoint on dict state restore#6707
bardock-2393 wants to merge 1 commit into
crewAIInc:mainfrom
bardock-2393:fix/flow-dict-state-restore-clear

Conversation

@bardock-2393

Copy link
Copy Markdown

Summary

Contributes to #6706 by keeping dict-typed flow state fields that were added to initial_state after an older checkpoint was saved.

_restore_state cleared the dict state before applying a stored checkpoint. self._state is populated from the flow's current initial_state at construction, so any key added since an older checkpoint was saved existed on the fresh instance with its default value — until .clear() wiped it out, leaving it missing entirely instead of falling back to that default. The BaseModel branch right below doesn't have this problem, since model_validate already fills unset fields from the model's own defaults; this brings the dict branch to the same behavior by merging the stored values on top of the constructed defaults instead of replacing the state outright.

Testing

  • uv run pytest lib/crewai/tests/test_flow_persistence.py::test_dict_state_restore_keeps_fields_added_after_the_checkpoint -q
  • uv run pytest lib/crewai/tests/test_flow_persistence.py lib/crewai/tests/test_flow_persistence_factory.py -q (18 passed)
  • uv run pytest lib/crewai/tests/ -k flow -q (600 passed, 22 skipped)
  • uv run ruff check lib/crewai/src/crewai/flow/runtime/__init__.py lib/crewai/tests/test_flow_persistence.py
  • uv run ruff format --check lib/crewai/src/crewai/flow/runtime/__init__.py lib/crewai/tests/test_flow_persistence.py
  • uv run mypy lib/crewai/src/crewai/flow/runtime/__init__.py

_restore_state cleared dict-typed flow state before applying a stored
checkpoint, so a key added to initial_state after that checkpoint was
saved was wiped out instead of keeping its fresh default. Merges the
stored values onto the freshly-constructed state instead, matching how
the BaseModel branch already fills unset fields from the model's
defaults via model_validate.

Contributes to crewAIInc#6706
@bardock-2393

Copy link
Copy Markdown
Author

Per CONTRIBUTING.md, this should carry the llm-generated label — I don't have permission to add it myself. Could a maintainer please apply it?

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7821e2ef-c6b4-4150-91b1-c6efc06f0df2

📥 Commits

Reviewing files that changed from the base of the PR and between f15844b and 27315b3.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/flow/runtime/__init__.py
  • lib/crewai/tests/test_flow_persistence.py

📝 Walkthrough

Walkthrough

Dictionary-based flow state restoration now merges persisted values into the current state, preserving fields added after a checkpoint. A regression test verifies restored values and newly introduced defaults coexist.

Changes

Flow state restoration

Layer / File(s) Summary
Dictionary state merge and regression coverage
lib/crewai/src/crewai/flow/runtime/__init__.py, lib/crewai/tests/test_flow_persistence.py
Flow._restore_state preserves current dictionary fields while applying persisted values, and the new test verifies defaults added after checkpoint creation remain available.

Suggested reviewers: lucasgomide

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: preserving newly added dict state fields during checkpoint restore.
Description check ✅ Passed The description accurately explains the state-restore change and the added test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant