{"record":{"id":"44f2e47ce5592a77","repo":"keras-team/keras","slug":"array-inputs-to-associative-scan-must-have-the-sam-44f2e4","errorCode":null,"errorMessage":"Array inputs to associative_scan must have the same first dimension. (saw: {})","messagePattern":"Array inputs to associative_scan must have the same first dimension\\. \\(saw: (.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/core.py","lineNumber":210,"sourceCode":"    )\n\n\nclass AssociativeScan(Operation):\n    def __init__(self, reverse=False, axis=0, *, name=None):\n        super().__init__(name=name)\n        self.reverse = reverse\n        self.axis = axis\n\n    def call(self, f, elems):\n        return backend.core.associative_scan(\n            f, elems, reverse=self.reverse, axis=self.axis\n        )\n\n    def compute_output_spec(self, f, elems):\n        elems_flat = tree.flatten(elems)\n        lens = [elem.shape[self.axis] for elem in elems_flat]\n        if len(set(lens)) != 1:\n            raise ValueError(\n                \"Array inputs to associative_scan must have the same \"\n                \"first dimension. (saw: {})\".format(\n                    [elem.shape for elem in elems_flat]\n                )\n            )\n\n        x = tree.pack_sequence_as(\n            elems,\n            [slice_along_axis(x, 0, 1, axis=self.axis) for x in elems_flat],\n        )\n        y_spec = backend.compute_output_spec(f, x, x)\n\n        def _restore_shape(x):\n            return KerasTensor(\n                shape=elems_flat[0].shape, dtype=x.dtype, sparse=x.sparse\n            )\n\n        y_spec = tree.map_structure(_restore_shape, y_spec)","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/core.py#L192-L228","documentation":"associative_scan folds a function over parallel arrays and requires every element in the (possibly nested) elems structure to share the same leading dimension (the scan length). Mismatched leading dims make the scan ill-defined.","triggerScenarios":"keras.ops.associative_scan(f, (a, b)) where a.shape[0] != b.shape[0]","commonSituations":"JAX-style functional loops ported to Keras 3 ops, sequence models with padded batches","solutions":["Slice or pad the arrays so all leading dims match","Fix the data pipeline that produced inconsistent lengths","If using tuples of (elems, init), verify init is not accidentally included in the scan structure"],"exampleFix":"# before\nkeras.ops.associative_scan(fn, (xs, ys))  # len(xs) != len(ys)\n\n# after\nn = min(len(xs), len(ys))\nkeras.ops.associative_scan(fn, (xs[:n], ys[:n]))","handlingStrategy":"validation","validationCode":"lens = {e.shape[0] for e in tree.flatten(elems)}\nassert len(lens) == 1, f'mismatched leading dims: {lens}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pad or slice inputs to a common leading dimension before scan","Check leading-dim equality in data pipelines feeding associative_scan"],"tags":["keras","ops","associative-scan","shape"],"backgroundTag":"shape-mismatch-between-tensors","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}