{"record":{"id":"863011a22557f4e7","repo":"Unity-Technologies/ml-agents","slug":"shape-and-dimensionproperties-must-have-the-same-l","errorCode":null,"errorMessage":"shape and dimensionProperties must have the same length.","messagePattern":"shape and dimensionProperties must have the same length\\.","errorType":"validation","errorClass":"UnityAgentsException","httpStatus":null,"severity":"error","filePath":"com.unity.ml-agents/Runtime/Sensors/ObservationSpec.cs","lineNumber":131,"sourceCode":"        /// Create a general ObservationSpec from the shape, dimension properties, and observation type.\n        /// </summary>\n        /// <remarks>\n        /// Note that not all combinations of DimensionProperty may be supported by the trainer.\n        /// shape and dimensionProperties must have the same size.\n        /// </remarks>\n        /// <param name=\"shape\">Shape</param>\n        /// <param name=\"dimensionProperties\">Dimension properties</param>\n        /// <param name=\"observationType\">Observation type</param>\n        /// <exception cref=\"UnityAgentsException\">Unity agents exception</exception>\n        public ObservationSpec(\n            InplaceArray<int> shape,\n            InplaceArray<DimensionProperty> dimensionProperties,\n            ObservationType observationType = ObservationType.Default\n        )\n        {\n            if (shape.Length != dimensionProperties.Length)\n            {\n                throw new UnityAgentsException(\"shape and dimensionProperties must have the same length.\");\n            }\n            m_Shape = shape;\n            m_DimensionProperties = dimensionProperties;\n            m_ObservationType = observationType;\n        }\n    }\n}\n","sourceCodeStart":113,"sourceCodeEnd":139,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/com.unity.ml-agents/Runtime/Sensors/ObservationSpec.cs#L113-L139","documentation":"The private ObservationSpec constructor requires the shape array and the dimensionProperties array to describe the same tensor, so their lengths must match. A length mismatch means one array was updated without the other, and the spec would be internally inconsistent, so it throws immediately.","triggerScenarios":"Calling ObservationSpec.MakeSpec(shape, dimensionProperties) with arrays of different lengths; hand-building a spec where shape has 3 entries but dimensionProperties has 2 (or vice versa).","commonSituations":"Custom sensor authors extending ObservationSpec with per-dimension properties; editing shape rank (e.g. adding a batch or time dimension) without extending the properties array; copy-pasted spec code with mismatched edits.","solutions":["Pass dimensionProperties with exactly the same number of entries as shape, one DimensionProperty per dimension","Use a helper like InplaceArray.Empty<DimensionProperty>() only when shape is also empty; otherwise fill entries (e.g. DimensionProperty.NoneSI) for each dimension","Prefer the built-in factory methods (ObservationSpec.Vector, .Visual, .Default) instead of constructing specs manually","Add a debug check on array lengths at your sensor construction site"],"exampleFix":"// before\nObservationSpec.MakeSpec(new[] { 3, 4 }, new[] { DimensionProperty.NoneSI });\n// after\nObservationSpec.MakeSpec(new[] { 3, 4 }, new[] { DimensionProperty.NoneSI, DimensionProperty.NoneSI });","handlingStrategy":"validation","validationCode":"// Unity C#: before building the spec\nif (shape.Length != dimensionProperties.Length)\n{\n    Debug.LogError($\"shape ({shape.Length}) and dimensionProperties ({dimensionProperties.Length}) must match\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var spec = ObservationSpec.MakeSpec(shape, dimensionProperties);\n}\ncatch (UnityAgentsException e) when (e.Message.Contains(\"same length\"))\n{\n    Debug.LogError(\"Spec arrays mismatched: \" + e.Message);\n}","preventionTips":["Prefer ObservationSpec.Vector/Visual factories over manual construction","Keep shape and dimensionProperties edits paired — change both or neither","Build dimensionProperties from shape.Length so they can never diverge"],"tags":["unity","ml-agents","observation-spec","shape-mismatch","api-misuse"],"backgroundTag":"array-length-mismatch","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}