diff --git a/src/crewai/flow/persistence/sqlite.py b/src/crewai/flow/persistence/sqlite.py index 8f3d0b80f..0a9971a93 100644 --- a/src/crewai/flow/persistence/sqlite.py +++ b/src/crewai/flow/persistence/sqlite.py @@ -90,23 +90,26 @@ class SQLiteFlowPersistence(FlowPersistence): f"state_data must be either a Pydantic BaseModel or dict, got {type(state_data)}" ) - with sqlite3.connect(self.db_path) as conn: - conn.execute( - """ - INSERT INTO flow_states ( - flow_uuid, - method_name, - timestamp, - state_json - ) VALUES (?, ?, ?, ?) - """, - ( + try: + with sqlite3.connect(self.db_path) as conn: + conn.execute( + """ + INSERT INTO flow_states ( flow_uuid, method_name, - datetime.now(timezone.utc).isoformat(), - json.dumps(state_dict, cls=CrewJSONEncoder), - ), - ) + timestamp, + state_json + ) VALUES (?, ?, ?, ?) + """, + ( + flow_uuid, + method_name, + datetime.now(timezone.utc).isoformat(), + json.dumps(state_dict, cls=CrewJSONEncoder), + ), + ) + except json.JSONEncodeError as e: + raise RuntimeError(f"Failed to serialize flow state: {str(e)}") from e def load_state(self, flow_uuid: str) -> Optional[Dict[str, Any]]: """Load the most recent state for a given flow UUID.