{"record":{"id":"91aba440ae0fe56e","repo":"langchain-ai/langchain","slug":"func-was-provided-as-a-coroutine-function-but-afu","errorCode":null,"errorMessage":"Func was provided as a coroutine function, but afunc was also provided. If providing both, func should be a regular function to avoid ambiguity.","messagePattern":"Func was provided as a coroutine function, but afunc was also provided\\. If providing both, func should be a regular function to avoid ambiguity\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/base.py","lineNumber":4924,"sourceCode":"\n        Raises:\n            TypeError: If the `func` is not a callable type.\n            TypeError: If both `func` and `afunc` are provided.\n\n        \"\"\"\n        func_for_name: Callable[..., Any]\n        if afunc is not None:\n            self.afunc = afunc\n            func_for_name = afunc\n\n        if is_async_callable(func) or is_async_generator(func):\n            if afunc is not None:\n                msg = (\n                    \"Func was provided as a coroutine function, but afunc was \"\n                    \"also provided. If providing both, func should be a regular \"\n                    \"function to avoid ambiguity.\"\n                )\n                raise TypeError(msg)\n            self.afunc = func\n            func_for_name = func\n        elif callable(func):\n            self.func = cast(\"Callable[[Input], Output]\", func)\n            func_for_name = func\n        else:\n            msg = (  # type: ignore[unreachable]\n                \"Expected a callable type for `func`.\"\n                f\"Instead got an unsupported type: {type(func)}\"\n            )\n            raise TypeError(msg)\n\n        try:\n            if name is not None:\n                self.name = name\n            elif func_for_name.__name__ != \"<lambda>\":\n                self.name = func_for_name.__name__\n        except AttributeError:","sourceCodeStart":4906,"sourceCodeEnd":4942,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/base.py#L4906-L4942","documentation":"`RunnableLambda` accepts `func` (sync) and optionally `afunc` (async). If `func` itself is a coroutine function or async generator AND `afunc` is also provided, there would be two async candidates with no way to disambiguate, so the constructor raises a `TypeError`. The contract is: `func` sync + `afunc` async, or a single async `func`.","triggerScenarios":"`RunnableLambda(async_fn, afunc=async_fn)`; passing `afunc` while `func` is an `async def` (even the same function); copy-pasting an async function into both arguments.","commonSituations":"Refactoring a sync+async pair and accidentally making `func` async while leaving `afunc`; passing a decorator-wrapped async callable to both slots; library wrappers that forward `**kwargs` into `RunnableLambda`.","solutions":["Provide a regular (non-async) function as `func` alongside `afunc`: `RunnableLambda(sync_fn, afunc=async_fn)`.","If you only have an async function, pass it alone: `RunnableLambda(async_fn)` — it becomes the async implementation.","Audit call sites that forward user callables to both parameters."],"exampleFix":"// before\nRunnableLambda(my_async_fn, afunc=my_async_fn)  # TypeError\n\n// after\nRunnableLambda(my_sync_fn, afunc=my_async_fn)\n// or, async-only:\nRunnableLambda(my_async_fn)","handlingStrategy":"validation","validationCode":"import inspect\n\ndef valid_lambda_pair(func, afunc) -> bool:\n    if afunc is None:\n        return True\n    return not inspect.iscoroutinefunction(func) and not inspect.isasyncgenfunction(func)","typeGuard":"import inspect\nfrom langchain_core.runnables.utils import is_async_callable, is_async_generator\n\ndef is_sync_callable(fn) -> bool:\n    return not is_async_callable(fn) and not is_async_generator(fn)","tryCatchPattern":"try:\n    r = RunnableLambda(func, afunc=afunc)\nexcept TypeError as e:\n    if 'should be a regular function' in str(e):\n        r = RunnableLambda(sync_version_of(func), afunc=afunc)\n    else:\n        raise","preventionTips":["Convention: func is always sync, afunc is always async.","Lint against passing `async def` functions into the func slot when afunc is present.","Never forward the same async callable to both arguments."],"tags":["runnable","runnable-lambda","typeerror","sync-async-mismatch"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}