apache/beam · error · TypeError

Expected a callable object instead of: %r

Error message

Expected a callable object instead of: %r

What it means

CallableWrapperDoFn.__init__ (core.py:1004) wraps an arbitrary callable as a DoFn for FlatMap/Map. It validates that `fn` is callable before wrapping; passing a non-callable (a plain value, class instance without __call__, string, etc.) raises this TypeError.

Solutions

  1. Pass a callable: a function, lambda, method, or object implementing __call__.
  2. Remove accidental parentheses so you pass the function itself, not its result.
  3. Check for None values from factory functions that were supposed to return the transform function.

Example fix

# before
beam.FlatMap('split_words')

# after
beam.FlatMap(lambda line: line.split())
Defensive patterns

Strategy: type-guard

Validate before calling

def check_callable(fn):
    if not callable(fn):
        raise TypeError(f'FlatMap/Map require a callable, got {type(fn).__name__}: {fn!r}')

Type guard

def is_callable_arg(x) -> bool:
    return callable(x)

Try / catch

try:
    dofn = beam.FlatMap(fn)
except TypeError as e:
    if 'Expected a callable object' in str(e):
        raise TypeError(f'Got {type(fn).__name__}; pass a function/lambda/callable') from e
    raise

Prevention

When it happens

Trigger: beam.FlatMap(x) / beam.Map(x) where x is not a function or callable object — e.g. FlatMap([1,2,3]), Map('lowercase'), passing a lambda result by mistake, or forgetting the lambda: FlatMap(lambda: ...) mis-parenthesized.

Common situations: Typo calling FlatMap with a value instead of a function; passing a bound method from the wrong object; calling a function and passing its result (fn() vs fn); passing None after a failed lookup.

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/b91757b02ed2e2d8. Report an issue: GitHub.

Appendix: source

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

class CallableWrapperDoFn(DoFn):
  """For internal use only; no backwards-compatibility guarantees.

  A DoFn (function) object wrapping a callable object.

  The purpose of this class is to conveniently wrap simple functions and use
  them in transforms.
  """
  def __init__(self, fn, fullargspec=None):
    """Initializes a CallableWrapperDoFn object wrapping a callable.

    Args:
      fn: A callable object.

    Raises:
      TypeError: if fn parameter is not a callable type.
    """
    if not callable(fn):
      raise TypeError('Expected a callable object instead of: %r' % fn)

    self._fn = fn
    self._fullargspec = fullargspec
    if isinstance(
        fn, (types.BuiltinFunctionType, types.MethodType, types.FunctionType)):
      self.process = fn
    else:
      # For cases such as set / list where fn is callable but not a function
      self.process = lambda element: fn(element)

    super().__init__()

  def display_data(self):
    # If the callable has a name, then it's likely a function, and
    # we show its name.
    # Otherwise, it might be an instance of a callable class. We
    # show its class.
    display_data_value = (

View on GitHub (pinned to 12126d8942)