Skip to content

API breaks when defining custom elements in model #215

@brooklynbagel

Description

@brooklynbagel

Describe the bug

When defining a custom element to use in a model, the the API for serving up the model breaks. For example, using this simple no-op transformer results in the API breaking with a error message like AttributeError: Can't get attribute 'MyTransformer' on <module '__main__' from '...'> even when loaded into an API like https://github.com/brooklynbagel/vetiver-reprex-custom-elements/blob/3ec27d180be0c6ec115af1554fbd2b8f830fa73b/attempt-2/api/app.py.

class MyTransformer(TransformerMixin, BaseEstimator):
    def fit(self, X, y=None):
        return self

    def transform(self, X):
        return X

To Reproduce
Steps to reproduce the behavior:

  1. Define a custom element to use a model
  2. Deploy said model
  3. Either deploy API for model or run locally with uvicorn app:api
  4. See error AttributeError: Can't get attribute 'MyTransformer' on <module '__main__' from '...'>

Expected behavior
API should start up normally with no error

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context

See reprex

It does work when forcing the transformer into module __main__, see

class MyTransformer(TransformerMixin, BaseEstimator):
    def fit(self, X, y=None):
        return self

    def transform(self, X):
        return X

# this fixes the `AttributeError`
setattr(sys.modules["__main__"], "MyTransformer", MyTransformer)

API deployed on dogfood: https://connect.posit.it/content/aaac8d80-fb22-48d0-98df-bf1683f91170

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions