code: def make_serializable(data):
      
      if is_serializable(data):
          return data
  
      # if numpy array convert to list
      try:
          return data.tolist()
      except AttributeError:
          pass
      except Exception as e:
          logger.debug('{} exception ({}): {}'.format(type(e).__name__, e, data))
  
      # try serializing each child element
      if isinstance(data, dict):
          return {key: make_serializable(value) for key, value in data.items()}
      try:
          return [make_serializable(element) for element in data]
      except TypeError:  # not iterable
          pass
      except Exception:
          logger.debug('Could not serialize {}; converting to string'.format(data))
  
      # last resort: convert to string
      return str(data)
docs: Ensure data is serializable.