{"record":{"id":"3a925aa794ae0097","repo":"Unity-Technologies/ml-agents","slug":"the-length-of-the-fields-key-list-were-not-of-sa","errorCode":null,"errorMessage":"The length of the fields {key_list} were not of same length","messagePattern":"The length of the fields (.+?) were not of same length","errorType":"exception","errorClass":"BufferException","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/buffer.py","lineNumber":499,"sourceCode":"        self,\n        target_buffer: \"AgentBuffer\",\n        key_list: List[AgentBufferKey] = None,\n        batch_size: int = None,\n        training_length: int = None,\n    ) -> None:\n        \"\"\"\n        Takes in a batch size and training length (sequence length), and appends this AgentBuffer to target_buffer\n        properly padded for LSTM use. Optionally, use key_list to restrict which fields are inserted into the new\n        buffer.\n        :param target_buffer: The buffer which to append the samples to.\n        :param key_list: The fields that must be added. If None: all fields will be appended.\n        :param batch_size: The number of elements that must be appended. If None: All of them will be.\n        :param training_length: The length of the samples that must be appended. If None: only takes one element.\n        \"\"\"\n        if key_list is None:\n            key_list = list(self.keys())\n        if not self.check_length(key_list):\n            raise BufferException(\n                f\"The length of the fields {key_list} were not of same length\"\n            )\n        for field_key in key_list:\n            target_buffer[field_key].extend(\n                self[field_key].get_batch(\n                    batch_size=batch_size, training_length=training_length\n                )\n            )\n\n    @property\n    def num_experiences(self) -> int:\n        \"\"\"\n        The number of agent experiences in the AgentBuffer, i.e. the length of the buffer.\n\n        An experience consists of one element across all of the fields of this AgentBuffer.\n        Note that these all have to be the same length, otherwise shuffle and append_to_update_buffer\n        will fail.\n        \"\"\"","sourceCodeStart":481,"sourceCodeEnd":517,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/buffer.py#L481-L517","documentation":"AgentBuffer.resequence_and_append copies resequenced samples into a target update buffer, but only if all source fields have equal length (rows represent aligned samples). check_length failing raises this BufferException naming the offending key_list.","triggerScenarios":"Calling resequence_and_append (usually via _append_to_update_buffer / process_batch) when the sample buffer fields have different lengths — e.g. some fields got fewer appends during collection.","commonSituations":"PPO/SAC training where a policy or reward signal skipped writes for some steps, heterogeneous agent counts per field, or custom trainers appending selectively.","solutions":["Ensure the collection code appends to all fields consistently before each update","Filter key_list down to fields of equal length, or pad/trim fields to match before appending","Compare len(self[k]) for each k in key_list to find the short field and fix its producer"],"exampleFix":"// before\nbuffer.resequence_and_append(target_buffer, training_length=seq_len)  # uneven fields\n// after\nkey_list = list(buffer.keys())\nif not buffer.check_length(key_list):\n    key_list = [k for k in key_list if len(buffer[k]) == min(len(buffer[k2]) for k2 in key_list)]\nbuffer.resequence_and_append(target_buffer, training_length=seq_len, key_list=key_list)","handlingStrategy":"validation","validationCode":"key_list = key_list or list(buffer.keys())\nif not buffer.check_length(key_list):\n    lengths = {str(k): len(buffer[k]) for k in key_list}\n    raise ValueError(f\"Fields not aligned before resequence_and_append: {lengths}\")","typeGuard":null,"tryCatchPattern":"try:\n    buffer.resequence_and_append(target_buffer, training_length=seq_len)\nexcept BufferException as e:\n    logger.error(f\"Update skipped, misaligned buffer fields: {e}\")","preventionTips":["Run check_length on update_buffer's incoming keys before each trainer update","Audit custom collectors for conditional appends that skip some fields","Log per-field lengths during debugging to spot the short field early"],"tags":["bufferexception","ml-agents","buffer","data-integrity"],"backgroundTag":"buffer-field-length-mismatch","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}