How to Get the Score of Every Label from a Hugging Face Text Classification Pipeline
A Hugging Face text-classification pipeline normally answers with one label: the one the model is most sure of. Often you want more, such as how close the second choice was, or a score for every label to compare or store. The top_k argument does that. This guide prints every emotion score an emotion model gives a sentence, with Transformers 5.26.0 on Python 3.14.
Syntax
results = classifier("<text>", top_k=<number or None>)
| Part | What it means |
|---|---|
classifier | A pipeline made with pipeline("text-classification", …) |
"<text>" | The text to classify |
top_k=None | Return every label, highest score first |
top_k=<number> | Return only that many labels, highest score first |
Without top_k, the pipeline returns only the best label. The pipeline's documentation describes top_k as "How many results to return", with a default of 1.
The Example
The sentence is "I can't believe I finally got tickets to the concert!" The model is bhadresh-savani/distilbert-base-uncased-emotion, which sorts English text into six emotions: sadness, joy, love, anger, fear and surprise. It's Apache-2.0 licensed. How to Find a Model for a Task on the Hugging Face Hub shows how it was chosen.
Prerequisites
- uv. This guide uses uv 0.13.10. The project uses Python 3.14, which uv downloads if needed.
- About 270 MB for the model, downloaded on the first run.
Step 1: Create the Project
uv init --python 3.14 top-k-demo
cd top-k-demo
uv add transformers torch
That's a project with Transformers 5.26.0 and PyTorch 2.15.0.
Step 2: Write the Script
The script classifies the sentence twice: once the default way, once with top_k=None:
from transformers import pipeline
classifier = pipeline(
"text-classification",
model="bhadresh-savani/distilbert-base-uncased-emotion",
revision="98864ce5bb9cb58a41e882e62f1d8e55173b5ac6",
)
text = "I can't believe I finally got tickets to the concert!"
print(classifier(text))
for result in classifier(text, top_k=None):
print(f"{result['label']:<9}{result['score']:.4f}")

With top_k=None the result is a list of dictionaries, one per label, so the loop prints each label with its score, padded to line them up.
Step 3: Run It
uv run python emotions.py
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
config.json: 100%|█████████████████████████████████████████████████| 768/768 [00:00<00:00, 4.22MB/s]
model.safetensors: downloading bytes: ██████████████████████████████████████████| 250MB, 6.99MB/s
model.safetensors: reconstructing file: 100%|██████████████████████████| 268MB / 268MB, 16.5MB/s
Loading weights: 100%|█████████████████████████████████████████| 104/104 [00:00<00:00, 12810.80it/s]
tokenizer_config.json: 100%|███████████████████████████████████████| 291/291 [00:00<00:00, 1.07MB/s]
vocab.txt: 100%|█████████████████████████████████████████████████| 232k/232k [00:00<00:00, 2.21MB/s]
special_tokens_map.json: 100%|██████████████████████████████████████| 112/112 [00:00<00:00, 451kB/s]
[{'label': 'joy', 'score': 0.9966805577278137}]
joy 0.9967
surprise 0.0009
sadness 0.0008
anger 0.0006
fear 0.0005
love 0.0005

The first run downloads the model. Then:
- The default gave a list with one dictionary:
joy, with a score of 0.9967. top_k=Nonegave all six emotions, highest first. Joy is far ahead; surprise is a distant second.
The six scores add up to 1. The model splits its certainty across its labels, so a score is how much of it went to that label. The command works unchanged in PowerShell.
Get Only the Best Few
A number keeps that many labels. This script asks for the best two:
from transformers import pipeline
classifier = pipeline(
"text-classification",
model="bhadresh-savani/distilbert-base-uncased-emotion",
revision="98864ce5bb9cb58a41e882e62f1d8e55173b5ac6",
)
text = "I can't believe I finally got tickets to the concert!"
print(classifier(text, top_k=2))
uv run python top_two.py
Loading weights: 100%|██████████████████████████████████████████| 104/104 [00:00<00:00, 7267.22it/s]
[{'label': 'joy', 'score': 0.9966805577278137}, {'label': 'surprise', 'score': 0.0008958745747804642}]
Joy, then surprise, as in the full list. That's handy when you only want to know how close the runner-up came.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using return_all_scores=True from older examples | In Transformers 5 it's silently ignored, and you get only the best label: see Fixed: return_all_scores=True Returns Only One Label | Use top_k=None |
| Expecting the labels in the model's own order | With top_k, they come highest score first | Sort them yourself, or look labels up by name |
| Reading a high score as "certainly right" | The score is the model's own certainty, which can be wrong | Check the model on examples of your own texts |
FAQ
Can I pass top_k when creating the pipeline?
Yes, but then one text comes back wrapped in one more list, [[…]], and a loop like the one above fails with TypeError: list indices must be integers or slices, not str. Fixed: TypeError: list indices must be integers or slices, not str with top_k shows why. Passing top_k in the call, as this guide does, avoids it.
Does it work with a list of texts?
Yes. classifier(["first text", "second text"], top_k=None) returns one list of labels per text, in the same order.
Why do the scores add up to 1?
For a model that picks one label per text, the pipeline turns the model's raw outputs into probabilities with the softmax function, which always adds up to 1.
Conclusion
top_k=None turned the pipeline's single answer, joy (0.9967), into a score for each of the model's six emotions, highest first, and top_k=2 kept just the best two.
Additional Resources
- TextClassificationPipeline —
top_k,function_to_applyand the other options. - bhadresh-savani/distilbert-base-uncased-emotion — the model's card.
