Skip to content

TypeError in TinyPersonFactory when LOGLEVEL=INFO/DEBUG: set is not JSON serializable #140

@takechanman1228

Description

@takechanman1228

Summary

TinyPersonFactory.create_factory_from_demography() raises a TypeError when logging level is set to INFO or DEBUG in config.ini. The error occurs in debug logging code that attempts to serialize self.sampling_dimensions using json.dumps(), but the dictionary contains Python set objects which are not JSON serializable.

This issue does not occur when LOGLEVEL=ERROR (the default), but users who enable more verbose logging for debugging purposes encounter this crash.

Error Traceback

TypeError: Object of type set is not JSON serializable

File ~/TinyTroupe/tinytroupe/factory/tiny_person_factory.py, line 532, in _initialize_sampling_plan_transaction
    logger.debug(f"Sampling dimensions: {json.dumps(self.sampling_dimensions, indent=4)}")

File ~/miniconda3/lib/python3.12/json/encoder.py, line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')

Root Cause

File: tinytroupe/factory/tiny_person_factory.py
Line: 532

self.sampling_dimensions = utils.try_function(lambda: self._compute_sampling_dimensions(...))
logger.info("Sampling dimensions computed successfully.")
logger.debug(f"Sampling dimensions: {json.dumps(self.sampling_dimensions, indent=4)}")  # ← CRASHES HERE

Problem:

  • self.sampling_dimensions contains Python set objects (likely used for deduplication)
  • json.dumps() cannot serialize set type by default
  • This logging statement only executes when LOGLEVEL=DEBUG

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions