<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Pyspark custom Transformer class -AttributeError: 'DummyMod' object has no attribute 'MyTransfor in Machine Learning</title>
    <link>https://community.databricks.com/t5/machine-learning/pyspark-custom-transformer-class-attributeerror-dummymod-object/m-p/178315#M4779</link>
    <description>&lt;P&gt;This looks like a class-loading issue. Spark saves the Python class path in the stage metadata and imports that class when loading the pipeline. A class defined inside a notebook may not be available under the same module path later.&lt;/P&gt;&lt;P&gt;DefaultParamsReadable and DefaultParamsWritable persist parameters, but they do not package your custom Python class.&lt;/P&gt;&lt;P&gt;For this particular transformation, I would use the built-in SQLTransformer to extract the result field:&lt;/P&gt;&lt;P&gt;from pyspark.ml.feature import SQLTransformer&lt;/P&gt;&lt;P&gt;extractor = SQLTransformer(&lt;BR /&gt;statement="""&lt;BR /&gt;SELECT *,&lt;BR /&gt;spark_nlp_col.result AS extracted_result&lt;BR /&gt;FROM __THIS__&lt;BR /&gt;"""&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;Replace your custom extraction stage with this transformer. Change spark_nlp_col and extracted_result to your actual column names, then rebuild and save the pipeline to a new path.&lt;/P&gt;&lt;P&gt;For a Spark NLP annotation column containing an array of structs, the expected extracted_result is an array of result strings. This matches the nested-field extraction in your custom transformer.&lt;/P&gt;&lt;P&gt;If you need to retain the custom class:&lt;/P&gt;&lt;P&gt;- Put MyTransformer in an importable Python module or wheel.&lt;BR /&gt;- Make that package available in both the saving and loading environments.&lt;BR /&gt;- Import the class from that module before creating the pipeline.&lt;BR /&gt;- Rebuild and save the pipeline so its metadata contains the stable module path.&lt;/P&gt;&lt;P&gt;Also check the small typos in the example: in_suff should be in_suffix and PipelinModel should be PipelineModel.&lt;/P&gt;&lt;P&gt;Official references:&lt;/P&gt;&lt;P&gt;&lt;A href="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.feature.SQLTransformer.html" target="_blank"&gt;https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.feature.SQLTransformer.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://spark.apache.org/docs/latest/api/python/_modules/pyspark/ml/util.html" target="_blank"&gt;https://spark.apache.org/docs/latest/api/python/_modules/pyspark/ml/util.html&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 01 Dec 2026 10:38:03 GMT</pubDate>
    <dc:creator>tajamulkhan</dc:creator>
    <dc:date>2026-12-01T10:38:03Z</dc:date>
    <item>
      <title>Pyspark custom Transformer class -AttributeError: 'DummyMod' object has no attribute 'MyTransformer'</title>
      <link>https://community.databricks.com/t5/machine-learning/pyspark-custom-transformer-class-attributeerror-dummymod-object/m-p/75669#M3386</link>
      <description>&lt;P&gt;I am trying to create a custom transformer as a stage in my pipeline. A few of the transformations I am doing via SparkNLP and the next few using MLlib. To pass the result of SparkNLP transformation at a stage to the next MLlib transformation, I need to extract the spark_nlp_col.result column and pass it, and I am using a custom transformation stage for that.&lt;BR /&gt;After I fit my pipeline, I am able to persist it but when I am loading it back again, I am getting an error:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;AttributeError: 'DummyMod' object has no attribute 'MyTransformer'&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Here is my class:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.ml import Transformer
from pyspark.ml.param.shared import Param,Params,TypeConverters

class MyTransformer(Transformer,DefaultParamsWritable,DefaultParamsReadable):
    inputCol = Param(Params._dummy(), "inputCol", "",TypeConverters.toString)
    outputCol = Param(Params._dummy(), "outputCol", "",TypeConverters.toString)

    def __init__(self,inputCol=None,outputCol=None):
        super(MyTransformer, self).__init__()
        self._setDefault(inputCol=None)
        self._set(inputCol = inputCol)
        self._setDefault(outputCol=None)
        self._set(outputCol = outputCol)

    def getInputCol(self):
        return self.getOrDefault(self.inputCol)

    def setInputCol(self, inputCol):
        self._set(inputCol=inputCol)

    def getOutputCol(self):
        return self.getOrDefault(self.outputCol)

    def setOutputCol(self, outputCol):
        self._set(outputCol=outputCol)

    def _transform(self, dataset):
        in_col = self.getInputCol()
        out_col = self.getOutputCol()

        final_in_col = in_col+".result"
        result = dataset.withColumn(out_col, dataset[final_in_col])
        return result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have created a simple wrapper function over it for standardisation and then used it to create pipeline, fit and save it:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def extract_col(cols, in_suffix, out_suffix):
   return [MyTransformer(inputCol=col+in_suff, outputCol=col+out_suffix) for col in cols]&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;'''
stages before custom transformer
'''
extractors = extract_col(cols, "_in", "_out")
'''
stages after custom transformer
'''

stages = s1 + s2 + .. + extractors + .. + sn-1 + sn
pipeline = Pipeline(stages = stages)
fit_pipeline = pipeline.fit(data)
fit_pipeline.write().overwrite().save(path_to_store_at)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How I am reading it back:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;saved_pipeline = PipelinModel.load("path_where_stored")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then I am encountering the error.&lt;BR /&gt;I have tried multiple ways of writing the custom class, using HasInputCol, HasOutputCol, etc, nothing working so far. Any idea on how I can resolve it?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2024 07:31:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/pyspark-custom-transformer-class-attributeerror-dummymod-object/m-p/75669#M3386</guid>
      <dc:creator>simranisanewbie</dc:creator>
      <dc:date>2024-06-25T07:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Pyspark custom Transformer class -AttributeError: 'DummyMod' object has no attribute 'MyTransfor</title>
      <link>https://community.databricks.com/t5/machine-learning/pyspark-custom-transformer-class-attributeerror-dummymod-object/m-p/178314#M4778</link>
      <description>&lt;P&gt;Hi Simran&lt;BR /&gt;&lt;BR /&gt;This looks like a class-loading issue. Spark saves the Python class path in the stage metadata and imports that class when loading the pipeline. A class defined inside a notebook may not be available under the same module path later.&lt;/P&gt;&lt;P&gt;DefaultParamsReadable and DefaultParamsWritable persist parameters, but they do not package your custom Python class.&lt;/P&gt;&lt;P&gt;For this particular transformation, I would use the built-in SQLTransformer to extract the result field:&lt;/P&gt;&lt;P&gt;from pyspark.ml.feature import SQLTransformer&lt;/P&gt;&lt;P&gt;extractor = SQLTransformer(&lt;BR /&gt;statement="""&lt;BR /&gt;SELECT *,&lt;BR /&gt;spark_nlp_col.result AS extracted_result&lt;BR /&gt;FROM __THIS__&lt;BR /&gt;"""&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;Replace your custom extraction stage with this transformer. Change spark_nlp_col and extracted_result to your actual column names, then rebuild and save the pipeline to a new path.&lt;/P&gt;&lt;P&gt;For a Spark NLP annotation column containing an array of structs, the expected extracted_result is an array of result strings. This matches the nested-field extraction in your custom transformer.&lt;/P&gt;&lt;P&gt;If you need to retain the custom class:&lt;/P&gt;&lt;P&gt;- Put MyTransformer in an importable Python module or wheel.&lt;BR /&gt;- Make that package available in both the saving and loading environments.&lt;BR /&gt;- Import the class from that module before creating the pipeline.&lt;BR /&gt;- Rebuild and save the pipeline so its metadata contains the stable module path.&lt;/P&gt;&lt;P&gt;Also check the small typos in the example: in_suff should be in_suffix and PipelinModel should be PipelineModel.&lt;/P&gt;&lt;P&gt;Official references:&lt;/P&gt;&lt;P&gt;&lt;A href="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.feature.SQLTransformer.html" target="_blank"&gt;https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.feature.SQLTransformer.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://spark.apache.org/docs/latest/api/python/_modules/pyspark/ml/util.html" target="_blank"&gt;https://spark.apache.org/docs/latest/api/python/_modules/pyspark/ml/util.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Dec 2026 10:34:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/pyspark-custom-transformer-class-attributeerror-dummymod-object/m-p/178314#M4778</guid>
      <dc:creator>tajamulkhan</dc:creator>
      <dc:date>2026-12-01T10:34:45Z</dc:date>
    </item>
    <item>
      <title>Re: Pyspark custom Transformer class -AttributeError: 'DummyMod' object has no attribute 'MyTransfor</title>
      <link>https://community.databricks.com/t5/machine-learning/pyspark-custom-transformer-class-attributeerror-dummymod-object/m-p/178315#M4779</link>
      <description>&lt;P&gt;This looks like a class-loading issue. Spark saves the Python class path in the stage metadata and imports that class when loading the pipeline. A class defined inside a notebook may not be available under the same module path later.&lt;/P&gt;&lt;P&gt;DefaultParamsReadable and DefaultParamsWritable persist parameters, but they do not package your custom Python class.&lt;/P&gt;&lt;P&gt;For this particular transformation, I would use the built-in SQLTransformer to extract the result field:&lt;/P&gt;&lt;P&gt;from pyspark.ml.feature import SQLTransformer&lt;/P&gt;&lt;P&gt;extractor = SQLTransformer(&lt;BR /&gt;statement="""&lt;BR /&gt;SELECT *,&lt;BR /&gt;spark_nlp_col.result AS extracted_result&lt;BR /&gt;FROM __THIS__&lt;BR /&gt;"""&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;Replace your custom extraction stage with this transformer. Change spark_nlp_col and extracted_result to your actual column names, then rebuild and save the pipeline to a new path.&lt;/P&gt;&lt;P&gt;For a Spark NLP annotation column containing an array of structs, the expected extracted_result is an array of result strings. This matches the nested-field extraction in your custom transformer.&lt;/P&gt;&lt;P&gt;If you need to retain the custom class:&lt;/P&gt;&lt;P&gt;- Put MyTransformer in an importable Python module or wheel.&lt;BR /&gt;- Make that package available in both the saving and loading environments.&lt;BR /&gt;- Import the class from that module before creating the pipeline.&lt;BR /&gt;- Rebuild and save the pipeline so its metadata contains the stable module path.&lt;/P&gt;&lt;P&gt;Also check the small typos in the example: in_suff should be in_suffix and PipelinModel should be PipelineModel.&lt;/P&gt;&lt;P&gt;Official references:&lt;/P&gt;&lt;P&gt;&lt;A href="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.feature.SQLTransformer.html" target="_blank"&gt;https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.feature.SQLTransformer.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://spark.apache.org/docs/latest/api/python/_modules/pyspark/ml/util.html" target="_blank"&gt;https://spark.apache.org/docs/latest/api/python/_modules/pyspark/ml/util.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Dec 2026 10:38:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/pyspark-custom-transformer-class-attributeerror-dummymod-object/m-p/178315#M4779</guid>
      <dc:creator>tajamulkhan</dc:creator>
      <dc:date>2026-12-01T10:38:03Z</dc:date>
    </item>
  </channel>
</rss>

