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 Any

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Make the transform a generator: accept an iterator and `yield` results per chunk.
  2. For async use, write an `async def` transform containing `yield` (an async generator).
  3. If the function cannot stream, use `RunnableLambda` instead of `RunnableGenerator`.
  4. 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

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


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/a597b162093340c7. Report an issue: GitHub.