{"record":{"id":"83ac58d993b64110","repo":"Unity-Technologies/ml-agents","slug":"the-batch-size-and-training-length-requested-for-g","errorCode":null,"errorMessage":"The batch size and training length requested for get_batch where too large given the current number of data points.","messagePattern":"The batch size and training length requested for get_batch where too large given the current number of data points\\.","errorType":"exception","errorClass":"BufferException","httpStatus":null,"severity":"error","filePath":"ml-agents/mlagents/trainers/buffer.py","lineNumber":172,"sourceCode":"        None: only takes one element.\n        :param sequential: If true and training_length is not None: the elements\n        will not repeat in the sequence. [a,b,c,d,e] with training_length = 2 and\n        sequential=True gives [[0,a],[b,c],[d,e]]. If sequential=False gives\n        [[a,b],[b,c],[c,d],[d,e]]\n        \"\"\"\n        if training_length is None:\n            training_length = 1\n        if sequential:\n            # The sequences will not have overlapping elements (this involves padding)\n            leftover = len(self) % training_length\n            # leftover is the number of elements in the first sequence (this sequence might need 0 padding)\n            if batch_size is None:\n                # retrieve the maximum number of elements\n                batch_size = len(self) // training_length + 1 * (leftover != 0)\n            # The maximum number of sequences taken from a list of length len(self) without overlapping\n            # with padding is equal to batch_size\n            if batch_size > (len(self) // training_length + 1 * (leftover != 0)):\n                raise BufferException(\n                    \"The batch size and training length requested for get_batch where\"\n                    \" too large given the current number of data points.\"\n                )\n            if batch_size * training_length > len(self):\n                if self.contains_lists:\n                    padding = []\n                else:\n                    # We want to duplicate the last value in the array, multiplied by the padding_value.\n                    padding = np.array(self[-1], dtype=np.float32) * self.padding_value\n                return self[:] + [padding] * (training_length - leftover)\n\n            else:\n                return self[len(self) - batch_size * training_length :]\n        else:\n            # The sequences will have overlapping elements\n            if batch_size is None:\n                # retrieve the maximum number of elements\n                batch_size = len(self) - training_length + 1","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents/mlagents/trainers/buffer.py#L154-L190","documentation":"AgentBuffer.get_batch for non-overlapping sequences (the first branch) computes the maximum batch size as len(self)//training_length plus leftover; requesting more sequences than the buffer can supply without overlap raises BufferException. This also fires when batch_size is None but training_length exceeds the buffer size.","triggerScenarios":"policy.update / trainer sampling with update_seq_len (training_length) larger than the number of samples in a policy buffer, or an explicit batch_size above len(buffer)//training_length (+leftover), e.g. requesting batch_size from a nearly empty buffer at the start of training.","commonSituations":"Training with sequence length 64+ on small buffers; very first update before enough experience accumulates; buffer cleared between updates; batch_size hyperparameter too large for collected experience.","solutions":["Increase trainer hyperparameters that govern how much experience is collected before update (e.g. buffer_size, add more env steps in the buffer).","Lower update_seq_len / sequence_length so it does not exceed len(buffer).","Lower the requested batch_size to at most len(buffer)//training_length.","Advance env.step()/collect more episodes before calling trainer.update.","Check that the buffer was not cleared/never populated (policy behavior output wired correctly)."],"exampleFix":"// before\nbuffer.get_batch(batch_size=1024, training_length=128)  # buffer has 500 points\n// after\nmax_batch = len(buffer) // 128\nif max_batch > 0:\n    buffer.get_batch(batch_size=min(1024, max_batch), training_length=128)","handlingStrategy":"validation","validationCode":"max_batch = len(buffer) // training_length + 1 * (len(buffer) % training_length != 0)\nassert batch_size is not None and batch_size <= max_batch and len(buffer) > 0, \\\n    f\"buffer too small: have {len(buffer)} points, need {training_length} seq len\"","typeGuard":null,"tryCatchPattern":"from mlagents.trainers.exception import BufferException\ntry:\n    batch = buffer.get_batch(batch_size=batch_size, training_length=training_length)\nexcept BufferException:\n    batch = None  # collect more experience before updating","preventionTips":["Check len(buffer) >= training_length before sampling","Cap batch_size at len(buffer)//training_length","Collect enough steps before the first update","Log buffer length and hyperparameters at each update"],"tags":["training","buffer","rl","hyperparameters"],"backgroundTag":"batch-size-too-large","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}