{"record":{"id":"5f438a1e58f3fa04","repo":"apache/beam","slug":"filter-can-be-used-only-with-callable-objects-received-r","errorCode":null,"errorMessage":"Filter can be used only with callable objects. Received %r instead.","messagePattern":"Filter can be used only with callable objects\\. Received %r instead\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/core.py","lineNumber":2836,"sourceCode":"\n  Args:\n    fn (``Callable[..., bool]``): a callable object. First argument will be an\n      element.\n    *args: positional arguments passed to the transform callable.\n    **kwargs: keyword arguments passed to the transform callable.\n\n  Returns:\n    ~apache_beam.pvalue.PCollection:\n    A :class:`~apache_beam.pvalue.PCollection` containing the\n    :func:`Filter` outputs.\n\n  Raises:\n    TypeError: If the **fn** passed as argument is not a callable.\n      Typical error is to pass a :class:`DoFn` instance which is supported only\n      for :class:`ParDo`.\n  \"\"\"\n  if not callable(fn):\n    raise TypeError(\n        'Filter can be used only with callable objects. '\n        'Received %r instead.' % (fn))\n  wrapper = lambda x, *args, **kwargs: [x] if fn(x, *args, **kwargs) else []\n\n  label = 'Filter(%s)' % ptransform.label_from_callable(fn)\n\n  # TODO: What about callable classes?\n  if hasattr(fn, '__name__'):\n    wrapper.__name__ = fn.__name__\n\n  # Get type hints from this instance or the callable. Do not use output type\n  # hints from the callable (which should be bool if set).\n  fn_type_hints = typehints.decorators.IOTypeHints.from_callable(fn)\n  if fn_type_hints is not None:\n    fn_type_hints = fn_type_hints.with_output_types()\n  type_hints = get_type_hints(fn).with_defaults(fn_type_hints)\n\n  # Proxy the type-hint information from the function being wrapped, setting the","sourceCodeStart":2818,"sourceCodeEnd":2854,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/core.py#L2818-L2854","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a plain predicate callable, e.g. Filter(lambda x: x > 0)","If you need DoFn features (side inputs via DoFn params, setup/teardown), use ParDo with the DoFn instead","Wrap the DoFn logic in a plain function if a predicate is all that is needed"],"exampleFix":"// before\nbeam.Filter(MyFilterDoFn())\n// after\nbeam.Filter(lambda x: MyFilterDoFn.process_logic(x))","handlingStrategy":"type-guard","validationCode":"if not callable(fn):\n    raise TypeError('beam.Filter requires a callable, got %r' % (fn,))","typeGuard":"def is_filter_fn(fn) -> bool:\n    return callable(fn) and not isinstance(fn, DoFn)","tryCatchPattern":"try:\n    step = beam.Filter(fn)\nexcept TypeError as e:\n    if 'Filter can be used only with callable' in str(e):\n        step = beam.Filter(lambda x: bool(fn.process_one(x)))  # adapt\n    else:\n        raise","preventionTips":["Remember Filter takes predicates, not DoFns; ParDo is the DoFn transform","Add type hints (Callable[[T], bool]) to filter helper functions","Quick-check with callable(fn) before composing pipelines programmatically"],"tags":["python","apache-beam","filter","type-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}