{"record":{"id":"78378c59af5a2e60","repo":"Unity-Technologies/ml-agents","slug":"the-demonstrations-do-not-have-the-same-number-of","errorCode":null,"errorMessage":"The demonstrations do not have the same number of observations as the policy.","messagePattern":"The demonstrations do not have the same number of observations as the policy\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/demo_loader.py","lineNumber":128,"sourceCode":"    :param file_path: Location of demonstration file (.demo).\n    :param sequence_length: Length of trajectories to fill buffer.\n    :return:\n    \"\"\"\n    behavior_spec, info_action_pair, _ = load_demonstration(file_path)\n    demo_buffer = make_demo_buffer(info_action_pair, behavior_spec, sequence_length)\n    if expected_behavior_spec:\n        # check action dimensions in demonstration match\n        if behavior_spec.action_spec != expected_behavior_spec.action_spec:\n            raise RuntimeError(\n                \"The actions {} in demonstration do not match the policy's {}.\".format(\n                    behavior_spec.action_spec, expected_behavior_spec.action_spec\n                )\n            )\n        # check observations match\n        if len(behavior_spec.observation_specs) != len(\n            expected_behavior_spec.observation_specs\n        ):\n            raise RuntimeError(\n                \"The demonstrations do not have the same number of observations as the policy.\"\n            )\n        else:\n            for i, (demo_obs, policy_obs) in enumerate(\n                zip(\n                    behavior_spec.observation_specs,\n                    expected_behavior_spec.observation_specs,\n                )\n            ):\n                if demo_obs.shape != policy_obs.shape:\n                    raise RuntimeError(\n                        f\"The shape {demo_obs} for observation {i} in demonstration \\\n                        do not match the policy's {policy_obs}.\"\n                    )\n    return behavior_spec, demo_buffer\n\n\ndef get_demo_files(path: str) -> List[str]:","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/demo_loader.py#L110-L146","documentation":"demo_to_buffer validates that a recorded demonstration file's BehaviorSpec matches the behavior spec expected by the policy being trained. This RuntimeError is thrown when the number of observation specs in the demonstration differs from the number the policy has. ML-Aggers throws it early so training never starts on incompatible data.","triggerScenarios":"Calling demo_to_buffer (via __init__ of a trainer using demonstrations, e.g. through demo_path in trainer config) where behavior_spec.observation_specs and expected_behavior_spec.observation_specs have different lengths, e.g. the .demo file was recorded with a different number of sensors/cameras than the environment used for training.","commonSituations":"Adding or removing a camera/visual sensor or vector observation in the Unity scene after recording demos; loading a demo from a different environment build; mismatched sensor flags (e.g. recording had two RenderTexture sensors but training env has one).","solutions":["Re-record the .demo demonstration file using the exact environment/scene configuration (same sensors and observations) as the training environment.","Verify the behavior spec of the demo against the policy with a quick script comparing len(observation_specs) before training.","If the extra observation is intentional, either re-record or regenerate the policy's behavior spec so both have the same observation count."],"exampleFix":"// before: training with demos recorded from env with 2 sensors\ntrainer_config = {\"demo_path\": \"old_demo.demo\"}  # demo has 2 obs, policy has 1\n// after: re-record demo in current env\ntrainer_config = {\"demo_path\": \"new_demo.demo\"}  # re-recorded with 1 obs","handlingStrategy":"validation","validationCode":"from mlagents.trainers.demo_loader import load_demonstration\ndemo_spec, _, _ = load_demonstration(\"demos/my_demo.demo\")\nassert len(demo_spec.observation_specs) == len(expected_behavior_spec.observation_specs), (\n    \"demo/policy observation count mismatch\")","typeGuard":"def demo_obs_count_matches(demo_spec, policy_spec):\n    return len(demo_spec.observation_specs) == len(policy_spec.observation_specs)","tryCatchPattern":"try:\n    demo_spec, demo_buffer = demo_to_buffer(path, expected_behavior_spec)\nexcept RuntimeError as e:\n    if \"same number of observations\" in str(e):\n        logger.error(\"Re-record demos: obs count mismatch\")\n        raise SystemExit(1)\n    raise","preventionTips":["Re-record demos whenever you change sensors or observation space in the Unity scene","Pin the same Unity project/build for demo recording and training","Validate demo behavior spec against the env behavior spec in CI before long training runs"],"tags":["ml-agents","demonstrations","observation-mismatch","imitation-learning"],"backgroundTag":"observation-schema-mismatch","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}