{"record":{"id":"9f82447984ded96d","repo":"langchain-ai/langchain","slug":"runnablebranch-default-must-be-runnable-callable","errorCode":null,"errorMessage":"RunnableBranch default must be Runnable, callable or mapping.","messagePattern":"RunnableBranch default must be Runnable, callable or mapping\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/branch.py","lineNumber":108,"sourceCode":"\n        Raises:\n            ValueError: If the number of branches is less than `2`.\n            TypeError: If the default branch is not `Runnable`, `Callable` or `Mapping`.\n            TypeError: If a branch is not a `tuple` or `list`.\n            ValueError: If a branch is not of length `2`.\n        \"\"\"\n        if len(branches) < _MIN_BRANCHES:\n            msg = \"RunnableBranch requires at least two branches\"\n            raise ValueError(msg)\n\n        default = branches[-1]\n\n        if not isinstance(\n            default,\n            (Runnable, Callable, Mapping),  # type: ignore[arg-type]\n        ):\n            msg = \"RunnableBranch default must be Runnable, callable or mapping.\"\n            raise TypeError(msg)\n\n        default_ = coerce_to_runnable(cast(\"Runnable[Input, Output]\", default))\n\n        branches_ = []\n\n        for branch in branches[:-1]:\n            if not isinstance(branch, (tuple, list)):\n                msg = (\n                    f\"RunnableBranch branches must be \"\n                    f\"tuples or lists, not {type(branch)}\"\n                )\n                raise TypeError(msg)\n\n            if len(branch) != _MIN_BRANCHES:\n                msg = (\n                    f\"RunnableBranch branches must be \"\n                    f\"tuples or lists of length 2, not {len(branch)}\"\n                )","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/branch.py#L90-L126","documentation":"In `RunnableBranch.__init__`, the last positional argument is the default branch and must be a `Runnable`, a `Callable`, or a `Mapping` so it can be coerced via `coerce_to_runnable`. Anything else (str, int, None, list, tuple) fails this `isinstance` check and raises `TypeError`.","triggerScenarios":"Ending the `RunnableBranch(...)` argument list with a non-runnable value: `RunnableBranch((cond, fn), \"default\")`, `RunnableBranch((cond, fn), None)`, or `RunnableBranch((cond, fn), [fn1, fn2])` (a list, not a Mapping/Runnable).","commonSituations":"Using a literal string as the fallback answer; passing a list of runnables expecting them to run in sequence instead of a `RunnableSequence`; a variable that is None because an optional handler was not configured; forgetting that the final argument is the default, not another condition pair.","solutions":["Wrap the final value: `RunnableBranch((cond, fn), RunnableLambda(lambda _: \"default\"))`.","If the default is a sequence of steps, use a `RunnableSequence`/pipe chain: `(prompt | model | parser)`.","If the default should be a set of parallel steps, pass a dict (Mapping) of runnables."],"exampleFix":"# before\nbranch = RunnableBranch((cond, fn), \"fallback\")\n\n# after\nfrom langchain_core.runnables import RunnableLambda\nbranch = RunnableBranch((cond, fn), RunnableLambda(lambda _: \"fallback\"))","handlingStrategy":"type-guard","validationCode":"from collections.abc import Callable, Mapping\nfrom langchain_core.runnables import Runnable\n\nok = isinstance(default, (Runnable, Callable, Mapping))\nassert ok, f\"default must be Runnable/callable/mapping, got {type(default)}\"","typeGuard":"from collections.abc import Callable, Mapping\nfrom typing import TypeGuard\nfrom langchain_core.runnables import Runnable\n\ndef is_valid_default(d: object) -> TypeGuard[Runnable | Callable | Mapping]:\n    return isinstance(d, (Runnable, Callable, Mapping))","tryCatchPattern":null,"preventionTips":["Always end RunnableBranch(...) with a runnable or function, never a literal.","Wrap constants with RunnableLambda(lambda _: value).","For multi-step defaults, use a pipe chain (RunnableSequence), not a list."],"tags":["runnable-branch","type-error","chain-composition","langchain-core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}