{"record":{"id":"311be206acf7632d","repo":"keras-team/keras","slug":"iterating-over-a-symbolic-kerastensor-is-not-suppo","errorCode":null,"errorMessage":"Iterating over a symbolic KerasTensor is not supported.","messagePattern":"Iterating over a symbolic KerasTensor is not supported\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/keras_tensor.py","lineNumber":223,"sourceCode":"            \"tf_fn(x)  # Invalid.\\n\"\n            \"```\\n\\n\"\n            \"What you should do instead is wrap `tf_fn` in a layer:\\n\\n\"\n            \"```\\n\"\n            \"class MyLayer(Layer):\\n\"\n            \"    def call(self, x):\\n\"\n            \"        return tf_fn(x)\\n\\n\"\n            \"x = MyLayer()(x)\\n\"\n            \"```\\n\"\n        )\n\n    def __repr__(self):\n        return (\n            f\"<KerasTensor shape={self.shape}, dtype={self.dtype}, \"\n            f\"sparse={self.sparse}, ragged={self.ragged}, name={self.name}>\"\n        )\n\n    def __iter__(self):\n        raise NotImplementedError(\n            \"Iterating over a symbolic KerasTensor is not supported.\"\n        )\n\n    def __bool__(self):\n        raise TypeError(\"A symbolic KerasTensor cannot be used as a boolean.\")\n\n    def __add__(self, other):\n        from keras.src import ops\n\n        return ops.Add().symbolic_call(self, other)\n\n    def __radd__(self, other):\n        from keras.src import ops\n\n        return ops.Add().symbolic_call(other, self)\n\n    def __sub__(self, other):\n        from keras.src import ops","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/keras_tensor.py#L205-L241","documentation":"Raised from KerasTensor.__iter__ when Python iterates a KerasTensor (for-loops, unpacking, iter()/list() calls). DuringFunctional-model tracing iteration over a symbolic tensor is ambiguous — there is no data to enumerate — so the dunder deliberately disables it to surface graph-construction mistakes.","triggerScenarios":"Thrown at keras/src/backend/common/keras_tensor.py:223 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use keras.ops ops (e.g. unstack/stack/split) to decompose the tensor along an axis inside the graph","Move the per-element Python logic into a layer or vectorize it with ops like vectorized_map","Iterate over concrete tensors after running the model, not over the symbolic tensor"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}