{"record":{"id":"a3edc0f34f350288","repo":"keras-team/keras","slug":"in-a-nested-call-argument-you-cannot-mix-tensor","errorCode":null,"errorMessage":"In a nested call() argument, you cannot mix tensors and non-tensors. Received invalid mixed argument: {name}={value}","messagePattern":"In a nested call\\(\\) argument, you cannot mix tensors and non-tensors\\. Received invalid mixed argument: (.+?)=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1938,"sourceCode":"        for name, value in bound_args.arguments.items():\n            arg_dict[name] = value\n            arg_names.append(name)\n            if is_backend_tensor_or_symbolic(value):\n                tensor_args.append(value)\n                tensor_arg_names.append(name)\n                tensor_arg_dict[name] = value\n            elif tree.is_nested(value) and len(value) > 0:\n                flat_values = tree.flatten(value)\n                if all(\n                    is_backend_tensor_or_symbolic(x, allow_none=True)\n                    for x in flat_values\n                ):\n                    tensor_args.append(value)\n                    tensor_arg_names.append(name)\n                    tensor_arg_dict[name] = value\n                    nested_tensor_arg_names.append(name)\n                elif any(is_backend_tensor_or_symbolic(x) for x in flat_values):\n                    raise ValueError(\n                        \"In a nested call() argument, \"\n                        \"you cannot mix tensors and non-tensors. \"\n                        \"Received invalid mixed argument: \"\n                        f\"{name}={value}\"\n                    )\n        self.arguments_dict = arg_dict\n        self.argument_names = arg_names\n        self.tensor_arguments_dict = tensor_arg_dict\n        self.tensor_arguments_names = tensor_arg_names\n        self.nested_tensor_argument_names = nested_tensor_arg_names\n        self.first_arg = arg_dict[arg_names[0]]\n        if all(\n            backend.is_tensor(x) for x in self.tensor_arguments_dict.values()\n        ):\n            self.eager = True\n        else:\n            self.eager = False\n","sourceCodeStart":1920,"sourceCodeEnd":1956,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1920-L1956","documentation":"When Keras inspects a nested (list/tuple/dict-valued) keyword argument to call(), it flattens the values and requires them to be either all tensors (backend or symbolic) or all non-tensors. This ValueError fires when a single nested argument mixes tensors and plain Python values, because Keras cannot decide whether the argument is graph input data or plain configuration.","triggerScenarios":"Calling a layer with a nested argument like layer(x, boxes=[tensor_a, (10, 20)]) or layer(x, anchors={'sizes': some_tensor, 'ratios': [1.0, 2.0]}) where the flattened structure contains both backend/KerasTensors and scalars/tuples.","commonSituations":"Detection layers that take a list of anchor boxes plus learned tensors; passing normalized coordinates alongside tensors; refactoring a flat tensor argument into a mixed config dict.","solutions":["Split the argument into two: one pure-tensor argument (e.g. boxes) and one plain-Python argument (e.g. box_config)","Convert the non-tensor entries into constants of the same backend (e.g. keras.ops.cast / backend constants) so the whole nested value is tensors","Move static configuration into the layer constructor instead of call()"],"exampleFix":"# before\nout = layer(x, anchors={'sizes': sizes_tensor, 'ratios': [1.0, 2.0]})  # ValueError\n\n# after\nlayer = AnchorLayer(ratios=[1.0, 2.0])\nout = layer(x, anchors=sizes_tensor)","handlingStrategy":"validation","validationCode":"flat = keras.tree.flatten(nested_arg)\nis_tensor = lambda v: hasattr(v, 'shape') and hasattr(v, 'dtype')\nflags = [is_tensor(v) for v in flat]\nassert all(flags) or not any(flags), 'mixed tensors and non-tensors in nested arg'","typeGuard":null,"tryCatchPattern":"try:\n    out = layer(x, nested=arg)\nexcept ValueError as e:\n    if 'cannot mix tensors and non-tensors' in str(e):\n        out = layer(x, tensors=tensor_part, config=py_part)","preventionTips":["Keep each nested call() argument homogeneous: all tensors or all plain Python values","Pass static configuration via the constructor","Split mixed structures into separate keyword arguments"],"tags":["keras","tensor","argument-validation","nested-argument"],"backgroundTag":"argument-type-validation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}