{"record":{"id":"6807f06e9f899921","repo":"keras-team/keras","slug":"cannot-convert-shape-to-a-shape-found-invalid","errorCode":null,"errorMessage":"Cannot convert '{shape}' to a shape. Found invalid dimension '{d}' of type '{type(d)}'. ","messagePattern":"Cannot convert '(.+?)' to a shape\\. Found invalid dimension '(.+?)' of type '(.+?)'\\. ","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/variables.py","lineNumber":627,"sourceCode":"        )\n\n    if config.backend() == \"torch\":\n        # Replace symbolic dimensions with None to preserve dynamic shapes\n        # during torch.export tracing\n        import torch\n\n        shape = tuple(None if isinstance(d, torch.SymInt) else d for d in shape)\n\n    # Handle dimensions that are not ints and not None, verify they're >= 0.\n    standardized_shape = []\n    for d in shape:\n        if d is None:\n            standardized_shape.append(d)\n            continue\n\n        # Reject these even if they can be cast to int successfully.\n        if isinstance(d, (str, float)):\n            raise ValueError(\n                f\"Cannot convert '{shape}' to a shape. \"\n                f\"Found invalid dimension '{d}' of type '{type(d)}'. \"\n            )\n\n        try:\n            # Cast numpy scalars, tf constant tensors, etc.\n            d = int(d)\n        except Exception as e:\n            raise ValueError(\n                f\"Cannot convert '{shape}' to a shape. \"\n                f\"Found invalid dimension '{d}' of type '{type(d)}'. \"\n            ) from e\n        if d < 0:\n            raise ValueError(\n                f\"Cannot convert '{shape}' to a shape. \"\n                \"Negative dimensions are not allowed.\"\n            )\n        standardized_shape.append(d)","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/variables.py#L609-L645","documentation":"Raised by standardize_shape while iterating over the shape's dimensions: a dimension is a str or float (rejected on purpose even though int() would succeed, to avoid silently truncating 3.5 to 3), or is not int-castable at all. The offending dimension and its type are named in the message.","triggerScenarios":"Thrown at keras/src/backend/common/variables.py:627 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use integer dimensions: replace '128' with 128 and 128.0 with 128.","Cast dimensions explicitly before building the shape: tuple(int(d) for d in shape).","Use None only for genuinely dynamic dims; never placeholder strings or floats."],"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"}