langchain-ai/langchain · error · TypeError
Cannot invoke a coroutine function synchronously.Use `ainvok
Error message
Cannot invoke a coroutine function synchronously.Use `ainvoke` instead.
What it means
`RunnableLambda.invoke` only works when the object was constructed with a sync callable (stored as `self.func`). If it was created from a coroutine function only (`RunnableLambda(async_fn)` or `func=..., afunc=...` where the sync path is absent), there is no synchronous implementation and calling `.invoke()` raises `TypeError` directing you to `ainvoke`. Coroutine functions cannot be executed from sync code without an event loop.
Source
Thrown at libs/core/langchain_core/runnables/base.py:5318
config: The config to use.
**kwargs: Additional keyword arguments.
Returns:
The output of this `Runnable`.
Raises:
TypeError: If the `Runnable` is a coroutine function.
"""
if hasattr(self, "func"):
return self._call_with_config(
self._invoke,
input,
ensure_config(config),
**kwargs,
)
msg = "Cannot invoke a coroutine function synchronously.Use `ainvoke` instead."
raise TypeError(msg)
@override
async def ainvoke(
self,
input: Input,
config: RunnableConfig | None = None,
**kwargs: Any | None,
) -> Output:
"""Invoke this `Runnable` asynchronously.
Args:
input: The input to this `Runnable`.
config: The config to use.
**kwargs: Additional keyword arguments.
Returns:
The output of this `Runnable`.
View on GitHub (pinned to e32fa9a52e)
Solutions
- Use `await r.ainvoke(x)` in async contexts.
- Provide a sync implementation too: `RunnableLambda(sync_fn, afunc=async_fn)`.
- Bridge in sync code: `asyncio.run(r.ainvoke(x))` (only when no loop is running).
- Inside a running loop, offload via `asyncio.run_coroutine_threadsafe` from another thread.
Example fix
# before
r = RunnableLambda(async_fetch) # async only
result = r.invoke('doc') # TypeError
# after
result = asyncio.run(r.ainvoke('doc'))
# or supply both:
r = RunnableLambda(sync_fetch, afunc=async_fetch)
result = r.invoke('doc') Defensive patterns
Strategy: type-guard
Validate before calling
def supports_sync_invoke(r) -> bool:
return hasattr(r, 'func') Type guard
def is_sync_runnable_lambda(r) -> bool:
return hasattr(r, 'func') Try / catch
try:
out = r.invoke(x)
except TypeError as e:
if 'ainvoke' in str(e):
out = asyncio.run(r.ainvoke(x))
else:
raise Prevention
- Provide both func and afunc for runnables used in both execution modes.
- Standardize entry points: sync callers get sync-built chains.
- Use asyncio.run only when no event loop is already running.
When it happens
Trigger: `r = RunnableLambda(async_fn)` then `r.invoke(x)`; a chain containing an async-only lambda invoked synchronously (`chain.invoke(x)`); sync test code exercising an async-built pipeline.
Common situations: Sharing runnables between async servers and sync scripts; tutorials written async being adapted into sync notebooks; forgetting a `func` when both `func`/`afunc` were planned.
Related errors
- Func was provided as a coroutine function, but afunc was als
- Cannot stream a coroutine function synchronously.Use `astrea
- Cannot stream from a generator function asynchronously.Use .
- {self!r} only supports sync methods.
- Expected a callable type for `func`.Instead got an unsupport
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/00aec15e15a1f25b.
Report an issue: GitHub.