-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
System Info
Transformers.js version 2.13.1 or newer
Next.js version 13.5.6
typescript 5.3.3
macOS 14.2.1
Environment/Platform
- Website/web-app
- Browser extension
- Server-side (e.g., Node.js, Deno, Bun)
- Desktop app (e.g., Electron)
- Other (e.g., VSCode extension)
Description
I'm using the singleton pattern from the examples in the documentation:
class SentimentSingleton {
static task = 'sentiment-analysis';
static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'; // this is the default model for sentiment
static instance : Promise<Pipeline>;
static getInstance () {
this.instance ??= pipeline(this.task, this.model);
return this.instance;
}
}Starting with 2.13.1, it the next.js build step throws this error:
Type error: Type 'Promise<FeatureExtractionPipeline | TextClassificationPipeline | TokenClassificationPipeline | ... 19 more ... | DepthEstimationPipeline>' is not assignable to type 'Promise<Pipeline>'.
Type 'FeatureExtractionPipeline | TextClassificationPipeline | TokenClassificationPipeline | ... 19 more ... | DepthEstimationPipeline' is not assignable to type 'Pipeline'.
Type 'FeatureExtractionPipeline' is missing the following properties from type 'Pipeline': task, model, tokenizer, processor, dispose
19 | static instance : Promise<Pipeline>;
20 | static getInstance () {
> 21 | this.instance ??= pipeline(this.task, this.model);
| ^
22 | return this.instance;
23 | }
24 | }
Up to and including transformers.js version 2.13.0 this compiles and runs cleanly. The pipeline() function interface is changed between 2.13.0 and 2.13.1. A patch release should not change the interfaces. A breaking change like this should bump the minor version number at least.
Reproduction
Save this typescript file and open it in VS Code. Install transformers 2.13.1 or newer (tested through 2.13.4) Typescript will complain with the same error above at this.instance = assignment and the type of the first argument to pipeline().
By moving the string 'sentiment-analysis' into the call to pipeline() as a constant instead of using this.task, and using the TextClassificationPipeline type for the Promise, the typing works.
import { pipeline, env, Tensor, Pipeline } from '@xenova/transformers';
class SentimentSingleton {
static task = 'sentiment-analysis';
static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'; // this is the default model for sentiment
static instance : Promise<Pipeline>;
static getInstance () {
this.instance ??= pipeline(this.task, this.model);
return this.instance;
}
}