[BUG] SQLiteFlowPersistence crashes with TypeError when Flow state contains datetime, UUID, or set fields
Description
When persisting Flow state via @persist or SQLiteFlowPersistence, workflows with structured Pydantic state models containing standard fields like datetime, UUID, set, Decimal, or Path crash during state saving with:
RuntimeError: State persistence failed: Object of type datetime is not JSON serializable
Root Cause:
In lib/crewai/src/crewai/flow/persistence/sqlite.py:
_to_state_dictcallsstate_data.model_dump()without specifyingmode="json". In Pydantic v2,model_dump()keeps native Python types (datetime.datetime,uuid.UUID,set, etc.) instead of converting them to JSON primitives.- In
_save_state_sql(line 142) andsave_pending_feedback(line 241),json.dumps(state_dict)is called directly without a serializer fallback (default=str), which immediately raises aTypeErroron any non-primitive type.
Steps to Reproduce
- Define a Flow with a Pydantic state model containing a
datetime(oruuid.UUID,set). - Attach
SQLiteFlowPersistenceusing@persiston a flow step. - Call
flow.kickoff(). - Observe the flow crashing upon completing the persisted method.
Expected behavior
SQLiteFlowPersistence should serialize Pydantic state models in JSON mode (model_dump(mode="json")) and handle fallback dicts gracefully with default=str. When loaded back via load_state, Pydantic's model_validate restores them to their native types (datetime, UUID, set) without data loss.
Screenshots/Code snippets
from datetime import datetime, timezone import uuid from pydantic import BaseModel, Field from crewai.flow.flow import Flow, start from crewai.flow.persistence import persist, SQLiteFlowPersistence
class WorkflowState(BaseModel): id: str = Field(default_factory=lambda: str(uuid.uuid4())) created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) user_id: uuid.UUID = Field(default_factory=uuid.uuid4) tags: set[str] = Field(default_factory=lambda: {"analytics", "v1"})
persistence = SQLiteFlowPersistence("test_flow.db")
class SampleFlow(Flow[WorkflowState]): initial_state = WorkflowState
@start()
@persist(persistence)
def step_one(self):
return "done"
flow = SampleFlow(persistence=persistence) flow.kickoff()
Operating System
macOS Sonoma
Python Version
3.12
crewAI Version
1.15.20
crewAI Tools Version
1.15.20
Virtual Environment
Venv
Evidence
Failed to persist state for method step_one: Object of type datetime is not JSON serializable Flow Method Failed
- Method: step_one
- Status: Failed
FLOW CRASHED: RuntimeError: State persistence failed: Object of type datetime is not JSON serializable
Possible Solution
- In
lib/crewai/src/crewai/flow/persistence/sqlite.py: update_to_state_dictto usestate_data.model_dump(mode="json"):
@staticmethod
def _to_state_dict(state_data: dict[str, Any] | BaseModel) -> dict[str, Any]:
"""Convert state_data to a plain dict."""
if isinstance(state_data, BaseModel):
return state_data.model_dump(mode="json")
if isinstance(state_data, dict):
return state_data
raise ValueError(
f"state_data must be either a Pydantic BaseModel or dict, got {type(state_data)}"
)
### Additional context
I have tested the fix locally along with full roundtrip serialization/deserialization verification and unit tests. Ready to submit a PR!
Hi Maintainers,
I would love to work on this! I've already tested the fix locally with full roundtrip serialization and deserialization across datetime, UUID, and set fields, and have the patch and unit tests ready to submit. If the proposal solutions looks good to you, I can work on it, Could you please assign this issue to me? Thanks!
Sounds good @Rohitkanithi
Hi @Vidit-Ostwal, Just following up on this, I've raised PR #7376 with the complete fix and regression test coverage. Whenever you have a moment, could you please take a look and review Thanks