# Opus-MT: Translation returns \<unk\> token

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fopus-mt-translation-returns-unk-token%2F158124%3E%5C
**Category:** Models\
**Created:** [June 5, 2025, 12:50pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fopus-mt-translation-returns-unk-token%2F158124 "2025-06-05T12:50:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathdons](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@mathdons](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmathdons)%5C
**Post date:** [June 5, 2025, 12:50pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fopus-mt-translation-returns-unk-token%2F158124%2F1 "2025-06-05T12:50:34Z")

</div>

(x-posting with StackOverflow)

I’m having relatively good results with HelsinkiNlp models for translation, except for one thing: some special characters are omitted from the translation. If I decode without skipping the special tokens, I get the following:

`<pad> <unk> a fait mal !</s>`

`<unk>` is right where the translation should include a French Ç (expected result “Ça fait mal” from source “That hurts!”). Note:

- lower case ç works just fine.
- Exact same issue with È: `<pad> APR<unk> S VOUS !</s>` (should be “APRÈS VOUS !”)

It’s definitely not a model issue, but a me issue, if I try on OpusTranslate Space ([OPUS Translate - a Hugging Face Space by Helsinki-NLP](https://huggingface.co/spaces/Helsinki-NLP/opus-translate)), it works just fine.

I tried using the code verbatim from the model page, to no avail ([Helsinki-NLP/opus-mt-tc-big-en-fr · Hugging Face](https://huggingface.co/Helsinki-NLP/opus-mt-tc-big-en-fr))

My current code is not far from it, and produces exactly the result I posted above:

```auto
def __init__ (self, model_path_or_name: str, source_language:str, target_langueg:str):
    self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    self.tokenizer = MarianTokenizer.from_pretrained(model_path_or_name)
    self.model = MarianMTModel.from_pretrained(model_path_or_name).to(self.device)

def single_translate(self, text: str) -> str:
    """
    Translate a single sentence and return the translated string only.
    """
    inputs = self.tokenizer([text], return_tensors="pt", padding=True, truncation=True)
    input_ids = inputs.input_ids.to(self.model.device)
    with torch.no_grad():
        outputs = self.model.generate(input_ids=input_ids)
    decoded = self.tokenizer.batch_decode(outputs, skip_special_tokens=False)
    return decoded[0]

```

Any advice would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [June 6, 2025, 12:58pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fopus-mt-translation-returns-unk-token%2F158124%2F2 "2025-06-06T12:58:25Z")

</div>

It seems model issue…

```py
from transformers import pipeline
pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
print(pipe("That hurts!")) # [{'translation_text': 'Ça fait mal !'}]
pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-big-en-fr")
print(pipe("That hurts!")) # [{'translation_text': 'a fait mal !'}]

```

---

<div class="post-metadata">

**Author:** ![mathdons](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@mathdons](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmathdons)%5C
**Post date:** [June 6, 2025, 1:37pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fopus-mt-translation-returns-unk-token%2F158124%2F3 "2025-06-06T13:37:55Z")

</div>

Damn, it never occurred to me that the space could be using a different model in the same family/language. Thanks a lot, you’ve saved me a lot of headaches trying to find what was going wrong. Going to add a comment on the model / community page.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [June 7, 2025, 1:38am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fopus-mt-translation-returns-unk-token%2F158124%2F4 "2025-06-07T01:38:40Z")

</div>

Closed on June 7, 2025, 1:38am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
