cocoindex-io/cocoindex · error · TypeError

mount_each() requires a ComponentSubpath when the function h

Error message

mount_each() requires a ComponentSubpath when the function has no __name__. Provide an explicit subpath as the first argument.

What it means

mount_each() mounts the same function once per item and, like mount(), derives each component's subpath from the function's __name__ (composed with the item path). If the function is anonymous it cannot derive a stable subpath, so an explicit ComponentSubpath must be supplied.

Source

Thrown at python/cocoindex/_internal/api.py:484

    Returns:
        A handle that can be used to wait until all processing units are ready.
    """
    check_cancellation()
    check_not_in_process_live("coco.mount_each")

    if pos_args and isinstance(pos_args[0], ComponentSubpath):
        subpath = pos_args[0]
        fn = pos_args[1]
        items = pos_args[2]
        extra_args = pos_args[3:]
    else:
        fn = pos_args[0]
        items = pos_args[1]
        extra_args = pos_args[2:]
        name = _default_subpath_name(fn)
        if name is None:
            raise TypeError(
                "mount_each() requires a ComponentSubpath when the function has no "
                "__name__. Provide an explicit subpath as the first argument."
            )
        subpath = ComponentSubpath(Symbol(name))

    parent_ctx = get_context_from_ctx()
    child_path = build_child_path(parent_ctx, subpath)

    if isinstance(items, LiveMapFeed):
        # Live data source: the per-item `fn` (whether a plain function or a
        # LiveComponent class) is dispatched through `mount()` / `operator.update()`
        # inside `_MountEachLiveComponent`, both of which already handle live
        # component classes — so no special-casing of `fn` is needed here.
        instance = _MountEachLiveComponent(items, fn, extra_args, kwargs)
        return await _mount_live_component(parent_ctx, child_path, instance)

    # Static data source: mount one component per item. When `fn` is a
    # LiveComponent class, each item gets its own live component instance

View on GitHub (pinned to e84aa99b32)

Solutions

  1. Pass an explicit subpath: coco.mount_each(coco.component_subpath('process'), fn, items, *extra_args)
  2. Use a named @coco.fn function as the per-item processor
  3. Ensure decorators use functools.wraps to preserve __name__

Example fix

// before
await coco.mount_each(lambda f, t: process(f, t), files.items(), target)
// after
await coco.mount_each(process_file, files.items(), target)
Defensive patterns

Strategy: validation

Validate before calling

name = getattr(fn, "__name__", None)
if name is None:
    await coco.mount_each(coco.component_subpath("process-item"), fn, items, *extra)
else:
    await coco.mount_each(fn, items, *extra)

Type guard

def per_item_fn_ok(fn) -> bool:
    return callable(fn) and getattr(fn, "__name__", "<lambda>") != "<lambda>"

Try / catch

try:
    await coco.mount_each(fn, items, *extra)
except TypeError as e:
    if "requires a ComponentSubpath" in str(e):
        await coco.mount_each(coco.component_subpath("process-item"), fn, items, *extra)

Prevention

When it happens

Trigger: Calling coco.mount_each(lambda item: ..., items, ...) or with any callable lacking __name__, without an explicit ComponentSubpath as the first argument.

Common situations: Using lambdas in per-item loops; applying mount_each to partial-bound functions; custom decorators dropping __name__.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of cocoindex-io/cocoindex@e84aa99b32 (2026-09-08). Data as JSON: /api/errors/af722d5978effc15. Report an issue: GitHub.