{"record":{"id":"941acfab2a41ebf3","repo":"keras-team/keras","slug":"please-initialize-timedistributed-layer-with-a","errorCode":null,"errorMessage":"Please initialize `TimeDistributed` layer with a `keras.layers.Layer` instance. Received: {layer}","messagePattern":"Please initialize `TimeDistributed` layer with a `keras\\.layers\\.Layer` instance\\. Received: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/rnn/time_distributed.py","lineNumber":49,"sourceCode":"    the timestamps, the same set of weights are used at each timestamp.\n\n    Args:\n        layer: a `keras.layers.Layer` instance.\n\n    Call arguments:\n        inputs: Input tensor of shape (batch, time, ...) or nested tensors,\n            and each of which has shape (batch, time, ...).\n        training: Python boolean indicating whether the layer should behave in\n            training mode or in inference mode. This argument is passed to the\n            wrapped layer (only if the layer supports this argument).\n        mask: Binary tensor of shape `(samples, timesteps)` indicating whether\n            a given timestep should be masked. This argument is passed to the\n            wrapped layer (only if the layer supports this argument).\n    \"\"\"\n\n    def __init__(self, layer, **kwargs):\n        if not isinstance(layer, Layer):\n            raise ValueError(\n                \"Please initialize `TimeDistributed` layer with a \"\n                f\"`keras.layers.Layer` instance. Received: {layer}\"\n            )\n        super().__init__(layer, **kwargs)\n        self.supports_masking = False\n\n    def _get_child_input_shape(self, input_shape):\n        if not isinstance(input_shape, (tuple, list)) or len(input_shape) < 3:\n            raise ValueError(\n                \"`TimeDistributed` Layer should be passed an `input_shape` \"\n                f\"with at least 3 dimensions, received: {input_shape}\"\n            )\n        return (input_shape[0], *input_shape[2:])\n\n    def compute_output_shape(self, input_shape):\n        child_input_shape = self._get_child_input_shape(input_shape)\n        child_output_shape = self.layer.compute_output_shape(child_input_shape)\n        return (child_output_shape[0], input_shape[1], *child_output_shape[1:])","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/rnn/time_distributed.py#L31-L67","documentation":"TimeDistributed wraps a single Keras Layer and applies it independently at every timestep. Its constructor enforces `isinstance(layer, Layer)` so that it can delegate build/call and output-shape computation; anything else (a string name, a function, a model config) is rejected immediately.","triggerScenarios":"Calling keras.layers.TimeDistributed('Dense') or TimeDistributed(some_python_function) or passing a dict config instead of a layer instance; also nesting wrappers incorrectly such as TimeDistributed(TimeDistributed(dense)) where an unexpected object slips through.","commonSituations":"Porting old Keras 1.x code where layers could be referenced by name; passing a lambda or a backend function instead of a keras.layers wrapper; JSON-deserializing a model config without using keras.layers.deserialize first.","solutions":["Pass an instantiated layer: TimeDistributed(keras.layers.Dense(10))","Wrap raw functions in a Lambda layer first: TimeDistributed(keras.layers.Lambda(fn))","When loading from config, deserialize to a Layer object before wrapping"],"exampleFix":"# before\nlayer = keras.layers.TimeDistributed('Dense')\n\n# after\nlayer = keras.layers.TimeDistributed(keras.layers.Dense(10))","handlingStrategy":"type-guard","validationCode":"from keras.layers import Layer\nassert isinstance(inner, Layer), f'TimeDistributed needs a Layer, got {type(inner)}'\ntd = keras.layers.TimeDistributed(inner)","typeGuard":"from keras.layers import Layer\n\ndef is_keras_layer(obj) -> bool:\n    return isinstance(obj, Layer)","tryCatchPattern":null,"preventionTips":["Never reference layers by string name in Keras 3","Wrap raw functions in keras.layers.Lambda before TimeDistributed"],"tags":["keras","time-distributed","type-validation","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}