{"record":{"id":"37ab3174c48992d6","repo":"apache/beam","slug":"map-can-be-used-only-with-callable-objects-received-r","errorCode":null,"errorMessage":"Map can be used only with callable objects. Received %r instead.","messagePattern":"Map 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":2121,"sourceCode":"  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\n  Returns:\n    ~apache_beam.pvalue.PCollection:\n    A :class:`~apache_beam.pvalue.PCollection` containing the\n    :func:`Map` 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        'Map can be used only with callable objects. '\n        'Received %r instead.' % (fn))\n  from apache_beam.transforms.util import fn_takes_side_inputs\n  if fn_takes_side_inputs(fn):\n    wrapper = lambda x, *args, **kwargs: [fn(x, *args, **kwargs)]\n  else:\n    wrapper = lambda x: [fn(x)]\n\n  label = 'Map(%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  # Proxy the type-hint information from the original function to this new\n  # wrapped function.\n  type_hints = get_type_hints(fn).with_defaults(\n      typehints.decorators.IOTypeHints.from_callable(fn))","sourceCodeStart":2103,"sourceCodeEnd":2139,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/core.py#L2103-L2139","documentation":"beam.Map accepts only plain callables; a DoFn instance or any other non-callable is rejected up front with this TypeError, since Map builds a lambda wrapper around fn rather than a ParDo-accepted DoFn.","triggerScenarios":"beam.Map(SomeDoFn()) or beam.Map(some_object) where fn does not implement __call__.","commonSituations":"Confusion between beam.Map and beam.ParDo when refactoring to stateful/DoFn-based transforms; passing a method result instead of the method; passing data (like a dict) intended as extra context rather than a function.","solutions":["Switch to beam.ParDo(MyDoFn(...)) if fn is a DoFn instance.","Pass a callable: a lambda, function, method, or callable class instance.","Verify you're not calling the function when passing it: beam.Map(my_fn) not beam.Map(my_fn(x)).","Wrap the DoFn's process logic in a plain function if Map semantics are what you need."],"exampleFix":"// before\nbeam.Map(MyDoFn())\n// after\nbeam.ParDo(MyDoFn())\n// or\nbeam.Map(lambda x: x * 2)","handlingStrategy":"type-guard","validationCode":"if not callable(fn):\n    raise TypeError('Map needs a callable, got %r' % (fn,))","typeGuard":"def is_map_fn(fn):\n    return callable(fn) and not isinstance(fn, DoFn)","tryCatchPattern":"try:\n    out = pcoll | beam.Map(fn)\nexcept TypeError as e:\n    if 'callable objects' in str(e):\n        out = pcoll | beam.ParDo(fn)\n    else:\n        raise","preventionTips":["Prefer lambdas/functions with Map; reserve DoFn for ParDo.","Review refactors that swap Map <-> ParDo.","Add type annotations: fn: Callable[[X], Y]."],"tags":["python","apache-beam","map","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"}