{"record":{"id":"eb32c004fcab8f04","repo":"Unity-Technologies/ml-agents","slug":"the-buffersensor-was-expecting-an-observation-of-s","errorCode":null,"errorMessage":"The BufferSensor was expecting an observation of size {m_ObsSize} but received {obs.Length} observations instead.","messagePattern":"The BufferSensor was expecting an observation of size (.+?) but received (.+?) observations instead\\.","errorType":"exception","errorClass":"UnityAgentsException","httpStatus":null,"severity":"error","filePath":"com.unity.ml-agents/Runtime/Sensors/BufferSensor.cs","lineNumber":51,"sourceCode":"\n        /// <inheritdoc/>\n        public ObservationSpec GetObservationSpec()\n        {\n            return m_ObservationSpec;\n        }\n\n        /// <summary>\n        /// Appends an observation to the buffer. If the buffer is full (maximum number\n        /// of observation is reached) the observation will be ignored. the length of\n        /// the provided observation array must be equal to the observation size of\n        /// the buffer sensor.\n        /// </summary>\n        /// <param name=\"obs\"> The float array observation</param>\n        public void AppendObservation(float[] obs)\n        {\n            if (obs.Length != m_ObsSize)\n            {\n                throw new UnityAgentsException(\n                    \"The BufferSensor was expecting an observation of size \" +\n                    $\"{m_ObsSize} but received {obs.Length} observations instead.\"\n                );\n            }\n            if (m_CurrentNumObservables >= m_MaxNumObs)\n            {\n                return;\n            }\n            for (int i = 0; i < obs.Length; i++)\n            {\n                m_ObservationBuffer[m_CurrentNumObservables * m_ObsSize + i] = obs[i];\n            }\n            m_CurrentNumObservables++;\n        }\n\n        /// <inheritdoc/>\n        public int Write(ObservationWriter writer)\n        {","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/com.unity.ml-agents/Runtime/Sensors/BufferSensor.cs#L33-L69","documentation":"BufferSensor.AppendObservation() validates that every appended observation vector has exactly the size declared when the BufferSensor was created. A mismatched length would corrupt the fixed-size buffer tensor fed to the trainer. This is thrown immediately when a vector of the wrong length is appended.","triggerScenarios":"Creating BufferSensorComponent with a fixed buffer size then appending float arrays whose Length differs from that size (m_ObsSize); changing the number of features per entity (e.g. one-hot tag size or added stats) without resizing the buffer; appending observations for differently-configured entities in the same buffer.","commonSituations":"Adding a new field to an entity's observation vector after the buffer sensor size was set in the inspector; sharing one BufferSensorComponent across prefab variants with different entity feature counts; off-by-one when building the float array manually.","solutions":["Ensure every float[] passed to AppendObservation() has exactly the same length as the buffer sensor's observation size setting","Update the BufferSensorComponent's 'Observation Size' field in the inspector to match the new vector length","Construct the per-entity float array with a constant/asserted length so all entities share the same feature count","Add a debug assert (obs.Length == expected) in your observation-building helper to catch drift early"],"exampleFix":"// before\nbufferSensorComponent.CreateBufferSensor(10, \"myBuffer\");\nbufferSensor.AppendObservation(new float[8]); // wrong length\n// after\nbufferSensorComponent.CreateBufferSensor(8, \"myBuffer\"); // size matches\nbufferSensor.AppendObservation(new float[8]);","handlingStrategy":"validation","validationCode":"// Unity C#: before AppendObservation\nif (obs.Length != expectedBufferSize)\n{\n    Debug.LogError($\"BufferSensor expects {expectedBufferSize} floats, got {obs.Length}\");\n    return;\n}\nbufferSensor.AppendObservation(obs);","typeGuard":null,"tryCatchPattern":"try\n{\n    bufferSensor.AppendObservation(obs);\n}\ncatch (UnityAgentsException e) when (e.Message.Contains(\"BufferSensor was expecting\"))\n{\n    Debug.LogError($\"Entity observation size mismatch: {e.Message}\");\n}","preventionTips":["Keep the buffer sensor's observation size constant across all entities sharing it","Define the per-entity vector length as a const and build arrays from it","Update the inspector 'Observation Size' whenever entity features change"],"tags":["unity","ml-agents","sensors","buffer-sensor","shape-mismatch"],"backgroundTag":"observation-size-mismatch","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}