{"record":{"id":"8ab2f46f4d97043e","repo":"apache/beam","slug":"flatmap-can-be-used-only-with-callable-objects-received-r","errorCode":null,"errorMessage":"FlatMap can be used only with callable objects. Received %r instead.","messagePattern":"FlatMap 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":2091,"sourceCode":"\n  Args:\n    fn (callable): a callable object.\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:`FlatMap` 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  label = 'FlatMap(%s)' % ptransform.label_from_callable(fn)\n  if not callable(fn):\n    raise TypeError(\n        'FlatMap can be used only with callable objects. '\n        'Received %r instead.' % (fn))\n\n  pardo = ParDo(CallableWrapperDoFn(fn), *args, **kwargs)\n  pardo.label = label\n\n  return pardo\n\n\ndef Map(fn, *args, **kwargs):  # pylint: disable=invalid-name\n  \"\"\":func:`Map` is like :func:`FlatMap` except its callable returns only a\n  single element.\n\n  Args:\n    fn (callable): a callable object.\n    *args: positional arguments passed to the transform callable.\n    **kwargs: keyword arguments passed to the transform callable.\n","sourceCodeStart":2073,"sourceCodeEnd":2109,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/core.py#L2073-L2109","documentation":"beam.FlatMap accepts only plain callables; it wraps them in CallableWrapperDoFn itself. Passing a non-callable — most commonly a DoFn instance, which is only supported by ParDo — raises this TypeError.","triggerScenarios":"beam.FlatMap(SomeDoFn()) or beam.FlatMap(non_callable_value) where fn lacks __call__.","commonSituations":"Migrating code between ParDo and FlatMap and forgetting to unwrap the DoFn; passing a class instead of an instance is fine but passing an instance that isn't callable fails; typos where a variable holding data is passed instead of a function.","solutions":["Use beam.ParDo(MyDoFn(...)) for DoFn instances instead of FlatMap.","If you meant a plain function, pass the function object itself (not its result): beam.FlatMap(my_fn) not beam.FlatMap(my_fn()).","Make sure the object is callable — classes are callable (they'll be instantiated), instances need __call__ or should be a function/method.","Extract the core logic of the DoFn into a plain function if you prefer FlatMap style."],"exampleFix":"// before\nbeam.FlatMap(MyDoFn(args))\n// after\nbeam.ParDo(MyDoFn(args))\n// or, with a plain function:\nbeam.FlatMap(lambda x: [x, x])","handlingStrategy":"type-guard","validationCode":"if not callable(fn):\n    raise TypeError('FlatMap needs a callable, got %r' % (fn,))","typeGuard":"def is_flatmap_fn(fn):\n    return callable(fn) and not isinstance(fn, DoFn)","tryCatchPattern":"try:\n    out = pcoll | beam.FlatMap(fn)\nexcept TypeError as e:\n    if 'callable objects' in str(e):\n        out = pcoll | beam.ParDo(fn)  # fn was a DoFn\n    else:\n        raise","preventionTips":["Rule of thumb: DoFn -> ParDo, plain function -> FlatMap/Map.","Don't call the function when passing it (pass my_fn, not my_fn()).","Enable mypy to catch non-callable arguments."],"tags":["python","apache-beam","flatmap","callable"],"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"}