langchain-ai/langchain · error · TypeError
Expected a generator function type for `transform`.Instead g
Error message
Expected a generator function type for `transform`.Instead got an unsupported type: {type(transform)} What it means
`RunnableGenerator` wraps a transform that must be either a sync generator function (`def f(input: Iterator[X]) -> Iterator[Y]` with `yield`) or an async generator function (`async def ... yield`). Passing a plain function, a coroutine function, or a non-function value fails the type checks and raises a `TypeError` at construction time. The class exists specifically to lazily stream chunks, so a non-generator transform has no streaming semantics to wrap.
Source
Thrown at libs/core/langchain_core/runnables/base.py:4515
"""
func_for_name: Callable[..., Any]
if atransform is not None:
self._atransform = atransform
func_for_name = atransform
if is_async_generator(transform):
self._atransform = transform
func_for_name = transform
elif inspect.isgeneratorfunction(transform):
self._transform = transform
func_for_name = transform
else:
msg = (
"Expected a generator function type for `transform`."
f"Instead got an unsupported type: {type(transform)}"
)
raise TypeError(msg)
try:
self.name = name or func_for_name.__name__
except AttributeError:
self.name = "RunnableGenerator"
@property
@override
def InputType(self) -> Any:
func = getattr(self, "_transform", None) or self._atransform
try:
params = inspect.signature(func).parameters
first_param = next(iter(params.values()), None)
if first_param and first_param.annotation != inspect.Parameter.empty:
return getattr(first_param.annotation, "__args__", (Any,))[0]
except ValueError:
pass
return AnyView on GitHub (pinned to e32fa9a52e)
Solutions
- Make the transform a generator: accept an iterator and `yield` results per chunk.
- For async use, write an `async def` transform containing `yield` (an async generator).
- If the function cannot stream, use `RunnableLambda` instead of `RunnableGenerator`.
- Verify with `inspect.isgeneratorfunction(f)` or `is_async_generator(f)` before constructing.
Example fix
// before
def double_all(chunks):
return [c * 2 for c in chunks]
rg = RunnableGenerator(double_all) # TypeError
// after
def double_all(chunks):
for c in chunks:
yield c * 2
rg = RunnableGenerator(double_all) Defensive patterns
Strategy: type-guard
Validate before calling
import inspect
from langchain_core.runnables.utils import is_async_generator
def is_generator_transform(fn) -> bool:
return inspect.isgeneratorfunction(fn) or is_async_generator(fn) Type guard
import inspect
def is_sync_generator_function(fn) -> bool:
return inspect.isgeneratorfunction(fn) Try / catch
try:
rg = RunnableGenerator(transform)
except TypeError as e:
if 'unsupported type' in str(e):
# convert fn into a generator or use RunnableLambda
raise
raise Prevention
- Always write transforms with `yield` (or `async def` + `yield`).
- Check with inspect.isgeneratorfunction() when accepting user-supplied transforms.
- Use RunnableLambda for non-streaming callables.
When it happens
Trigger: `RunnableGenerator(regular_function)`; `RunnableGenerator(async_regular_function)` (a coroutine, not an async generator); `RunnableGenerator(lambda chunks: [f(c) for c in chunks])` (returns a list, no `yield`); passing a callable object that is not a function.
Common situations: Migrating from `RunnableLambda` to `RunnableGenerator` and forgetting to add `yield`; writing `async def transform(x): return ...` instead of `async def transform(x): yield ...`; wrapping an existing map-style helper.
Related errors
- {self!r} only supports async methods.
- {self!r} only supports sync methods.
- Cannot stream a coroutine function synchronously.Use `astrea
- SyncTextProjection requires a string delta
- SyncTextProjection requires a string final value
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/a597b162093340c7.
Report an issue: GitHub.