Skip to main content

Fixed: Hugging Face NER Pipeline Splits a Name into Pieces Like "##ka"

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A Hugging Face named entity recognition (NER) pipeline returned the name "Anoushka" as three separate people: An, ##oush and ##ka. The model had split the name into word pieces and tagged two of the later pieces as the start of a new name, and aggregation_strategy="simple" followed those tags. aggregation_strategy="first" gives each word one tag, so the name came back whole.

What I Was Doing​

I was finding the names in a sentence with dslim/bert-base-NER, a model that tags people (PER), organizations (ORG), places (LOC) and other names (MISC), in Transformers 5.26.0 on Python 3.14. aggregation_strategy="simple" asks the pipeline to group the model's tokens into whole names:

ner.py
from transformers import pipeline

ner = pipeline(
"ner",
model="dslim/bert-base-NER",
revision="d1a3e8f13f8c3566299d95fcfc9a8d2382a9affc",
aggregation_strategy="simple",
)

for entity in ner("My friend Anoushka moved to Toronto last year."):
print(f"{entity['word']:<10}{entity['entity_group']:<4}{entity['score']:.3f}")
Terminal
uv run python ner.py

What I Expected​

Two names: Anoushka as a person and Toronto as a place.

What I Got​

Output
Loading weights: 100%|██████████████████████████████████████████| 199/199 [00:00<00:00, 6541.99it/s]
[transformers] BertForTokenClassification LOAD REPORT from: dslim/bert-base-NER
Key | Status | Details
-------------------------+------------+--------
bert.pooler.dense.bias | UNEXPECTED |
bert.pooler.dense.weight | UNEXPECTED |

Notes:
- UNEXPECTED: can be ignored when loading from different task/architecture; not ok if you expect identical arch.
An PER 0.999
##oush PER 0.764
##ka PER 0.957
Toronto LOC 0.999

The terminal after uv run python ner.py: the load report, then four entities: An as PER with 0.999, ##oush as PER with 0.764, ##ka as PER with 0.957, and Toronto as LOC with 0.999.

Toronto was right, but Anoushka came back as three people: An, ##oush and ##ka. The ## at the start of a piece means it continues the word before it. (The load report before the results is harmless: see Fixed: BertForTokenClassification LOAD REPORT from: dslim/bert-base-NER.)

What I Checked​

What the model tagged. Without aggregation_strategy, the pipeline returns one result per token, with the model's own tag. I printed each token's word, tag and score:

tokens.py
from transformers import pipeline

ner = pipeline(
"ner",
model="dslim/bert-base-NER",
revision="d1a3e8f13f8c3566299d95fcfc9a8d2382a9affc",
)

for token in ner("My friend Anoushka moved to Toronto last year."):
print(f"{token['word']:<10}{token['entity']:<8}{token['score']:.3f}")
Output (after the load report)
An B-PER 0.999
##ous B-PER 0.976
##h I-PER 0.552
##ka B-PER 0.957
Toronto B-LOC 0.999

The tokenizer split "Anoushka" into four pieces: An, ##ous, ##h and ##ka. The model tagged three of them B-PER, "beginning of a person's name", and one I-PER, "inside a person's name". The pipeline leaves out tokens tagged O, "outside any name", which is why the other words aren't listed.

What "simple" does with those tags. The pipeline's documentation for aggregation_strategy says "simple" groups tokens following the tags, and warns:

Notice that two consecutive B tags will end up as different entities. On word based languages, we might end up splitting words undesirably

That's what happened: each B-PER started a new name. ##ous and ##h (B-PER, then I-PER) became ##oush, with the average of their scores, 0.764, and ##ka became a third name.

What the documentation offers instead. The same documentation describes three other strategies, each marked "works only on word based models", meaning languages that put spaces between words:

  • "first": words "cannot end up with different tags. Words will simply use the tag of the first token of the word when there is ambiguity."
  • "average": "scores will be averaged first across tokens, and then the maximum label is applied."
  • "max": "Word entity will simply be the token with the maximum score."

The model's card warns about it too: "the model occassionally tags subword tokens as entities and post-processing of results may be necessary to handle those cases." Its community discussions include a closed one about the same thing, Error NER result of people's name, where "simple" returned the name "Tweet" as T, T. The answer there was to use "average".

Why It Happens​

BERT models read words as pieces from a fixed vocabulary, so an uncommon name is split into several tokens. The model tags each piece separately, and it doesn't always tag the later pieces I-PER. "simple" trusts every tag, so each B-PER piece starts a name of its own, even in the middle of a word.

The Fix​

Use aggregation_strategy="first", which tags each whole word with its first piece's tag:

ner.py
from transformers import pipeline

ner = pipeline(
"ner",
model="dslim/bert-base-NER",
revision="d1a3e8f13f8c3566299d95fcfc9a8d2382a9affc",
aggregation_strategy="first",
)

for entity in ner("My friend Anoushka moved to Toronto last year."):
print(f"{entity['word']:<10}{entity['entity_group']:<4}{entity['score']:.3f}")

The fixed ner.py in a macOS-style code window: the NER pipeline with dslim/bert-base-NER pinned to a commit and aggregation_strategy set to first, and the loop printing each entity's word, group and score for the sentence My friend Anoushka moved to Toronto last year.

The first piece, An, is tagged B-PER, so the whole word "Anoushka" becomes one person.

After the Fix​

Terminal
uv run python ner.py
Output
Loading weights: 100%|█████████████████████████████████████████| 199/199 [00:00<00:00, 13362.79it/s]
[transformers] BertForTokenClassification LOAD REPORT from: dslim/bert-base-NER
Key | Status | Details
-------------------------+------------+--------
bert.pooler.dense.bias | UNEXPECTED |
bert.pooler.dense.weight | UNEXPECTED |

Notes:
- UNEXPECTED: can be ignored when loading from different task/architecture; not ok if you expect identical arch.
Anoushka PER 0.999
Toronto LOC 0.999

The terminal after the fix: the load report, then two entities: Anoushka as PER with 0.999 and Toronto as LOC with 0.999.

Anoushka is one person, with a score of 0.999, and Toronto is still a place. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Joining the ## pieces yourself after "simple"You rebuild what "first", "average" and "max" already doChoose one of those strategies
Using "first", "average" or "max" with a language written without spacesThe documentation says they work "only on word based models"Use "simple" for those languages
Expecting the fix to make the model more accurateIt only changes how the tokens are grouped; the model's tags are the sameFor names the model misses, try another model

Troubleshooting​

SymptomLikely causeFix
A name of two words comes back as two namesThe model tagged the second word B-PERThe documentation notes the strategies "only work on real words, "New york" might still be tagged with two different entities"
A grouped name has a low score, such as ##oush with 0.764A group's score is the average of its pieces' scores, and ##h had 0.552Run without aggregation_strategy to see each piece's own tag and score

FAQ​

Do "average" and "max" fix it too?​

Yes. On this sentence, all three word-based strategies returned Anoushka as one person and Toronto as a place, while "simple" returned An, ##oush and ##ka.

Which one should I choose?​

"first" is the simplest to reason about: a word gets the tag of its first piece. "average" and "max" look at the scores of every piece, so they can choose another tag when the first piece is uncertain.

Conclusion​

With aggregation_strategy="simple", dslim/bert-base-NER returned "Anoushka" as An, ##oush and ##ka, because the model tagged three of the name's four pieces B-PER. aggregation_strategy="first" gave each word the tag of its first piece and returned Anoushka as one person.

Additional Resources​