{"record":{"id":"67c6ee51d229c036","repo":"Unity-Technologies/ml-agents","slug":"the-shape-demo-obs-for-observation-i-in-demons","errorCode":null,"errorMessage":"The shape {demo_obs} for observation {i} in demonstration                         do not match the policy's {policy_obs}.","messagePattern":"The shape (.+?) for observation (.+?) in demonstration                         do not match the policy's (.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/demo_loader.py","lineNumber":139,"sourceCode":"                    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]:\n    \"\"\"\n    Retrieves the demonstration file(s) from a path.\n    :param path: Path of demonstration file or directory.\n    :return: List of demonstration files\n\n    Raises errors if |path| is invalid.\n    \"\"\"\n    if os.path.isfile(path):\n        if not path.endswith(\".demo\"):\n            raise ValueError(\"The path provided is not a '.demo' file.\")\n        return [path]","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/demo_loader.py#L121-L157","documentation":"When the number of observations matches, demo_to_buffer compares each observation spec's shape element-wise. This RuntimeError is thrown when demonstration observation i has a different shape (e.g. camera resolution or vector observation size) than the policy's corresponding observation. It prevents training on dimensionally incompatible demo data.","triggerScenarios":"demo_to_buffer called with a demo whose observation spec shape differs from expected_behavior_spec's, e.g. demo recorded at 84x84 resolution while the training env uses 64x64, or vector observation size changed in the Brain/Behavior Parameters.","commonSituations":"Changing camera resolution or grayscale setting in Unity after recording demos; changing vector observation space size in Behavior Parameters; using a demo file from an older version of the project with different observation dimensions.","solutions":["Re-record the demonstration file with the observation shapes (resolution, vector size) matching the current training environment.","Align the training environment's observation shapes to the demo's shapes (e.g. set camera resolution to 84x84 in Behavior Parameters).","Inspect the demo's BrainParametersProto offline (load_demonstration returns behavior_spec) and diff shapes against BehaviorSpec before training."],"exampleFix":"// before\nbehavior_parameters: camera_resolution: 64  # demo recorded at 84\n// after\nbehavior_parameters: camera_resolution: 84  # matches demo recording","handlingStrategy":"validation","validationCode":"demo_spec, _, _ = load_demonstration(\"demos/my_demo.demo\")\nfor i, (d, p) in enumerate(zip(demo_spec.observation_specs, expected_behavior_spec.observation_specs)):\n    assert d.shape == p.shape, f\"obs {i}: demo {d.shape} != policy {p.shape}\"","typeGuard":"def demo_obs_shapes_match(demo_spec, policy_spec):\n    if len(demo_spec.observation_specs) != len(policy_spec.observation_specs):\n        return False\n    return all(d.shape == p.shape for d, p in zip(demo_spec.observation_specs, policy_spec.observation_specs))","tryCatchPattern":"try:\n    demo_spec, demo_buffer = demo_to_buffer(path, expected_behavior_spec)\nexcept RuntimeError as e:\n    if \"do not match the policy's\" in str(e):\n        logger.error(\"Align camera resolution / vector obs size between demo and env\")\n        raise SystemExit(1)\n    raise","preventionTips":["Keep Behavior Parameters (camera resolution, grayscale, vector obs size) unchanged after recording demos","Store demos alongside the config that produced them","Diff demo vs policy observation shapes before starting imitation training"],"tags":["ml-agents","demonstrations","shape-mismatch","imitation-learning"],"backgroundTag":"observation-shape-mismatch","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}