{"record":{"id":"5f1f47ee90aa116c","repo":"langchain-ai/langchain","slug":"runnablebranch-requires-at-least-two-branches","errorCode":null,"errorMessage":"RunnableBranch requires at least two branches","messagePattern":"RunnableBranch requires at least two branches","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/branch.py","lineNumber":99,"sourceCode":"            RunnableLike[Input, Output],\n        ]\n        | RunnableLike[Input, Output],\n    ) -> None:\n        \"\"\"A `Runnable` that runs one of two branches based on a condition.\n\n        Args:\n            *branches: A list of `(condition, Runnable)` pairs.\n                Defaults a `Runnable` to run if no condition is met.\n\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 \"","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/branch.py#L81-L117","documentation":"`RunnableBranch.__init__` in `langchain_core.runnables.branch` requires at least `_MIN_BRANCHES` (2) positional arguments: one or more `(condition, runnable)` pairs plus a final default runnable. With fewer than two arguments there is no conditional branch, so the constructor raises `ValueError` immediately.","triggerScenarios":"Calling `RunnableBranch()` with no arguments, or with only a single default runnable, e.g. `RunnableBranch(default_fn)` or `RunnableBranch((cond, fn))` with nothing else — the last argument is always treated as the default, so a lone pair still leaves zero real branches.","commonSituations":"Dynamically building a branch list that ends up empty (`RunnableBranch(*pairs, default)` where `pairs == []`); misunderstanding the signature and assuming the default is optional; passing a single pair and expecting it to be the condition branch rather than the default.","solutions":["Always supply at least one `(condition, runnable)` pair plus a trailing default: `RunnableBranch((cond, fn), default_fn)`.","When building branches dynamically, guard: `if not pairs: just use the runnable directly instead of RunnableBranch`.","If you only need one path, skip `RunnableBranch` and use the runnable or a simple conditional `RunnableLambda`."],"exampleFix":"# before\nbranch = RunnableBranch(*(pairs_or_empty_list), default_fn)  # pairs empty -> ValueError\n\n# after\nif pairs:\n    branch = RunnableBranch(*pairs, default_fn)\nelse:\n    branch = default_fn","handlingStrategy":"validation","validationCode":"branches = [(cond, fn), ...]\nif len(branches) + 1 < 2:  # pairs + default\n    raise ValueError(\"need at least one (condition, runnable) pair plus a default\")","typeGuard":"from typing import TypeGuard\n\ndef has_enough_branches(pairs: list, default: object) -> TypeGuard[list]:\n    return len(pairs) >= 1 and default is not None","tryCatchPattern":"try:\n    branch = RunnableBranch(*pairs, default_fn)\nexcept ValueError as e:\n    if \"at least two branches\" in str(e):\n        branch = default_fn  # degenerate case: no conditions\n    else:\n        raise","preventionTips":["Treat the last argument to RunnableBranch as the default, always.","Build branches as a list of pairs and assert it is non-empty before construction.","Skip RunnableBranch entirely when there are no conditions."],"tags":["runnable-branch","value-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"}