{"record":{"id":"3f3d73247c906221","repo":"keras-team/keras","slug":"cannot-infer-argument-num-from-shape-x-shape","errorCode":null,"errorMessage":"Cannot infer argument `num` from shape {x.shape}. Either provide a tensor with a concrete shape in the `axis` dimension or explicitly pass the `num` argument.","messagePattern":"Cannot infer argument `num` from shape (.+?)\\. Either provide a tensor with a concrete shape in the `axis` dimension or explicitly pass the `num` argument\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/core.py","lineNumber":764,"sourceCode":"\n\nclass Unstack(Operation):\n    def __init__(self, num=None, axis=0, *, name=None):\n        super().__init__(name=name)\n        self.num = num\n        self.axis = axis\n\n    def call(self, x):\n        return backend.core.unstack(x, self.num, self.axis)\n\n    def compute_output_spec(self, x):\n        axis = canonicalize_axis(self.axis, len(x.shape))\n        output_shapes = x.shape[:axis] + x.shape[axis + 1 :]\n        num = self.num\n        if num is None:\n            num = x.shape[axis]\n        if num is None:\n            raise ValueError(\n                \"Cannot infer argument `num` from shape \"\n                f\"{x.shape}. Either provide a tensor with a \"\n                \"concrete shape in the `axis` dimension or \"\n                \"explicitly pass the `num` argument.\"\n            )\n        output = [\n            KerasTensor(shape=output_shapes, dtype=x.dtype) for _ in range(num)\n        ]\n        return output\n\n\n@keras_export(\"keras.ops.unstack\")\ndef unstack(x, num=None, axis=0):\n    \"\"\"Unpacks the given dimension of a rank-R tensor into rank-(R-1) tensors.\n\n    Args:\n        x: The input tensor.\n        num: The length of the dimension axis. Automatically inferred","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/core.py#L746-L782","documentation":"For element-count based behavior (e.g. boolean all/any), num defaults to the size of the reduced axis. In symbolic tracing that dimension may be None, so the count cannot be inferred and you must supply num.","triggerScenarios":"Calling an op with num=None on a KerasTensor whose shape[axis] is None inside a functional model or symbolic call","commonSituations":"Using count-based reductions on boolean tensors inside a symbolic (KerasTensor) trace or functional model","solutions":["Pass num explicitly","Run outside symbolic tracing or with concrete shapes","Reshape the tensor so the axis dim is concrete before the reduction"],"exampleFix":"# before\nkeras.ops.all(x)  # x.shape[axis] is None under tracing\n\n# after\nkeras.ops.all(x, num=128)  # or pass a concrete-shaped tensor","handlingStrategy":"validation","validationCode":"if num is None:\n    num = x.shape[axis]\nassert num is not None, 'pass num explicitly when axis dim is dynamic'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass num explicitly for count-based reductions","Test ops inside a concrete (non-symbolic) shape context first"],"tags":["keras","ops","symbolic-tensors","dynamic-shape"],"backgroundTag":"dynamic-shape-not-supported","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}