How to beginner · 3 min read

How to use NER pipeline Hugging Face

Quick answer
Use the Hugging Face transformers library's pipeline function with the ner task to perform named entity recognition. Initialize the pipeline with a pretrained model like dbmdz/bert-large-cased-finetuned-conll03-english and pass your text to extract entities.

PREREQUISITES

  • Python 3.8+
  • pip install transformers>=4.0.0
  • pip install torch (or tensorflow)
  • Internet connection to download pretrained models

Setup

Install the transformers library and a backend like torch or tensorflow. Set up your Python environment to run the NER pipeline.

bash
pip install transformers torch

Step by step

Use the pipeline function from transformers to create an NER pipeline. Pass your input text to get named entities with their labels and confidence scores.

python
from transformers import pipeline

# Initialize NER pipeline with a pretrained model
ner_pipeline = pipeline('ner', model='dbmdz/bert-large-cased-finetuned-conll03-english', aggregation_strategy='simple')

# Input text
text = "Hugging Face Inc. is a company based in New York City."

# Run NER
entities = ner_pipeline(text)

# Print extracted entities
for entity in entities:
    print(f"Entity: {entity['word']}, Label: {entity['entity_group']}, Score: {entity['score']:.4f}")
output
Entity: Hugging Face Inc., Label: ORG, Score: 0.9987
Entity: New York City, Label: LOC, Score: 0.9991

Common variations

You can customize the NER pipeline by using different pretrained models or changing the aggregation_strategy to control how tokens are grouped into entities. Async usage is possible with transformers but requires additional setup.

python
from transformers import pipeline

# Using a different model
ner_pipeline = pipeline('ner', model='dslim/bert-base-NER', aggregation_strategy='simple')

text = "Apple is looking at buying U.K. startup for $1 billion"
entities = ner_pipeline(text)

for entity in entities:
    print(f"Entity: {entity['word']}, Label: {entity['entity_group']}, Score: {entity['score']:.4f}")
output
Entity: Apple, Label: ORG, Score: 0.9992
Entity: U.K., Label: LOC, Score: 0.9985
Entity: $1 billion, Label: MISC, Score: 0.9876

Troubleshooting

If you encounter errors like Model not found, verify your model name and internet connection. For tokenization issues, ensure your transformers and backend libraries are up to date. If entities are fragmented, try changing aggregation_strategy to simple or none depending on your needs.

Key Takeaways

  • Use Hugging Face's pipeline with task ner for easy named entity recognition.
  • Choose pretrained models like dbmdz/bert-large-cased-finetuned-conll03-english for high accuracy.
  • Set aggregation_strategy='simple' to group tokens into coherent entities.
  • Install transformers and a backend like torch before running the pipeline.
  • Check model names and library versions if you face errors or unexpected output.
Verified 2026-04 · dbmdz/bert-large-cased-finetuned-conll03-english, dslim/bert-base-NER
Verify ↗