Sub topic 13
Multilingual Generative Models and Prompting Across Languages
Assume you know Python, the Hugging Face transformers library, tokenization, and basic text generation. This chapter focuses on decoder and encoder-decoder models that generate text, rather than only classifying it.
What makes a generative model multilingual?
A multilingual generative model is trained to produce text in more than one language. Models such as mT5 learn from multilingual text using a shared subword vocabulary and shared parameters. During generation, the model predicts one token at a time based on the prompt and the tokens it has already produced.
This creates several useful behaviors:
- It can answer in the language used by the prompt.
- It can follow an English instruction while producing a French or Hindi answer.
- It can translate, summarize, or rewrite across languages.
- It can use patterns learned in one language to help another language.
The model does not store a separate, perfectly isolated language system for every language. Its parameters are shared. This saves deployment and maintenance costs, but languages can compete for vocabulary space, training examples, and model capacity.
Prompt language and output language
A prompt has at least two important parts:
- The task instruction, such as “summarize this text.”
- The requested output language, such as “Answer in German.”
State the desired language explicitly. Do not assume that the model will infer it correctly, especially when the prompt contains several languages.
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model_name = "google/mt5-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
prompts = [
"Translate to French: The meeting starts at nine.",
"Answer in Spanish: What is a database?",
]
for prompt in prompts:
inputs = tokenizer(prompt, return_tensors="pt")
output_ids = model.generate(
**inputs,
max_new_tokens=48,
num_beams=4,
)
output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
print(f"Prompt: {prompt}\nOutput: {output}\n")This example downloads the model the first time it runs. num_beams=4 asks beam search to keep several likely continuations instead of choosing each token independently. The model may still produce an incomplete or incorrect answer. mT5 is multilingual, but it is not necessarily instruction-tuned for reliable chat behavior.
A practical prompting workflow
Use this sequence when prompting across languages:
- Identify the input language and the required output language.
- State the task with a short, direct instruction.
- Keep examples in the same format as the real inputs.
- Specify constraints such as length, tone, and whether names should remain unchanged.
- Check that the output language and meaning are correct.
For example, a translation prompt can include: “Translate from Swahili to English. Preserve names and numbers. Return only the translation.” A summarization prompt can say: “Summarize the following Japanese text in three English bullet points.”
Common failure modes
Prompting across languages can fail in predictable ways:
- Language drift: The answer begins in the requested language and switches to another.
- Unequal instruction following: Instructions work better in high-resource languages.
- Translationese: The output is grammatical but follows the source language's word order.
- Code-switching: The model mixes languages, sometimes because the prompt does.
- Cultural mismatch: A literal translation misses local conventions or implied meaning.
- Tokenization cost: Some scripts require more tokens, reducing the usable context window.
A language identifier, maximum output length, and post-generation validation can catch some of these problems. For important applications, use human review or a language-specific evaluator.
Choosing a prompting strategy
Use direct prompting when the model follows instructions reliably and the task is low risk. Add a bilingual example when the task has a specialized format. For high-stakes translation or extraction, compare the result with a dedicated translation or language-specific system.
Measure each language separately. A single average score can hide poor performance in a low-resource language. Test language identification, task correctness, and factual consistency rather than fluency alone.
Exercise
Why should a prompt explicitly name the required output language?
Show answer
Shared multilingual parameters allow the model to generate more than one language, so an explicit language instruction reduces language drift and makes the desired behavior testable.