Lightning-AI/pytorch-lightning · error · TypeError

`Fabric.launch(function={function})` needs to take at least

Error message

`Fabric.launch(function={function})` needs to take at least one argument. The launcher will pass in the `Fabric` object so you can use it inside the function.

What it means

The function passed to fabric.launch() must accept at least one parameter because the launcher injects the Fabric instance as the first argument (fn(fabric, *args, **kwargs)). inspect.signature(function).parameters is empty for zero-arg functions, so they fail with TypeError.

Source

Thrown at src/lightning/fabric/fabric.py:1001

                # ... training code ...

            fabric = Fabric(accelerator="tpu", devices=8)
            fabric.launch(train_function)

        """
        if _is_using_cli():
            raise RuntimeError(
                "This script was launched through the CLI, and processes have already been created. Calling "
                " `.launch()` again is not allowed."
            )
        if function is not _do_nothing:
            if not callable(function):
                raise TypeError(
                    f"`Fabric.launch(...)` needs to be a callable, but got {function}."
                    " HINT: do `.launch(your_fn)` instead of `.launch(your_fn())`"
                )
            if not inspect.signature(function).parameters:
                raise TypeError(
                    f"`Fabric.launch(function={function})` needs to take at least one argument. The launcher will"
                    " pass in the `Fabric` object so you can use it inside the function."
                )
        elif isinstance(self.strategy.launcher, (_MultiProcessingLauncher, _XLALauncher)):
            raise TypeError(
                f"To spawn processes with the `{type(self.strategy).__name__}` strategy, `.launch()` needs to be called"
                " with a function that contains the code to launch in processes."
            )
        return self._wrap_and_launch(function, self, *args, **kwargs)

    def _filter_kwargs_for_callback(self, method: Callable, kwargs: dict[str, Any]) -> dict[str, Any]:
        """Filter keyword arguments to only include those that match the callback method's signature.

        Args:
            method: The callback method to inspect
            kwargs: The keyword arguments to filter

        Returns:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Add a fabric parameter: def train(fabric): ... and use it inside instead of globals
  2. For lambdas: .launch(lambda fabric: train(fabric))

Example fix

# before
def train(): ...
fabric.launch(train)
# after
def train(fabric): ...
fabric.launch(train)
Defensive patterns

Strategy: validation

Validate before calling

import inspect
assert inspect.signature(train_fn).parameters, 'launch fn must accept fabric'

Type guard

import inspect
def takes_fabric(fn):
    try:
        return len(inspect.signature(fn).parameters) > 0
    except (TypeError, ValueError):
        return False

Prevention

When it happens

Trigger: fabric.launch(lambda: train()) or def train(): ... — any zero-parameter callable. Note: builtins without introspectable signatures can also trip inspect.signature.

Common situations: Wrapping an existing training function that used a global Fabric object; using functools.partial with no remaining free parameters.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/5de22dae91c9cafb. Report an issue: GitHub.