C.W.K.
Stream
Lesson 02 of 06 · published

The pipeline() Abstraction

~28 min · transformers, pipeline

Level 0Scout
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

What pipeline() actually does

pipeline() is a factory function that wires three things behind one call: a tokenizer, a model, and a task-specific pre/post-processing function. You pass in a task name ("text-generation", "sentiment-analysis", "automatic-speech-recognition", ...) and a model id; it returns a callable that takes raw input (text, audio bytes, image PIL) and returns task-shaped output.

The task name is the contract. pipeline("text-generation") always returns [{"generated_text": str}]. pipeline("text-classification") always returns [{"label": str, "score": float}]. This consistency is why the same pipeline call works across thousands of models — the model card's pipeline_tag field is the routing key.

Where pipeline() ends

pipeline() is the right tool for: prototyping, single-input inference, demos, and Colab notebooks. It is the wrong tool for: batch inference at throughput, custom decoding (beam search variants, JSON-mode, structured output), or anything that needs the raw logits. The moment you need control, drop down to AutoTokenizer + AutoModelForXxx — that's the next lesson.

One feature you do get out of pipeline(): device placement and batching. Pass device=0 for GPU 0, device="mps" on Apple Silicon, or device_map="auto" for multi-GPU sharding via accelerate.

Code

Five common tasks, same shape·python
from transformers import pipeline

# Text generation (causal LM)
gen = pipeline("text-generation", model="gpt2", device_map="auto")
print(gen("Hello", max_new_tokens=20)[0]["generated_text"])

# Text classification
cls = pipeline("sentiment-analysis")
print(cls("I love this library."))  # [{'label': 'POSITIVE', 'score': 0.99...}]

# Zero-shot classification
zs = pipeline("zero-shot-classification")
print(zs("This is a plot synopsis", candidate_labels=["sci-fi", "romance", "horror"]))

# Speech-to-text
asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
# print(asr("audio.wav"))

# Image-to-text
i2t = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
# print(i2t("photo.jpg"))
Batched inference with device control·python
from transformers import pipeline

cls = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english", device=0)

# Pass a list — pipeline batches automatically
texts = [
    "I love this library.",
    "This is okay.",
    "What a nightmare.",
] * 100  # 300 inputs

results = cls(texts, batch_size=32)
print(len(results), results[0])

External links

Exercise

Build a small tool that takes a CSV of customer reviews and outputs a CSV with two new columns: sentiment label and confidence score. Use pipeline('text-classification', batch_size=32, device=0 or 'mps'). Time the run on 1000 reviews. Then re-run with device='cpu' and note the difference.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.