apache/beam · error · TypeError

Filter can be used only with callable objects. Received %r…

Error message

Filter can be used only with callable objects. Received %r instead.

What it means

apache_beam.Filter accepts only plain callables (functions, lambdas, etc.); unlike ParDo it does not accept DoFn instances. This TypeError is raised in Filter() when the fn argument is not callable, most commonly because a DoFn object was passed.

Solutions

  1. Pass a plain predicate callable, e.g. Filter(lambda x: x > 0)
  2. If you need DoFn features (side inputs via DoFn params, setup/teardown), use ParDo with the DoFn instead
  3. Wrap the DoFn logic in a plain function if a predicate is all that is needed

Example fix

// before
beam.Filter(MyFilterDoFn())
// after
beam.Filter(lambda x: MyFilterDoFn.process_logic(x))
Defensive patterns

Strategy: type-guard

Validate before calling

if not callable(fn):
    raise TypeError('beam.Filter requires a callable, got %r' % (fn,))

Type guard

def is_filter_fn(fn) -> bool:
    return callable(fn) and not isinstance(fn, DoFn)

Try / catch

try:
    step = beam.Filter(fn)
except TypeError as e:
    if 'Filter can be used only with callable' in str(e):
        step = beam.Filter(lambda x: bool(fn.process_one(x)))  # adapt
    else:
        raise

Prevention

When it happens

Trigger: calling Filter(MyDoFnInstance(...)), Filter(SomeObject) where the object lacks __call__, or passing a class instead of an instance in a way that is not callable.

Common situations: Developers migrating transforms between ParDo and Filter assume Filter supports DoFns like ParDo does, or pass a type/option object by mistake instead of a predicate function.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/5f438a1e58f3fa04. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/transforms/core.py:2836

  Args:
    fn (``Callable[..., bool]``): a callable object. First argument will be an
      element.
    *args: positional arguments passed to the transform callable.
    **kwargs: keyword arguments passed to the transform callable.

  Returns:
    ~apache_beam.pvalue.PCollection:
    A :class:`~apache_beam.pvalue.PCollection` containing the
    :func:`Filter` outputs.

  Raises:
    TypeError: If the **fn** passed as argument is not a callable.
      Typical error is to pass a :class:`DoFn` instance which is supported only
      for :class:`ParDo`.
  """
  if not callable(fn):
    raise TypeError(
        'Filter can be used only with callable objects. '
        'Received %r instead.' % (fn))
  wrapper = lambda x, *args, **kwargs: [x] if fn(x, *args, **kwargs) else []

  label = 'Filter(%s)' % ptransform.label_from_callable(fn)

  # TODO: What about callable classes?
  if hasattr(fn, '__name__'):
    wrapper.__name__ = fn.__name__

  # Get type hints from this instance or the callable. Do not use output type
  # hints from the callable (which should be bool if set).
  fn_type_hints = typehints.decorators.IOTypeHints.from_callable(fn)
  if fn_type_hints is not None:
    fn_type_hints = fn_type_hints.with_output_types()
  type_hints = get_type_hints(fn).with_defaults(fn_type_hints)

  # Proxy the type-hint information from the function being wrapped, setting the

View on GitHub (pinned to 12126d8942)