{"record":{"id":"e9a8b36e7e5c2871","repo":"keras-team/keras","slug":"adapt-expects-an-iterable-that-yields-arrays-or","errorCode":null,"errorMessage":"adapt() expects an iterable that yields arrays or tensors with a `.shape` attribute (e.g. numpy arrays or backend tensors). Got an element of type {type(first_batch).__name__}. Ensure each yielded element is array-like with a `.shape` attribute.","messagePattern":"adapt\\(\\) expects an iterable that yields arrays or tensors with a `\\.shape` attribute \\(e\\.g\\. numpy arrays or backend tensors\\)\\. Got an element of type (.+?)\\. Ensure each yielded element is array-like with a `\\.shape` attribute\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/normalization.py","lineNumber":310,"sourceCode":"        elif isinstance(data, PyDataset):\n            input_shape = _extract_batch(data[0]).shape\n        elif hasattr(data, \"__iter__\"):\n            data_is_iterable = True\n            # Consume first batch to infer input_shape; then chain it back for\n            # accumulation so we iterate over (first_batch, *rest).\n            data_iter = iter(data)\n            first_batch = next(data_iter, None)\n            if first_batch is None:\n                raise ValueError(\n                    \"adapt() received an empty iterable (no batches). \"\n                    \"Expected at least one batch. Pass a non-empty iterable \"\n                    \"of arrays or tensors, e.g. layer.adapt([x]) or \"\n                    \"layer.adapt(list_of_batches).\"\n                )\n            first_batch = _extract_batch(first_batch)\n            input_shape = getattr(first_batch, \"shape\", None)\n            if input_shape is None:\n                raise TypeError(\n                    \"adapt() expects an iterable that yields arrays or \"\n                    \"tensors with a `.shape` attribute (e.g. numpy arrays or \"\n                    \"backend tensors). Got an element of type \"\n                    f\"{type(first_batch).__name__}. Ensure each yielded \"\n                    \"element is array-like with a `.shape` attribute.\"\n                )\n            input_shape = tuple(input_shape)\n            data = itertools.chain([first_batch], data_iter)\n        else:\n            raise TypeError(\n                f\"Unsupported data type: {type(data)}. `adapt` supports \"\n                f\"`np.ndarray`, backend tensors, `tf.data.Dataset`, \"\n                f\"`keras.utils.PyDataset`, and iterables of batches (e.g. \"\n                f\"list, generator).\"\n            )\n\n        if not self.built:\n            self.build(input_shape)","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/normalization.py#L292-L328","documentation":"adapt() on a generic iterable infers the input shape from the first yielded element's .shape attribute. If the element lacks .shape (plain list, tuple, scalar, dict), a TypeError is raised naming the offending type.","triggerScenarios":"layer.adapt([[1,2],[3,4]]) where elements are plain Python lists; adapt(zip(a,b)); adapting an iterable of scalars.","commonSituations":"Notebook prototypes passing raw nested lists; forgetting to convert a list-of-lists to np.ndarray; adapting over zipped columns.","solutions":["Convert to numpy first: layer.adapt(np.array(data))","Convert each element: layer.adapt([np.asarray(b) for b in batches])","For raw arrays pass the array/tensor directly, not wrapped in an iterable"],"exampleFix":"// before\nlayer.adapt([[1.0,2.0],[3.0,4.0]])\n// after\nimport numpy as np\nlayer.adapt(np.array([[1.0,2.0],[3.0,4.0]]))","handlingStrategy":"type-guard","validationCode":"first = next(iter(data))\nif not hasattr(first, 'shape'):\n    data = [np.asarray(b) for b in data]","typeGuard":"def batch_is_arraylike(b): return hasattr(b, 'shape')","tryCatchPattern":null,"preventionTips":["Convert list-of-lists to np.ndarray before adapt","Pass arrays/tensors directly"],"tags":["keras","normalization","adapt","type-validation"],"backgroundTag":"unsupported-input-type","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}