{"record":{"id":"e27a53d9baaabbc4","repo":"langchain-ai/langchain","slug":"runnablebranch-branches-must-be-tuples-or-lists-n","errorCode":null,"errorMessage":"RunnableBranch branches must be tuples or lists, not {type(branch)}","messagePattern":"RunnableBranch branches must be tuples or lists, not (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/branch.py","lineNumber":120,"sourceCode":"\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                )\n                raise ValueError(msg)\n            condition, runnable = branch\n            condition = cast(\"Runnable[Input, bool]\", coerce_to_runnable(condition))\n            runnable = coerce_to_runnable(cast(\"Runnable[Input, Output]\", runnable))\n            branches_.append((condition, runnable))\n\n        super().__init__(\n            branches=branches_,\n            default=default_,\n        )\n\n    model_config = ConfigDict(","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/branch.py#L102-L138","documentation":"Each non-final argument to `RunnableBranch.__init__` must be a `tuple` or `list` of the form `(condition, runnable)`. When an argument is any other type (a bare function, a string, a dict, None), the `isinstance(branch, (tuple, list))` check fails and a `TypeError` naming the offending type is raised.","triggerScenarios":"Passing unwrapped callables as condition branches: `RunnableBranch(is_q, answer_fn, default)`; passing a dict `{cond: fn}` instead of a pair; forgetting the parentheses around a pair; passing None from a failed lookup in a dynamically built branch list.","commonSituations":"Porting code from LCEL pipe syntax where bare functions are valid; building branches with `*branches` where one element is malformed; assuming keyword-style `{condition: runnable}` dicts are accepted like in older examples.","solutions":["Wrap every condition branch as a 2-tuple: `RunnableBranch((is_q, answer_fn), default)`.","When assembling branches programmatically, validate each element is a tuple/list of length 2 before constructing.","Check for None/empty elements in generated branch lists and filter them out."],"exampleFix":"# before\nbranch = RunnableBranch(is_q, answer_fn, default_fn)\n\n# after\nbranch = RunnableBranch((is_q, answer_fn), default_fn)","handlingStrategy":"validation","validationCode":"for b in branches[:-1]:\n    assert isinstance(b, (tuple, list)), f\"branch must be tuple/list, got {type(b)}: {b!r}\"","typeGuard":"from typing import TypeGuard\n\ndef is_branch_pair(b: object) -> TypeGuard[tuple | list]:\n    return isinstance(b, (tuple, list))","tryCatchPattern":null,"preventionTips":["Always parenthesize condition branches: (condition, runnable).","When building branches dynamically, validate shape before RunnableBranch(*branches).","Filter out None entries from generated branch lists."],"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-15T17:31:12.345Z"}