In Hugging Face Transformers, what does a single pipeline('sentiment-analysis') call assemble for you, and what should you pin before production?
answer
- a task string is a lookup key
- alias first, then the task registry
- Auto head class plus its tokenizer
- softmax or sigmoid after the logits
- warning about default model and revision
basics
~20 spipeline('sentiment-analysis') maps the alias to text-classification, loads that task's default checkpoint through AutoModelForSequenceClassification with its tokenizer, and turns logits into label and score dicts. Pin model= and revision= so production stops depending on library defaults.
solid answer
~40 s`pipeline()` is a factory. For `"sentiment-analysis"` it first maps the alias to `"text-classification"` (`TASK_ALIASES`), then reads that task's entry in `SUPPORTED_TASKS`: the pipeline class, the Auto head (`AutoModelForSequenceClassification`) and a default checkpoint pinned to a commit. With no `model=`, it loads `distilbert/distilbert-base-uncased-finetuned-sst-2-english` and logs a warning that relying on a default model and revision in production is not recommended. It loads the matching tokenizer, runs the forward pass, and in post-processing applies softmax (several labels) or sigmoid (one label) to return `[{'label': ..., 'score': ...}]`. Before production, pass an explicit `task=`, `model=` and a `revision=` commit, so a library upgrade or a push to the repo's `main` branch cannot silently change the weights you run.
code
python · 13 linesfrom transformers import pipeline
# Notebook: every choice left to the library
clf = pipeline("sentiment-analysis")
# Batch job: task, checkpoint and commit spelled out
clf = pipeline(
task="text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0f", # the commit Transformers pins for this task's default
)
results = clf(["The delivery was late again.", "Great support team."])
# one {'label': ..., 'score': ...} dict per inputgo deeper
Recall the three stages: tokenizer in, model forward pass, post-processing out. Know that a task string picks a default model and that sentiment-analysis is only an alias for text-classification.
Explain the registry: task to pipeline class, Auto head and default checkpoint, and how the Hub's pipeline_tag supplies the task when you omit it. Show that feature-extraction returns per-token hidden states.
Treat the one-liner as unfinished work: cite the warning about default models, pin task, model and revision, and recognise a freshly initialised head from the MISSING entries in the loading report.
Frame pipeline() as a prototyping convenience whose defaults follow the library's release cycle, and argue for a team rule that unattended code never relies on a task default or a moving branch.
## What `pipeline()` actually is `pipeline()` in Hugging Face Transformers is a **factory function**. It takes a task string, a model id, or both, and returns a ready-to-call object that wraps three stages: **pre-processing** (a tokenizer, image processor or feature extractor turns raw input into tensors), the **model forward pass**, and **post-processing** (turning raw outputs such as logits into labels, scores or text). The convenience is real, but every stage has a default that someone else chose, and a notebook that works with those defaults is not yet a reproducible job. ## What one call resolves, step by step For `pipeline("sentiment-analysis")` with nothing else passed: 1. **Alias lookup.** `"sentiment-analysis"` is an entry in `TASK_ALIASES` and maps to `"text-classification"`. The other aliases are `"ner"` for `"token-classification"` and `"text-to-speech"` for `"text-to-audio"`. 2. **Registry lookup.** The canonical task is looked up in `SUPPORTED_TASKS`, which records the pipeline class (`TextClassificationPipeline`), the Auto head class (`AutoModelForSequenceClassification`) and a default checkpoint with a pinned commit. An unknown string raises a `KeyError` that lists the available tasks. 3. **Default model.** With no `model=`, the factory loads `distilbert/distilbert-base-uncased-finetuned-sst-2-english` at the commit the registry records, and logs that using a pipeline without specifying a model name and revision in production is not recommended. 4. **Revision resolution.** The requested revision (by default the repo's `main` branch) is resolved to a commit once per load, so the config, weights and tokenizer of that load all come from one repository state. 5. **Model and pre-processor.** The weights are loaded through the head class, and the matching tokenizer is loaded from the same repo. For vision, audio and multimodal tasks the pipeline class decides whether an image processor, feature extractor or combined processor is loaded instead of, or alongside, the tokenizer. 6. **Post-processing.** For text classification the pipeline applies softmax when the model has several labels, sigmoid when it has one or is configured for multi-label, and nothing for regression, then returns `[{'label': ..., 'score': ...}]`, by default only the top label (`top_k=1`). ## Where task names come from: `pipeline_tag` Every model repo on the Hub can carry a **`pipeline_tag`**, the task it is meant for. The Hub infers it from `config.json` (a token-classification architecture maps to `token-classification`) or from the card's tags, and an author can override it with `pipeline_tag:` in the model card's YAML header. When you call `pipeline(model="your-org/your-model")` without a task, Transformers asks the Hub for that tag and uses it. That lookup: - fails in offline mode, because it needs the Hub API; - fails when the repo has no `pipeline_tag`; - refuses a repo whose `library_name` is neither `transformers` nor `timm`; - raises the unknown-task `KeyError` when the tag names a task Transformers has no pipeline for. Hub task names and Transformers task strings overlap but are not the same list: a Sentence Transformers repo may be tagged `sentence-similarity`, which no Transformers pipeline implements. ## What the head and post-processing hide - **The head is chosen by the task, not by the checkpoint.** `pipeline("text-classification", model="google-bert/bert-base-uncased")` loads, but that checkpoint has no classification head, so a new one is created with fresh weights. The loading report lists those parameters as **MISSING** (newly initialized because missing from the checkpoint, consider training on your downstream task), and the scores are noise. - **`feature-extraction` has no head at all.** It loads `AutoModel` and returns the first model output, normally the last hidden state: one vector per token, shape `[1, sequence_length, hidden_size]` with `return_tensors=True`, nested lists otherwise. Nothing is pooled or normalised. - **Scores are post-processed.** `score` is a softmax or sigmoid output rather than a raw logit; `function_to_apply` overrides the choice (`"softmax"`, `"sigmoid"` or `"none"`), and `top_k` controls how many labels come back, with `top_k=None` returning a score for every label. A multi-label classifier read through softmax, or a regression head read as a probability, is a silent bug the defaults usually prevent but a wrong config can cause. - **The output shape varies by task.** Classification returns label and score dicts, token classification returns one dict per token unless an `aggregation_strategy` groups them into entities, text generation returns `generated_text`. Code that consumes the results is written against that shape, which is one more reason to fix the task explicitly. ## What to pin before production | Left to the library | What can change under you | Pin it with | |---|---|---| | Default model for a task | A new Transformers release can change the registry's default checkpoint | `model="..."` | | Repo branch (`main`) | The repo owner pushes new weights or a new tokenizer | `revision="<commit sha>"` | | Task inferred from `pipeline_tag` | A card edit changes the tag; offline runs cannot look it up | `task="..."` | Spell out all three for anything that runs unattended. Revision pinning in depth (tags, pull-request refs, a shared cache, offline mode) is Hub material; for the pipeline the rule is simply that the notebook's one-liner becomes an explicit `task`, `model` and `revision`.
- What happens if you call pipeline(model='your-org/your-model') without a task?Transformers asks the Hub for the repo's `pipeline_tag` and uses it as the task. That lookup raises in offline mode, raises when the repo has no `pipeline_tag` or its `library_name` is something other than transformers or timm, and raises an unknown-task `KeyError` when the tag names a task Transformers has no pipeline for. Passing `task=` removes the Hub lookup and that whole class of failures.
- Why does pipeline('text-classification', model='google-bert/bert-base-uncased') load but return meaningless scores?That checkpoint has no classification head. The task makes the pipeline load it through `AutoModelForSequenceClassification`, which creates a classifier whose weights are not in the checkpoint, so they are freshly initialised; the loading report lists them as MISSING and suggests training. The label names and scores mean nothing until you fine-tune it or choose a checkpoint trained for the task.
- What does pipeline('feature-extraction') return, and why is it not a sentence embedding?It loads the checkpoint with `AutoModel`, which has no task head, and returns the first model output, normally the last hidden state: one vector per token, shape `[1, sequence_length, hidden_size]` with `return_tensors=True` and nested lists otherwise. Nothing pools or normalises those vectors, so turning them into one vector per sentence is a separate step.
- A pipeline with only a task string is production-ready because its default model never changes.
- sentiment-analysis and text-classification build two different pipeline classes.
- Any Hub checkpoint gives meaningful output under any task string you pass.
- Text-classification pipelines return raw logits as the score.
- feature-extraction returns one pooled vector per sentence.