{"record":{"id":"d6a432b37c62f898","repo":"keras-team/keras","slug":"true-fn-and-false-fn-should-return-outputs-of","errorCode":null,"errorMessage":"`true_fn` and `false_fn` should return outputs of the same kind (struct, dtype and shape). Got {true_fn_spec} and {false_fn_spec} instead.","messagePattern":"`true_fn` and `false_fn` should return outputs of the same kind \\(struct, dtype and shape\\)\\. Got (.+?) and (.+?) instead\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/core.py","lineNumber":1106,"sourceCode":"                        e,\n                        self.call,\n                        args,\n                        kwargs,\n                        object_name=(f\"{self.__class__.__name__}.call()\"),\n                    ) from None\n                raise\n\n        # Plain flow.\n        return call_fn(*args, **kwargs)\n\n    def call(self, pred, true_fn, false_fn):\n        return backend.core.cond(pred, true_fn, false_fn)\n\n    def compute_output_spec(self, pred, true_fn, false_fn):\n        true_fn_spec = backend.compute_output_spec(true_fn)\n        false_fn_spec = backend.compute_output_spec(false_fn)\n        if not self._check_output_spec(true_fn_spec, false_fn_spec):\n            raise ValueError(\n                \"`true_fn` and `false_fn` should return outputs \"\n                \"of the same kind (struct, dtype and shape). \"\n                f\"Got {true_fn_spec} and {false_fn_spec} instead.\"\n            )\n        return true_fn_spec\n\n    def _check_output_spec(self, true_fn_spec, false_fn_spec):\n        try:\n            tree.assert_same_structure(true_fn_spec, false_fn_spec)\n        except:\n            return False\n\n        def check_leaf(t_spec, f_spec):\n            if t_spec is None or f_spec is None:\n                return t_spec is None and f_spec is None\n            return t_spec.shape == f_spec.shape and t_spec.dtype == f_spec.dtype\n\n        same = tree.map_structure(check_leaf, true_fn_spec, false_fn_spec)","sourceCodeStart":1088,"sourceCodeEnd":1124,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/core.py#L1088-L1124","documentation":"cond requires both branches to be interchangeable: identical pytree structure, dtypes and shapes. Keras compares the computed output specs of true_fn and false_fn and rejects mismatches because backends compile both paths.","triggerScenarios":"keras.ops.cond(pred, lambda: x, lambda: keras.ops.cast(x, 'float16')) or branches returning (x,) vs (x, y)","commonSituations":"Data-dependent branching with jitted backends (JAX), quantization-aware branches, or dtype-mixed training/eval paths","solutions":["Make both branches return the same structure, dtype, and rank","Cast outputs in one branch to match the other (keras.ops.cast)","Return tuples of the same arity from both branches"],"exampleFix":"# before\nkeras.ops.cond(p, lambda: x, lambda: (x, x))\n\n# after\nkeras.ops.cond(p, lambda: (x,), lambda: (x,))","handlingStrategy":"validation","validationCode":"ts = keras.ops.compute_output_spec(true_fn)\nfs = keras.ops.compute_output_spec(false_fn)\nassert str(ts) == str(fs), f'{ts} vs {fs}'","typeGuard":null,"tryCatchPattern":"try:\n    keras.ops.cond(p, tfn, ffn)\nexcept ValueError:\n    fix ffn to match tfn output spec","preventionTips":["Ensure both branches of cond return the same structure, dtype, and shape","Return a tuple from both branches when multiple outputs are needed"],"tags":["keras","ops","cond","shape-mismatch"],"backgroundTag":"branch-output-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}