{"record":{"id":"9fb17561ee7831e0","repo":"Unity-Technologies/ml-agents","slug":"unknown-statsaggregationmethod-encountered-agg-t","errorCode":null,"errorMessage":"Unknown StatsAggregationMethod encountered. {agg_type}","messagePattern":"Unknown StatsAggregationMethod encountered\\. (.+?)","errorType":"exception","errorClass":"UnityTrainerException","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/agent_processor.py","lineNumber":467,"sourceCode":"        :param env_stats:\n        :param worker_id:\n        :return:\n        \"\"\"\n        for stat_name, value_list in env_stats.items():\n            for val, agg_type in value_list:\n                if agg_type == StatsAggregationMethod.AVERAGE:\n                    self._stats_reporter.add_stat(stat_name, val, agg_type)\n                elif agg_type == StatsAggregationMethod.SUM:\n                    self._stats_reporter.add_stat(stat_name, val, agg_type)\n                elif agg_type == StatsAggregationMethod.HISTOGRAM:\n                    self._stats_reporter.add_stat(stat_name, val, agg_type)\n                elif agg_type == StatsAggregationMethod.MOST_RECENT:\n                    # In order to prevent conflicts between multiple environments,\n                    # only stats from the first environment are recorded.\n                    if worker_id == 0:\n                        self._stats_reporter.set_stat(stat_name, val)\n                else:\n                    raise UnityTrainerException(\n                        f\"Unknown StatsAggregationMethod encountered. {agg_type}\"\n                    )\n","sourceCodeStart":449,"sourceCodeEnd":470,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/agent_processor.py#L449-L470","documentation":"AgentProcessor.record_environment_stats reads a StatsSummary's aggregation type and only handles SUM/AVERAGE/HISTOGRAM/MOST_RECENT. An unknown StatsAggregationMethod means a new enum value from a different mlagents version or an uninitialized/invalid StatsSummary, so UnityTrainerException is raised.","triggerScenarios":"Calling record_environment_stats with a StatsSummary whose agg_type is not one of the four supported StatsAggregationMethod values (e.g. enum added in a newer package, or a manually constructed StatsSummary with a bad value).","commonSituations":"Mixing mlagents/mlagents_envs versions so env stats carry an aggregation type the trainer does not know; custom training loops constructing StatsSummary incorrectly.","solutions":["Match mlagents_envs and mlagents package versions (pip install -U mlagents==<envs version pair>).","Only pass StatsSummary objects created by the standard env-stats path, not hand-built ones.","Update the package so both sides share the same StatsAggregationMethod enum.","Catch UnityTrainerException and log the offending agg_type value for diagnosis."],"exampleFix":"// before\nsummary = StatsSummary(value_list=[1.0], agg_type=MyCustomAggType)  # unsupported\ntrainer.record_environment_stats(summary, worker_id=0)\n// after\nfrom mlagents_envs.side_channel.stats_side_channel import StatsAggregationMethod\nsummary = StatsSummary(value_list=[1.0], agg_type=StatsAggregationMethod.AVERAGE)\ntrainer.record_environment_stats(summary, worker_id=0)","handlingStrategy":"try-catch","validationCode":"from mlagents_envs.side_channel.stats_side_channel import StatsAggregationMethod\nassert stats_summary.agg_type in {StatsAggregationMethod.SUM, StatsAggregationMethod.AVERAGE, StatsAggregationMethod.HISTOGRAM, StatsAggregationMethod.MOST_RECENT}","typeGuard":"def is_supported_agg(agg_type) -> bool:\n    return agg_type in {StatsAggregationMethod.SUM, StatsAggregationMethod.AVERAGE,\n                        StatsAggregationMethod.HISTOGRAM, StatsAggregationMethod.MOST_RECENT}","tryCatchPattern":"from mlagents.trainers.exception import UnityTrainerException\ntry:\n    agent_processor.record_environment_stats(stats_summary, worker_id)\nexcept UnityTrainerException as e:\n    logging.error(\"Unsupported stats aggregation (version mismatch?): %s\", e)","preventionTips":["Keep mlagents and mlagents_envs versions aligned","Pass only env-produced StatsSummary objects into the trainer","After upgrading packages, run a short training smoke test","Avoid inventing custom StatsAggregationMethod values"],"tags":["trainer","stats","version-mismatch"],"backgroundTag":"invalid-enum-value","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}