deepfakes/faceswap · error · FaceswapError

Error serializing data for type {type(data)}: {str(err)}

Error message

Error serializing data for type {type(data)}: {str(err)}

What it means

Serializer.marshal wraps ANY exception from the format-specific _marshal call: the data could not be serialized to the target format (commonly pickle-refusing objects, JSON-unserializable types like numpy arrays/sets/datetime, or circular references). FaceswapError reports the data type and the underlying error string.

Source

Thrown at lib/serializer.py:142

            The data that is to be serialized

        Returns
        -------
        data: varies
            The data in a the serialized data format

        Example
        ------
        >>> serializer = get_serializer('json')
        >>> data ['foo', 'bar']
        >>> json_data = serializer.marshal(data)
        """
        logger.debug("data type: %s", type(data))
        try:
            retval = self._marshal(data)
        except Exception as err:
            msg = f"Error serializing data for type {type(data)}: {str(err)}"
            raise FaceswapError(msg) from err
        logger.debug("returned data type: %s", type(retval))
        return retval

    def unmarshal(self, serialized_data):
        """ Unserialize data to its original object type

        Parameters
        ----------
        serialized_data: varies
            Data in serializer format that is to be unmarshalled to its original object

        Returns
        -------
        data: varies
            The data in a python object format

        Example
        ------

View on GitHub (pinned to f530cb7508)

Solutions

  1. Convert numpy types before serializing: ndarray.tolist(), numpy scalars via .item().
  2. Use the pickle serializer for arbitrary objects, or a custom default= handler for JSON.
  3. Read str(err) in the message — it names the exact unserializable type.

Example fix

import numpy as np

# before
data = {'landmarks': np.array([1.0, 2.0])}
get_serializer('json').marshal(data)  # TypeError -> FaceswapError

# after
data = {'landmarks': np.array([1.0, 2.0]).tolist()}
get_serializer('json').marshal(data)
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def json_safe(obj):
    if isinstance(obj, np.ndarray):
        return obj.tolist()
    if isinstance(obj, (np.integer,)):
        return int(obj)
    if isinstance(obj, (np.floating,)):
        return float(obj)
    raise TypeError(f'not JSON serializable: {type(obj)}')

json.dumps(data, default=json_safe)  # dry-run before serializer.save

Try / catch

try:
    serializer.save(filename, data)
except FaceswapError as err:
    if 'Error serializing' in str(err):
        data = sanitize(data)  # convert numpy/custom types, then retry once
        serializer.save(filename, data)
    else:
        raise

Prevention

When it happens

Trigger: Calling save/marshal with json serializer on data containing numpy arrays, sets, tuples-as-keys, or custom objects without a JSON encoder; pickling lambdas or unpicklable objects with the pickle serializer.

Common situations: Persisting alignments or state dicts that embed numpy landmarks; user plugins injecting custom objects into serialized state; version upgrades changing stored structures.

Related errors


AI-assisted analysis of deepfakes/faceswap@f530cb7508 (2026-08-15). Data as JSON: /api/errors/deb5d5fe5070b5d2. Report an issue: GitHub.