FamilyManager.CurrentType returns transaction error

Hello everyone, I’m trying to rename all familytypes of a category of families in my project via Dynamo.
When running a python node to rename family types (this is after I have opened all family documents in the background) I get the following exception: A sub-transaction can only be active inside an open Transaction. The error occurs on the line of document.FamilyManager.CurrentType = ftype.

I can’t figure out where this problem is comming from since on the line before the error I open a new transaction. I hope anyone can help me understand why the script is giving me this error.

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

projectDocument = DocumentManager.Instance.CurrentDBDocument

#IN[0] the documents that are opened in the background
#IN[1] The typenames as a list with sublists where the sublists equals the amount of types in the family
doc = IN[0]
newTypeNames = IN[1]
typeslist = list()
typenameslist = list()
newNames = list()
i = 0
j = 0

for document in doc:
	ftypelist = []
	ftypenameslist = []
	specificTypeNames = newTypeNames[i]
	
	for ftype in document.FamilyManager.Types:

		TransactionManager.Instance.EnsureInTransaction(document)
		
		document.FamilyManager.CurrentType = ftype
		try:
			document.FamilyManager.RenameCurrentType(specificTypeNames[j])
			newNames.append(ftype.Name)
		except:
			newNames.append(['failed to change name',ftype.Name])
		j+=1
		
		TransactionManager.Instance.TransactionTaskDone()	
	i+=1
	j = 0	

OUT = newNames